【上机训练】输入格式

  1. a+b

    题目描述:

    Calculate a+b

    输入

    Two integer a,b (0<=a,b<=10)

    输出

    Output a+b

    样例输入

    1 2

    样例输出

    3

    解法思路

    代码

    	#include <stdio.h>
    	int main(){
          
          
    	   int a;
    		int b;
    		scanf("%d%d",&a,&b);
    		printf("%d\n",a+b);
    	return 0;
    	}
    
  2. a+b(多组输入,未知组数)

    题目描述:

    Your task is to Calculate a + b.
    Too easy?! Of course! I specially designed the problem for acm beginners.
    You must have found that some problems have the same titles with this one, yes, all these problems were designed for the same aim.

    输入

    The input will consist of a series of pairs of integers a and b, separated by a space, one pair of integers per line.

    输出

    For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

    样例输入

    1 5
    10 20

    样例输出

    6
    30

    解法思路

    代码

    	#include <stdio.h>
    	int main(){
          
          
    		 int a;
    		 int b;
    		 while(scanf("%d%d",&a,&b) != EOF){
          
           //不等于输入文件结尾就一直请求输入(多组输入)
       			 printf("%d\n",a+b);
    		}
    	  return 0;
    	}
    
  3. a+b(多组输入,指定组数)

    题目描述:

    Your task is to Calculate a + b.

    输入

    Input contains an integer N in the first line, and then N lines follow. Each line consists of a pair of integers a and b, separated by a space, one pair of integers per line.

    输出

    For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

    样例输入

    2
    1 5
    10 20

    样例输出

    6
    30

    解法思路

    代码

    #include <stdio.h>
    
    int main(){
          
          
    	 int a;
    	 int b;
    	 int n;	//输入组数
    	 scanf("%d",&n);
    	 while(n--){
          
          
     		scanf("%d%d",&a,&b);
    	    printf("%d\n",a+b);
    	 }
    	 return 0;
    }
    
  4. a+b(多组输入,输入到特定值的时候跳出)

    题目描述:

    Your task is to Calculate a + b.

    输入

    Input contains multiple test cases. Each test case contains a pair of integers a and b, one pair of integers per line. A test case containing 0 0 terminates the input and this test case is not to be processed.

    输出

    For each pair of input integers a and b you should output the sum of a and b in one line, and with one line of output for each line in input.

    样例输入

    1 5
    10 20
    0 0

    样例输出

    6
    30

    解法思路

    代码

    #include <stdio.h>
    
    int main(){
          
          
    	int a;
    	int b;
      	while(scanf("%d%d",&a,&b)!= EOF){
          
          
       		 if(a==0 && b==0){
          
           //输入0 0时终止
            	break;
       		 }
      		printf("%d\n",a+b);
    	}
    	return 0;
    }
    
    

猜你喜欢

转载自blog.csdn.net/Qmilumilu/article/details/114460958