ACM模式输入输出总结

单组输入

数量固定

数量不固定

多组输入

多组数量固定-单组数量固定

A+B(2)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main() {
int t;
cin >> t;

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

A+B(5)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main() {
int t;
cin >> t;

while (t --) {
int n;
cin >> n;

int x, sum = 0;
while (n --) cin >> x, sum += x;

cout << sum << endl;
}

return 0;
}

多组数量固定-单组数量不固定

多组数量不固定-单组数量固定

A+B(1)

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

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

A+B(3)

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

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

A+B(4)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

using namespace std;

int main()
{
int n;
while(cin >> n, n)
{
int x, sum = 0;

while(n--) cin >> x, sum += x;
cout << sum << endl;
}

return 0;
}

A+B(6)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>

using namespace std;

int main() {
int n;
while (cin >> n) {
int sum = 0, x;

while (n --) cin >> x, sum += x;
cout << sum << endl;
}

return 0;
}

多组数量不固定-单组数量不固定

A+B(7)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main() {
string s;
while (getline(cin, s)) {
stringstream line(s);
int sum = 0, x;
while (line >> x) sum += x;
cout << sum << endl;
}
return 0;
}

字符串排序(2)

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

using namespace std;

int main() {
string s;
while (getline(cin, s)) {
stringstream line(s);

vector<string> res;
string x;

while (line >> x) res.push_back(x);
sort(res.begin(), res.end());

for (auto &s: res) cout << s << " ";
cout << endl;
}
return 0;
}

字符串排序(3)

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 <string>
#include <vector>
#include <sstream>
#include <algorithm>

using namespace std;

int main() {
string s;
while (getline(cin, s)) {
stringstream line(s);

string x;
vector<string> res;
while (getline(line, x, ',')) res.push_back(x);
sort(res.begin(), res.end());

for (int i = 0; i < res.size(); i ++ ) {
if (i == res.size() - 1) cout << res[i] << endl;
else cout << res[i] << ",";
}
}
return 0;
}

ACM模式输入输出总结
https://vendestine.com/acm_io
Author
Wenzhe Li
Posted on
June 4, 2023
Licensed under