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

img

ACM的时候才知道自己连基本的输入都难以搞定!!!耻辱柱定死了~~~😭😭😭

如果大家有更好的方法一定给我分享一下,我太菜了👤

题目

A+B(1))||A+B(2))||A+B(3))||A+B(4))||A+B(5))||A+B(6))||A+B(7))||字符串排序(1))||字符串排序(2))||字符串排序(3))

A+B(1)

链接:https://ac.nowcoder.com/acm/contest/5650/A
来源:牛客网

题目描述

计算a+b

输入描述:

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

输出描述:

1
输出a+b的结果

示例1

输入

1
2
1 5
10 20

输出

1
2
6
30

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> a;
int ta;
cin >> ta;
while (cin) {
a.push_back(ta);
cin >> ta;
}
for (int i = 0; i < a.size(); i+=2)
cout << a[i] + a[i+1] << endl;
return 0;
}

A+B(2)

链接:https://ac.nowcoder.com/acm/contest/5650/B
来源:牛客网

题目描述

计算a+b

输入描述:

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

输出描述:

1
输出a+b的结果

示例1

输入

1
2
3
2
1 5
10 20

输出

1
2
6
30

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int t;
cin >> t;
vector<int> nums;
while (t-- && cin) {
int a,b;
cin >> a >> b;
nums.push_back(a+b);
}
for (auto it : nums)
cout << it << endl;
system("pause");
return 0;
}

A+B(3)

链接:https://ac.nowcoder.com/acm/contest/5650/C
来源:牛客网

题目描述

计算a+b

输入描述:

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

输出描述:

1
输出a+b的结果

示例1

输入

1
2
3
1 5
10 20
0 0

输出

1
2
6
30

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int a, b;
cin >> a >> b;
vector<int> nums;
while (cin && a != 0 && b != 0) {
nums.push_back(a + b);
cin >> a >> b;
}
for (auto it : nums)
cout << it << endl;
system("pause");
return 0;
}

A+B(4)

链接:https://ac.nowcoder.com/acm/contest/5650/D
来源:牛客网

题目描述

计算一系列数的和

输入描述:

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

输出描述:

1
每组数据输出求和的结果

示例1

输入

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

输出

1
2
10
15

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> nums;
int length;
cin >> length;
while (length != 0) {
int ans = 0;
for (int i = 0; i < length; ++i) {
int temp;
cin >> temp;
ans += temp;
}
nums.push_back(ans);
cin >> length;
}
for (auto it : nums)
cout << it << endl;
system("pause");
return 0;
}

A+b(5)

链接:https://ac.nowcoder.com/acm/contest/5650/E
来源:牛客网

题目描述

计算一系列数的和

输入描述:

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

输出描述:

1
每组数据输出求和的结果

示例1

输入

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

输出

1
2
10
15

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
using namespace std;
int main()
{
int row;
cin >> row;
vector<int> res;
while (row--) {
int length = 0, ans = 0;
cin >> length;
for (int i = 0; i < length; ++i) {
int temp = 0;
cin >> temp;
ans += temp;
}
res.push_back(ans);
}
for (auto i : res)
cout << i << endl;
system("pause");
return 0;
}

A+B(6)

链接:https://ac.nowcoder.com/acm/contest/5650/F
来源:牛客网

题目描述

计算一系列数的和

输入描述:

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

输出描述:

1
每组数据输出求和的结果

示例1

输入

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

输出

1
2
10
15

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> nums;
int length;
cin >> length;
while (cin) {
int ans = 0;
for (int i = 0; i < length; ++i) {
int temp;
cin >> temp;
ans += temp;
}
nums.push_back(ans);
cin >> length;
}
for (auto it : nums)
cout << it << endl;
system("pause");
return 0;
}

A+B(7)

链接:https://ac.nowcoder.com/acm/contest/5650/G
来源:牛客网

题目描述

计算一系列数的和

输入描述:

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

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

输出描述:

1
每组数据输出求和的结果

示例1

输入

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

输出

1
2
3
6
9
0

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> sum;
vector<vector<int>> res;
while (!cin.eof()) {
vector<int> nums;
int ans = 0;
int temp = 0;
while(cin){
cin >> temp;
ans += temp;
// nums.push_back(temp); //把结果保存在nums中
if (getchar() == '\n') break;
}
// res.push_back(nums); //把nums保存在res中
sum.push_back(ans);
}
for (int i = 0; i < sum.size()-1; ++i)
cout << sum[i] << endl;
system("pause");
return 0;
}

字符串排序(1)

链接:https://ac.nowcoder.com/acm/contest/5650/H
来源:牛客网

题目描述

对输入的字符串进行排序后输出

输入描述:

1
2
3
输入有两行,第一行n

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

输出描述:

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

示例1

输入

1
2
5
c d a bb e

输出

1
a bb c d e

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<string> vstr;
int length;
cin >> length;
for (int i = 0; i < length; ++i) {
string str;
cin >> str;
vstr.push_back(str);
}
sort(vstr.begin(), vstr.end());
for (auto it : vstr)
cout << it << " ";


system("pause");
return 0;
}

字符串排序(2)

链接:https://ac.nowcoder.com/acm/contest/5650/I
来源:牛客网

题目描述

对输入的字符串进行排序后输出

输入描述:

1
2
3
多个测试用例,每个测试用例一行。

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

输出描述:

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

示例1

输入

1
2
3
a c bb
f dddd
nowcoder

输出

1
2
3
a bb c
dddd f
nowcode

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
vector<vector<string> > res;
while (!cin.eof()) {
vector<string> vstr;
while (cin) {
string str;
cin >> str;
vstr.push_back(str);
if (getchar() == '\n') {
sort(vstr.begin(), vstr.end());
break;
}
}
res.push_back(vstr);
}
//输出
for (int i = 0; i < res.size(); ++i) {
cout << res[i][0];
for (auto j = 1;j < res[i].size(); ++j)
cout << " " << res[i][j];
cout << endl;
}
system("pause");
return 0;
}

字符串排序(3)

链接:https://ac.nowcoder.com/acm/contest/5650/J
来源:牛客网

题目描述

对输入的字符串进行排序后输出

输入描述:

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

输出描述:

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

示例1

输入

1
2
3
a,c,bb
f,dddd
nowcoder

输出

1
2
3
a,bb,c
dddd,f
nowcoder

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include<iostream>
#include<string>
#include<vector>
#include <sstream>
#include<algorithm>
using namespace std;

int main() {
string s;
vector<string> vec;
while (cin >> s) {
istringstream input(s);
string t;
while (getline(input, t, ','))
vec.push_back(t);
if (cin.get() == '\n') {
sort(vec.begin(), vec.end());
for (int i = 0; i < vec.size(); i++) {
if (i) cout << "," << vec[i];
else cout << vec[i];
}
cout << endl;
vec.clear();
}
}

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<sstream>
using namespace std;

int main()
{
string str;
vector<string>array;
while(getline(cin,str))
{
istringstream strs(str);
string strt;
while(getline(strs,strt,','))
array.push_back(strt);
sort(array.begin(),array.end());
for(auto iter=array.begin();iter!=array.end()-1;iter++)
cout<<*iter<<',';
cout<<array.back()<<endl;
array.clear();
}
return 0;
}