ACM HDU 1029 Ignatius and the Princess IV(JAVA)import java.util.Scanner;public class Main {public static void main(String args[]) {Scanner ss = new Scanner(System.in);long n, num, cnt, res = 0;n = ss.nextLong();while (true) { if (!ss.hasNext()) break;cnt = 0;while (n!=0) {n--;num = ss.nextLong();if (0 == cnt) {res = num;cnt++;} else {if (res == num)cnt++;elsecnt--;}}System.out.println(res);n = ss.nextLong();}}}为什么总是WA.
ACM HDU 1029 Ignatius and the Princess IV(JAVA)
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner ss = new Scanner(System.in);
long n, num, cnt, res = 0;
n = ss.nextLong();
while (true) {
if (!ss.hasNext()) break;
cnt = 0;
while (n!=0) {
n--;
num = ss.nextLong();
if (0 == cnt) {
res = num;
cnt++;
} else {
if (res == num)
cnt++;
else
cnt--;
}
}
System.out.println(res);
n = ss.nextLong();
}
}
}
为什么总是WA.
是读取的问题. 最后一组数据没有被处理
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner ss = new Scanner(System.in);
long n, num, cnt, res = 0;
while(ss.hasNextLong())
{
n = ss.nextLong();
cnt = 0;
while (n!=0) {
n--;
num = ss.nextLong();
if (0 == cnt) {
res = num;
cnt++;
} else {
if (res == num)
cnt++;
else
cnt--;
}
}
System.out.println(res);
}
}
}