在做一道算法题,结果总是RuntimeError,一步步查后发现通过keySet()遍历与通过entrySet()遍历产生结果不同,前者产生RuntimeEror(第二个注释块),后者测试通过(第一个注释块)。
有点晕了,不知道是什么原因import java.math.*;
import java.util.*;public class Main
{
static int[] ten = {1, 10, 100, 1000, 10000, 100000, 1000000}; static char check(char c)
{
if(c <= '9' && c >= '0')
{
return c;
}
if(c < 'Q')
{
return (char)('2' + (c - 'A') / 3);
}
else
{
return (char)('7' + (c - 'Q') / 3);
}
} static class Counter
{
public int value = 1;
} public static void main(String[] args)
{
try
{
Scanner scan = new Scanner(System.in);
int total = scan.nextInt();
Map<String, Counter> map = new TreeMap<String, Counter>(); scan.nextLine();
char c;
for(int i = 0; i < total; i++)
{
String temp = scan.nextLine();
StringBuilder builder = new StringBuilder();
for(int j = 0, k = 0; j < temp.length(); j++)
{
if(temp.charAt(j) == '-')
{
continue;
}
c = check(temp.charAt(j));
builder.append(c);
} String str = builder.toString();
Counter counter = (Counter)map.get(str);
if(counter == null)
{
counter = new Counter();
map.put(str, counter);
}
else
{
counter.value++;
}
} boolean hasResult = false;
/* 去掉注释测试通过
for (Map.Entry entry : map.entrySet())
{
String str = entry.getKey().toString();
Counter counter = map.get(str);//(Counter)entry.getValue();结果均正确
if (counter.value > 1)
{
System.out.println(str.substring(0, 3) + "-" + str.substring(3) + " " + counter.value);
hasResult = true;
}
}*/ /* 去掉注释测试不通过 Runtime Error
for(String str : map.keySet())
{
Counter counter = map.get(str);
if(counter.value > 1)
{
System.out.println(str.substring(0, 3) + "-" + str.substring(3) + " " + counter.value);
hasResult = true;
}
}*/ if(!hasResult)
{
System.out.print("No duplicates.");
return;
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
有点晕了,不知道是什么原因import java.math.*;
import java.util.*;public class Main
{
static int[] ten = {1, 10, 100, 1000, 10000, 100000, 1000000}; static char check(char c)
{
if(c <= '9' && c >= '0')
{
return c;
}
if(c < 'Q')
{
return (char)('2' + (c - 'A') / 3);
}
else
{
return (char)('7' + (c - 'Q') / 3);
}
} static class Counter
{
public int value = 1;
} public static void main(String[] args)
{
try
{
Scanner scan = new Scanner(System.in);
int total = scan.nextInt();
Map<String, Counter> map = new TreeMap<String, Counter>(); scan.nextLine();
char c;
for(int i = 0; i < total; i++)
{
String temp = scan.nextLine();
StringBuilder builder = new StringBuilder();
for(int j = 0, k = 0; j < temp.length(); j++)
{
if(temp.charAt(j) == '-')
{
continue;
}
c = check(temp.charAt(j));
builder.append(c);
} String str = builder.toString();
Counter counter = (Counter)map.get(str);
if(counter == null)
{
counter = new Counter();
map.put(str, counter);
}
else
{
counter.value++;
}
} boolean hasResult = false;
/* 去掉注释测试通过
for (Map.Entry entry : map.entrySet())
{
String str = entry.getKey().toString();
Counter counter = map.get(str);//(Counter)entry.getValue();结果均正确
if (counter.value > 1)
{
System.out.println(str.substring(0, 3) + "-" + str.substring(3) + " " + counter.value);
hasResult = true;
}
}*/ /* 去掉注释测试不通过 Runtime Error
for(String str : map.keySet())
{
Counter counter = map.get(str);
if(counter.value > 1)
{
System.out.println(str.substring(0, 3) + "-" + str.substring(3) + " " + counter.value);
hasResult = true;
}
}*/ if(!hasResult)
{
System.out.print("No duplicates.");
return;
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
之前明明是RE的,我又从网上拷了之前RE的代码,测试却通过了,估计是平台的事...