为了测试数据,在网上看了好多思想,还是坚持自己最初的想法,字符转成数组。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while (s.hasNext()) {
for (int i = s.nextInt(); i >= 1; i--) {
int[] a = new int[12];// 把字符对应成int数组
int count = 0;// 不平衡的次数 = 假币出现的次数
for (int j = 0; j < 3; j++) {
String left = s.next();
String right = s.next();
switch (s.next().charAt(0)) {
case 'd':
for (char c : left.toCharArray()) {
a[c - 65] -= 4;
}
for (char c : right.toCharArray()) {
a[c - 65] += 4;
}
count++;
break;
case 'e':
for (char c : left.toCharArray()) {
a[c - 65]++;
}
for (char c : right.toCharArray()) {
a[c - 65]++;
}
break;
case 'u':
for (char c : left.toCharArray()) {
a[c - 65] += 4;
}
for (char c : right.toCharArray()) {
a[c - 65] -= 4;
}
count++;
break;
}
}
for (int j = 0; j < 12; j++) {
if (a[j] % (4 * count) == 0) {
if (a[j] > 0) {
System.out.println((char) (j + 65) + " is the counterfeit coin and it is heavy.");
break;
} else if (a[j] < 0) {
System.out.println((char) (j + 65) + " is the counterfeit coin and it is light.");
break;
}
}
}
}
break;
}
s.close();
}
}