ACM输入输出 (C++)

(14条未读私信) 牛客竞赛_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ (nowcoder.com)

1. A+B(1)

输入两个数 ,返回两者相加结果

输入:

1 5

10 20

输出:

6

30 

#include <iostream>
using namespace std;

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

 2. A+B(2)

输入第一行表示下列每一行输入的数据量n

接下来每行输入n个数据

输入:

2

1 5

10 20

输出:

6

30 

#include <iostream>
using namespace std;

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

3. A+B(3)

输入多行数据

知道 a b 都为0时停止

输入:

1 5
10 20
0 0

输出:

 6
30

#include <iostream>
using namespace std;

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

 4.A+B(4)

计算一系列的数,每行的第一个数据n为该行的个数 n为0时停止

返回每一行的和 

输入:

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

 输出:

10
15

#include <iostream>
using namespace std;
int main()
{
    int a,b;
    while(cin>>a){
        if(a == 0) break;
        int sum = 0;
        while(a--){
            cin>>b;
            sum += b;
        }
        cout<< sum <<endl;
    }
}

5. A+B(5)


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

输入:

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

输出:

10
15

#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n, start, num;
    cin >> n;
    while (n--) {
        int sum{0};
        cin >> start;
        for (int i = 0; i < start; i++) {
            cin >> num;
            sum += num;
        }
        cout << sum <<endl;
    }
    return 0;
}
#include <bits/stdc++.h>
using namespace std;

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

 6. A+B(6)

 多组数据,每行表示一组输入数据 换行符终止 ' \n '  并终止计算sum

输入:

 1 2 3
4 5
0 0 0 0 0

输出:

 6
9
0

#include <iostream>
using namespace std;

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

 7. 字符串排序(1)

输入俩行,第一行n

第二行是n个字符串,字符串之间用空格隔开

输入:

 5
c d a bb e

 输出:

a bb c d e

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    int n;
    cin>>n;
    vector<string> res;
    while(n--)
    {
        string a;
        cin>>a;
        res.push_back(a);
    }
    sort(res.begin(),res.end());
    for(auto s:res){
        cout<<s<<" ";
    }
    cout<<endl;
}

  8. 字符串排序(2)多行

多个测试用例 

多行 换行符结尾 '\n'

输入:

a c bb
f dddd
nowcoder

输入:

 a bb c
dddd f
nowcoder

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
int main(){
    string a;
    vector<string> res; 
    while(cin>>a){
        res.push_back(a);
        if(cin.get()=='\n'){
            sort(res.begin(), res.end());
            for(auto i : res)
            {
                cout<<i<<" ";
            }
            cout << endl;
            res.clear();
            
        }
    }
    cout<<endl;
}

 8. 字符串排序()逗号隔开

输入:

a,c,bb
f,dddd
nowcoder

输出:

 a,bb,c
dddd,f
nowcoder

#include<bits/stdc++.h>

using namespace std;

int main() {
    vector<string> vec;
    string s;
    while (getline(cin, s)) {
        int p = 0;
        for(int q = 0; q < s.size(); q++) {
		  p = q;
		  while(p < s.size() && s[p] != ',') {
			p++;
		  }
		  vec.push_back(s.substr(q, p - q));
		  q = p;
	    }
  
        sort(vec.begin(), vec.end());
        
        for (int i = 0; i < vec.size(); ++i) {
            if (i != vec.size() - 1)
                cout << vec[i] << ",";
            else
                cout << vec[i];
        }
        cout << endl;
        vec.clear();
    }
    
    
    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值