牛客网输入输出11道题——C++版本

第1题

输入描述:

输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组。

输出描述:

输出a+b的结果

输入例子1:

1 5
10 20

输出例子1:

6
30

我的代码

#include <iostream>

using namespace std;

int main()
{
    int a, b;
    while(cin >> a >> b)
    {
        cout << a + b << endl;
    }
    
    return 0;
}

第2题

输入描述:

输入第一行包括一个数据组数t(1 <= t <= 100)
接下来每行包括两个正整数a,b(1 <= a, b <= 10^9)

输出描述:

输出a+b的结果

输入例子1:

2
1 5
10 20

输出例子1:

6
30

我的代码

#include <iostream>

using namespace std;

int main()
{
    int t;
    int a, b;
    cin >> t;
    while(t--)
    {
        cin >> a >> b;
        cout << a + b << endl;
     }
    
    return 0;
}

第3题

输入描述:

输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入

输出描述:

输出a+b的结果

输入例子1:

1 5
10 20
0 0

输出例子1:

6
30

我的代码

#include <iostream>

using namespace std;

int main()
{
    int a, b;
    while(cin >> a >> b)
    {
        if(a == 0 && b == 0)
            break;
        cout << a + b << endl;
    }
    
    return 0;
}

第4题

输入描述:

输入数据包括多组。
每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
接下来n个正整数,即需要求和的每个正整数。

输出描述:

每组数据输出求和的结果

输入例子1:

4 1 2 3 4
5 1 2 3 4 5
0

输出例子1:

10
15

我的代码

#include <iostream>

using namespace std;

int main()
{
    int n;
    while(cin >> n)
    {
        if(n == 0)
            break;
        int sum = 0, a = 0;
        for(int i = 0; i < n; i++)
        {
            cin >> a;
            sum += a;
        }
        cout << sum << endl;
        
    }
    
    return 0;
}

第5题

输入描述:

输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。
接下来t行, 每行一组数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。

输出描述:

每组数据输出求和的结果

输入例子1:

2
4 1 2 3 4
5 1 2 3 4 5

输出例子1:

10
15

我的代码

#include <iostream>
using namespace std;

int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        int sum = 0, a = 0;
        while(n--)
        {
            cin >> a;
            sum += a;
        }
        cout << sum << endl;
    }
    
    return 0;
}

第6题

输入描述:

输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。

输出描述:

每组数据输出求和的结果

输入例子1:

4 1 2 3 4
5 1 2 3 4 5

输出例子1:

10
15

我的代码

#include <iostream>

using namespace std;

int main()
{
    int n;
    while(cin >> n)
    {
        int sum = 0, a = 0;
        for(int i = 0; i < n; i++)
        {
            cin >> a;
            sum += a;
        }
        cout << sum << endl;
    }
    
    
    return 0;
}

第7题

输入描述:

输入数据有多组, 每行表示一组输入数据。

每行不定有n个整数,空格隔开。(1 <= n <= 100)

输出描述:

每组数据输出求和的结果

输入例子1:

1 2 3
4 5
0 0 0 0 0

输出例子1:

6
9
0

我的代码

#include <iostream>

using namespace std;

int main()
{
    int sum = 0, a;
    while(cin >> a)
    {
        sum += a;
        if(cin.get() == '\n')
        {
            cout << sum << endl;
            sum = 0;
        }
    }
    
    return 0;
}

第8题

输入描述:

输入有两行,第一行n

第二行是n个空格隔开的字符串

输出描述:

输出一行排序后的字符串,空格隔开,无结尾空格

输入例子1:

5
c d a bb e

输出例子1:

a bb c d e

我的代码

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
    int n;
    cin >> n;
    vector<string> str(n);
    for(int i = 0; i < n; i++)
    {
        cin >> str[i];
    }
    sort(str.begin(), str.end());
    
    for(int i = 0; i < n; i++)
        cout << str[i] << ' ';
    cout << endl;
    
    return 0;
}

第9题

输入描述:

多个测试用例,每个测试用例一行。

每行通过空格隔开,有n个字符,n<100

输出描述:

对于每组测试用例,输出一行排序过的字符串,每个字符串通过空格隔开

输入例子1:

a c bb
f dddd
nowcoder

输出例子1:

a bb c
dddd f
nowcoder

我的代码

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main()
{
    string tmp;
    vector<string> str;
    while(cin >> tmp)
    {
        str.push_back(tmp);
        if(cin.get() == '\n')
        {
            sort(str.begin(), str.end());
            for(int i = 0; i < str.size(); i++)
                cout << str[i] << ' ';
            cout << endl;
            str.clear();
        }
    } 
    return 0;
}

第10题

输入描述:

多个测试用例,每个测试用例一行。
每行通过,隔开,有n个字符,n<100

输出描述:

对于每组用例输出一行排序后的字符串,用','隔开,无结尾空格

输入例子1:

a,c,bb
f,dddd
nowcoder

输出例子1:

a,bb,c
dddd,f
nowcoder

我的代码 有yi点点难

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>


using namespace std;

int main()
{
    string oneline;//oneline表示某一行的数据
    while(cin >> oneline)
    //输入一行的数据内部,没有出现空格,故可以用cin >> oneline
    //否则得用getline(cin, oneline)
    //cin碰到空格或换行符就终止读入!!!
    {
        istringstream mycin(oneline); //将oneline中的数据用输入流mycin重新播发
        string tmp;
        vector<string> str;
        while(getline(mycin, tmp, ','))
            str.push_back(tmp);
        sort(str.begin(), str.end());
        for(int i = 0; i < str.size() - 1; i++)
            cout << str[i] << ",";
        cout << str[str.size() - 1] << endl;
    }
    
    return 0;
}

第11题

输入描述:

对于每组数据输出一行两个整数的和

输入例子1:

1 1

输出例子1:

2

我的代码

#include <iostream>

using namespace std;

int main()
{
    long long a, b;
    while(cin >> a >> b)
        cout << a + b << endl;
    return 0;
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YMWM_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值