A+B

image

点击查看代码
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
    int a,b;
    cin >> a >> b;
    int result = a + b;
    cout << result << endl;
    return 0;
}
上述基础代码只能输出一次数据如果想要正确完成题目要求需要借助循环 A + B2:

image

注意N要持续输入

点击查看代码
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
    int a,b,N,result;
    while (cin >> N)
    {
        while(N){  
        cin >> a >> b;
        result = a + b;
        cout << result << endl;
        N--;
        }
    }
    return 0;
}

A + B3:

image

点击查看代码
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
    int a,b,result;
    while(cin >> a >> b)
    {
        if(a == 0 && b == 0){
            break;
        }
        result = a + b;
        cout << result << endl;
    }
    return 0;
}

A + B 4:

image

点击查看代码
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main(){
    int a,N;
    int b = 0;
    while(cin >> N){
        b = 0;    //注意刷新b的值
        if(N == 0){
            break;
        }
        while(N--){//注意退出循环条件
            cin >> a;
            b+=a;
        }
        cout << b << endl;
    }
    return 0;
 }

A + B 5:

image

注意最后空格输入的条件:
但是题目要求每组数据内部的输出(也就是每行数据之间)是有空行的,但两组数据之间并没有空行,所以在每组数据的最后一行,也就是n被修改为0的时候,不需要输出空行。

点击查看代码
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main(){
    int M,N,a;
    int sum = 0;
    while(cin >> N){
        if(N == 0){
            break;
        }
        while(N--){
            sum = 0;
            cin >> M;
            while(M--){
                cin >> a;
                sum+=a;
            }
            cout << sum << endl ;
            if(N!=0){           //n被修改为0的时候,不需要输出空行
                cout << endl;
            }
        }
    }
    return 0;
}
posted @ 2025-07-11 17:04  Lè_Sage  阅读(18)  评论(0)    收藏  举报