OJ 在线编程常见输入输出练习场

2021-03-07  本文已影响0人  snowhou

牛客网地址

函数相关知识

函数 总结
cin>> 遇到空格和换行自动停止读入,换行空格仍在缓存区中,下次读入自动跳过 读取后面的字符
cin.get() 读入字符
getline(cin,str,delim) 读取整行数据到 str 中 ,delim 分隔符
cin.getline(char*,int) 接收字符串储存到 char * 中,长度为 n 可以接受空格
char a; cin.get(a) a 中可以储存被 cin 丢弃的换行符

A+B(1)

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

1 5
10 20

输出a+b的结果

6
30
#include <iostream>
using namespace std;

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

A+B(2)

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

2
1 5
10 20

输出a+b的结果

6
30
#include <iostream>
using namespace std;

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

A+B(3)

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

1 5
10 20
0 0

输出a+b的结果

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;
}

A+B(4)

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

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

每组数据输出求和的结果

10
15
#include <iostream>
using namespace std;

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

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 <iostream>
using namespace std;

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

A+B(6)

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

4 1 2 3 4
5 1 2 3 4 5

每组数据输出求和的结果

10
15
#include <iostream>
using namespace std;

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

A+B(7)

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

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

1 2 3
4 5
0 0 0 0 0

每组数据输出求和的结果

6
9
0
#include <iostream>
using namespace std;

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

字符串排序 (1)

输入有两行,第一行n

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

5
c d a bb e

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

a bb c d e
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

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

字符串排序 (2)

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

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

a c bb
f dddd
nowcoder

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

a bb c
dddd f
nowcoder
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

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

字符串排序 (3)

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

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

a,c,bb
f,dddd
nowcoder

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

a,bb,c
dddd,f
nowcoder
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <sstream>
using namespace std;

int main() {
    string line, str;
    vector<string> ans;
    while (getline(cin, line)) {
        istringstream sin(line);
        while (getline(sin, str, ',')) {
            ans.push_back(str);
        }
        sort(ans.begin(), ans.end());
        int n = ans.size() - 1;
        for (int i = 0; i < n; i++) {
            cout << ans[i] << ',';
        }
        cout << ans[n] << endl;
        ans.clear();
    }
    return 0;
}

自测本地通过提交为 0

输入有多组测试用例,每组空格隔开两个整数

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

#include <iostream>
using namespace std;

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

#include <iostream>
using namespace std;

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

猜你喜欢

热点阅读