对于输入包含多组测试数据的这种题,该怎么处理(Java语言)
发布于 2017-09-20 18:10 2122 次浏览 0 赞 来自 我要提问  

自知算法没问题,老是死在处理这种多行输入的题中,今天又做了这种的,郁闷死了,明明处理一行是没问题的,可是人家就是多行的,然后耗尽了时间也没处理好,。。。。比如多行输入:

4 2 3 1 

2 2 4 6 

3 2 4 9

每行其实代表的是同一类输入,可是就是不会处理了,各位大佬们,教教我把,该怎么弄啊这种。。。Java语言的。。。


2 条回复
import java.io.*;
import java.util.*;
class Test {
}
public class Main
{
    public static void main(String args[])
    {
        Scanner cin = new Scanner(System.in);
        int a, b, c, d;
        while(cin.hasNextInt())
        {
            a = cin.nextInt();
            b = cin.nextInt();
            c = cin.nextInt();
            d = cin.nextInt();
            System.out.println(a + b + c + d);
        }
    }
}


2017-09-20 19:12

用nextLine()得到一行字符串,再用split()函数分割,再用parseInt()转成数字。

2017-09-20 19:28
添加回复
回到顶部