?? C++輸入輸出
目的:解決ACM模式下的輸入輸出問題
題目來源:牛客OJ練習場
第一題:計算多組a+b
輸入描述:輸入包括兩個正整數a,b(1 <= a, b <= 10^9),輸入資料包括多組,
輸出描述:輸出a+b的結果
示例
輸入:
1 5
10 20
輸出:
6
30
代碼:
#include <iostream>
using namespace std;
int main() {
int a, b;
while (cin >> a >> b) {
cout << a + b << endl;
}
return 0;
}
第二題:告知多少組,計算多組a+b
輸入描述:
輸入第一行包括一個資料組數t(1 <= t <= 100)
接下來每行包括兩個正整數a,b(1 <= a, b <= 10^9)
輸出描述:輸出a+b的結果
示例
輸入:
2
1 5
10 20
輸出:
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都不為0,計算多組a+b
輸入描述:
輸入包括兩個正整數a,b(1 <= a, b <= 10^9),輸入資料有多組, 如果輸入為0 0則結束輸入
輸出描述:輸出a+b的結果
示例
輸入:
1 5
10 20
0 0
輸出:
6
30
代碼:
#include <iostream>
using namespace std;
int main() {
int a, b;
while (cin >> a >> b && a != 0 || b != 0) {
cout << a + b << endl;
}
return 0;
}
第四題:計算一系列數的和(第一個數為0時結束)
輸入描述:
輸入資料包括多組,
每組資料一行,每行的第一個整數為整數的個數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 && n != 0) {
int arr[n];
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
for (int num : arr) {
sum += num;
}
cout << sum << endl;
}
return 0;
}
第五題:計算一系列數的和(告知一共有幾組)
輸入描述:
輸入的第一行包括一個正整數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 n;
cin >> n;
for (int i = 0; i < n; i++) {
int m;
cin >> m;
int arr[m];// 用作記錄用,記錄一下c++創建的陣列資料,如果題目需要就可以用,實際這里并沒有什么用的hhh;
int sum = 0;
for (int i = 0; i < m; i++) {
cin >> arr[i];
sum += arr[i];
}
cout << sum << endl;
}
return 0;
}
第六題:計算一系列數的和(不告知幾組和何時結束,每一組第一個數為一共本組幾個數)
輸入描述:
輸入資料有多組, 每行表示一組輸入資料,
每行的第一個整數為整數的個數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 arr[n];
int sum = 0;
for (int i = 0; i < n; i++) {
cin >> arr[i];
sum += arr[i];
}
cout << sum << endl;
}
return 0;
}
第七題:計算一系列數的和(一行一組,不告訴一組幾個,需要判斷)
輸入描述:
輸入資料有多組, 每行表示一組輸入資料,
每行不定有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 sum = 0;
int num;
while (cin >> num) {
sum += num;
if (cin.get() == '\n') {
cout << sum << endl;
sum = 0;
}
}
return 0;
}
第八題:字串排序(只排一組,告訴有幾個)
輸入描述:
輸入有兩行,第一行n
第二行是n個空格隔開的字串
輸出描述:
輸出一行排序后的字串,空格隔開,無結尾空格
示例
輸入:
5
c d a bb e
輸出:
a bb c d e
代碼:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n;
cin >> n;
vector<string> vec(n);
for (int i = 0; i < n; i++) {
string s;
cin >> s;
vec[i] = s;
}
sort(vec.begin(), vec.end());//用這個排序就行
for (string s: vec) {
cout << s << " ";
}
return 0;
}
第九題:字串排序(排列多組,有結束符)
輸入描述:
多個測驗用例,每個測驗用例一行,
每行通過空格隔開,有n個字符,n<100
輸出描述:
對于每組測驗用例,輸出一行排序過的字串,每個字串通過空格隔開
示例
輸入:
a c bb
f dddd
nowcoder
輸出:
a bb c
dddd f
nowcoder
代碼:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main() {
vector<string> vec;
string s;
while (cin >> s) {
vec.emplace_back(s);
if (cin.get() == '\n') {
sort(vec.begin(), vec.end());
for (int i = 0; i < vec.size() - 1; i++) cout << vec[i] << " ";
cout << vec[vec.size() - 1] << endl;//最后一個元素不帶空格(本題也可以帶空格),直接換行
vec.clear();
}
}
return 0;
}
第十題:字串排序(輸入用逗號分開)
輸入描述:
多個測驗用例,每個測驗用例一行,
每行通過,隔開,有n個字符,n<100
輸出描述:
對于每組用例輸出一行排序后的字串,用’,'隔開,無結尾空格
示例
輸入:
a,c,bb
f,dddd
nowcoder
輸出:
a,bb,c
dddd,f
nowcoder
采用substr函式
代碼:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string s;
while (cin >> s) {
vector<string> vec;
int cur = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] == ',') { //遇到","就拷貝部分字串
vec.emplace_back(s.substr(cur, i - cur));
cur = i + 1;
}
}
vec.emplace_back(s.substr(cur, s.size() - cur));
//排序輸出
sort(vec.begin(), vec.end());
for (int i = 0; i < vec.size() - 1; i++) {
cout << vec[i] << ",";
}
cout << vec[vec.size() - 1] << endl;
}
return 0;
}
第十一題:一個很長的提示
依然是求a+b,不過要用long int/ long long int! 看資料量范圍
這個題很明顯告訴我們:請先不要懷疑系統和題目,一定要檢查自己的代碼!
代碼:
#include <iostream>
using namespace std;
int main() {
long long a, b;
while (cin >> a >> b) {
cout << a + b << endl;
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/484315.html
標籤:C++
上一篇:boost::bind 不能處理函式多載 (error: no matching function for call to 'bind')
下一篇:C++設計模式——單例模式
