我在做去年网易的笔试题,http://www.nowcoder.com/test/970447/summary
里面的第一题,小易大怪兽的那道,我用scanner
对象来获取输入,他的测试用例是个3000多的数组,但是每次scanner
获取到1902个元素之后就阻塞了,不知道是怎么回事。
代码如下:
import java.util.Scanner;
/**
* Created by kbyyd on 2016/3/21.
*/
public class main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
XiaoYi xiaoyi = new XiaoYi(scanner.nextInt());
for (int i = 0; i < n; i++) {
xiaoyi.addAbility(scanner.nextInt());
}
scanner.close();
System.out.println(xiaoyi.getAbility());
}
}
class XiaoYi {
private int ability;
XiaoYi(int ability) {
this.ability = ability;
}
public void addAbility(int b) {
if (ability >= b) ability += b;
else ability += getDivisor(b);
}
public int getAbility() {
return this.ability;
}
private int getDivisor(int b) {
if (b < ability) return 0;
int a = ability;
int s = -1;
do {
s = b % a;
b = a;
a = s;
} while (s != 0);
return b;
}
}
输入有多组数据,你的代码只处理了一组数据就退出了。
加一个
while (scanner.hasNext())
的判断就好了。