大廠筆試 ACM模式常見輸入輸出練習
一、大廠篇
1.1 單行數字,用空格/逗號分割
輸入描述
輸入數據有1行數字,空格分開,表示一組輸入數據。
或
輸入數據有1行數字,逗號','分開,表示一組輸入數據。
4 1 2 3 4
或
4,1,2,3,4
代碼
#include <iostream>
using namespace std;
int main(){
vector<int> data;
int tmp;
while (cin >> tmp) {
//data.push_back(tmp);// 處理數據
if (cin.get() == '\n') break;
}
return 0;
}
1.2 單行字符串,用空格/逗號分割
輸入描述
輸入數據有1行字符串,空格分開,表示一組輸入數據。
或
輸入數據有1行字符串,逗號','分開,表示一組輸入數據。
string char xiao meng
或
string,char,xiao,meng
代碼
#include <iostream>
using namespace std;
int main(){
string str;
vector<string> v;
getline(cin, str);
stringstream ss(str);
while (getline(ss, str, ' ')) {//用空格為分隔符
//while (getline(ss, str, ',')) {//用','為分隔符
v.push_back(str);
}
return 0;
}
2.1 多行數字/字符串,用空格分割
輸入描述
輸入數據有多組, 每行表示一組輸入數據。
每行不定有n個整數,空格隔開。(1 <= n <= 100)。
或
輸入數據有多組, 每行表示一組輸入數據。
每行不定有n個字符串,空格隔開。(1 <= n <= 100)。
1 2 3
4 5
或
a c bb
f dddd
代碼
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
int main(){
vector<string> v;
string s;
while(cin >> s){
v.push_back(s);
if(getchar() == '\n'){
// 處理數據
v.clear();
}
}
return 0;
}
2.2 多行數字/字符串,用逗號分割
輸入描述
輸入數據有多組, 每行表示一組輸入數據。
每行不定有n個整數,','隔開。(1 <= n <= 100)。
或
輸入數據有多組, 每行表示一組輸入數據。
每行不定有n個字符串,','隔開。(1 <= n <= 100)。
1,2,3
4,5
或
a,c,bb
f,dddd
nowcoder
代碼1
#include <iostream>
using namespace std;
#include <sstream>
#include <vector>
#include <algorithm>
int main(){
vector<int> v;
string str;
while(getline(cin,str)){
stringstream ss(str);//定義流對象 復制str到ss
while(getline(ss, str, ',')){//分隔符, 從流ss中取字符到str
v.push_back(stoi(str));
}
// 處理數據
v.clear();
}
return 0;
}
代碼2 (前提已知有 n 行數據)
vector<int> data;
int tmp;
while(n--){
while (cin >> tmp) {
data.push_back(tmp);
if (cin.get() == '\n') break;
}
}
3、二維數組,逗號隔開,同時接收換行符
【范例】輸入
則
輸入描述
二維數組,用空格或','隔開。
12 23 34
23 45 56
34 45 76
或
12,23,34
23,45,56
34,45,76
代碼1
#include <iostream>
using namespace std;
#include <sstream>
#include <vector>
#include <algorithm>
int main(){
int num;
vector<vector<int>> v;
vector<int> tmp;
int n =3;//二維數組行數
while (cin>>num){
tmp.push_back(num);
if (cin.get() == '\n'){
v.push_back(tmp);
n++;
tmp.clear();
}
if(n == 3){
break;
}
}
//二維數組遍歷
for (int i = 0; i < v.size(); i++){
for(int j = 0; j < v[i].size(); j++){
cout << *it <<" "<<endl;
}
cout << endl;
}
return 0;
}
牛客篇
1、
輸入描述
輸入有兩行,第一行n
第二行是n個字符串,字符串之間用空格隔開
5
c d a bb e
代碼
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
int main(){
int n;
cin >> n;
vector<string> v;
string s;
while(n--){
cin >> s;
v.push_back(s);
}
// 處理數據
return 0;
}
2、
輸入描述
輸入數據有多組, 每行表示一組輸入數據。
每行的第一個整數為整數的個數n(1 <= n <= 100)。
接下來n個正整數, 即需要求和的每個正整數。
4 1 2 3 4
5 1 2 3 4 5
代碼
#include <iostream>
using namespace std;
int main(){
int n;
while(cin >> n){
int nums[n];
// 處理數據
}
return 0;
}
輸入描述
輸入的第一行包括一個正整數t(1 <= t <= 100), 表示數據組數。
接下來t行, 每行一組數據。
每行的第一個整數為整數的個數n(1 <= n <= 100)。
接下來n個正整數, 即需要求和的每個正整數。
2
4 1 2 3 4
5 1 2 3 4 5
代碼
#include <iostream>
using namespace std;
int main(){
int n,k;
cin >> k;
while(k--){
while(cin>>n){
int nums[n];
// 處理數據
}
}
return 0;
}
輸入描述
輸入數據包括多組。
每組數據一行,每行的第一個整數為整數的個數n(1 <= n <= 100), n為0的時候結束輸入。
接下來n個正整數,即需要求和的每個正整數。
4 1 2 3 4
5 1 2 3 4 5
0
代碼
#include <iostream>
using namespace std;
int main(){
int n;
while(cin>>n){
if(n == 0){
return 0;
}
int nums[n];
// 處理數據
}
return 0;
}
3、輸入包括兩個正整數a,b(1 <= a, b <= 1000),輸入數據包括多組。
1 5
10 20
代碼
#include <iostream>
using namespace std;
int main(){
int a,b;
while(cin>>a>>b){
// 處理數據
}
return 0;
}
輸入描述
輸入第一行包括一個數據組數t(1 <= t <= 100)
接下來每行包括兩個正整數a,b(1 <= a, b <= 1000)
2
1 5
10 20
代碼
#include <iostream>
using namespace std;
int main(){
int a,b;
int n;
while(n--){
cin>>a>>b;
// 處理數據
}
return 0;
}
輸入描述
輸入包括兩個正整數a,b(1 <= a, b <= 10^9),輸入數據有多組, 如果輸入為0 0則結束輸入
1 5
10 20
0 0
代碼
#include <iostream>
using namespace std;
int main(){
int a,b;
while(cin>>a>>b){
if(a == 0 && b == 0){
return 0;
}
// 處理數據
}
return 0;
}