OJ中循环处理多组输入数据

输入描述:
输入包括两个正整数a,b(1 <= a, b <= 1000),输入数据包括多组。

 

python

 1 import sys
 2 for line in sys.stdin:
 3     a = line.split()
 4     print(int(a[0])+int(a[1]))
 5 
 6 
 7 
 8 
 9 while True:
10     try:
11         a = input().split()
12         print(int(a[0]) + int(a[1]))
13     except:
14         break

 

 

java

 1 import java.util.Scanner;
 2  
 3 // 注意类名必须为 Main, 不要有任何 package xxx 信息
 4 public class Main {
 5     public static void main(String[] args) {
 6         Scanner in = new Scanner(System.in);
 7         // 注意 hasNext 和 hasNextLine 的区别
 8         while (in.hasNextInt()) { // 注意 while 处理多个 case
 9             int a = in.nextInt();
10             int b = in.nextInt();
11             System.out.println(a + b);
12         }
13     }
14 }

 

 1 import java.io.BufferedReader;
 2 import java.io.InputStreamReader;
 3  
 4 public class Main {
 5    public static void main(String[] strr) throws Exception {
 6         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 7         String str = null;
 8         while ((str=br.readLine())!=null){
 9             String[] nums = str.split(" ");
10             int a = Integer.parseInt(nums[0]);
11             int b = Integer.parseInt(nums[1]);
12             System.out.println(a+b);
13         }
14     }
15 }

 

 

c++

#include <iostream>
 
using namespace std;
 
int main()
{
    int n1;
    while(cin >> n1){
        int n2;
        cin >> n2;
        cout << n1 + n2 << endl;
    }
    return 0;
}

 

posted @ 2022-03-27 19:05  r1-12king  阅读(203)  评论(0编辑  收藏  举报