当前位置:编程学习 > JAVA >>

北大poj1002题java做的为何总说Time Limit Exceeded,请大虾帮忙。。。

北大poj1002题java做的为何总说Time Limit Exceeded,请大虾帮忙。。。
具体程序如下:
import java.util.Arrays;
import java.util.Scanner;

public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int x=sc.nextInt();
String[] str=new String[x];
int y=0;
while(y<x){
str[y]=sc.next();
String temp="";
int len=str[y].length();
for(int s=0;s<len;s++){
char ch=str[y].charAt(s);
if(ch>='0'&&ch<='9')
temp=temp+ch;
else if(ch>='A'&&ch<='Z'){
switch(ch){
case 'A':case 'B':
case 'C':ch='2';
    break;
    case 'D':case 'E':
case 'F':ch='3';
break;
case 'G':case 'H':
case 'I':ch='4';
    break;
case 'J':case 'K':
case 'L':ch='5';
break;
case 'M':case 'N':
case 'O':ch='6';
break;
case 'P':case 'R':
case 'S':ch='7';
break;
case 'T':case 'U':
case 'V':ch='8';
break;
case 'W':case 'X':
case 'Y':ch='9';
break;
}
temp=temp+ch;
}
    if(temp.length()==3)
temp=temp+"-";
}
str[y]=temp;
y++;
}
Arrays.sort(str);
boolean flag=false;
for(int i=0;i<x;i++){
int num=1;
if(!str[i].equals("")){
for(int j=i+1;j<x;j++){
if(str[i].equals(str[j])){
num++;
str[j]="";
}
}
if(num>1){
System.out.println(str[i]+" "+num);
 flag=true;
}
}
}
if(!flag)
            System.out.println("No duplicates.");
}
}
--------------------编程问答-------------------- Time Limit Exceeded,是指你的程序的运行速度不够高,需要重新设计算法,减少循环次数;如果可能,消除双重循环。

你不如直接把题目列出来,让大家给你些好的建议。 --------------------编程问答-------------------- 超时了冒失这种比赛对java有保护 给的时间多但是java加载的也确实慢
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,