ACM HDU 1029 Ignatius and the Princess IV(JAVA)

ACM HDU 1029 Ignatius and the Princess IV(JAVA)

题目
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);
}
}
}
举一反三
已知函数f(x)=x,g(x)=alnx,a∈R.若曲线y=f(x)与曲线y=g(x)相交,且在交点处有相同的切线,求a的值和该切线方程.
我想写一篇关于奥巴马的演讲的文章,写哪一篇好呢?为什么好
奥巴马演讲不用看稿子.为什么中国领导演讲要看?
想找英语初三上学期的首字母填空练习……
英语翻译
最新试题
热门考点

超级试练试题库

© 2017-2019 超级试练试题库,All Rights Reserved.