笔试ACM模式输入输出练习c++


前言

笔试ACM模式下的输入输出联系(C++)
cin用法


1.不知道组数,每组数据个数相同,直接输入多组数据,

	#include <iostream>
	using namespace std;
	
	int main(){
	    int a, b;
	    while(cin >> a >> b){
	        cout << a + b << endl;
	    }
	    return 0;
	}


2. 预先知道输入组数,每组数据个数相同,然后输入多组数据

	#include <iostream>
	using namespace std;
	
	int main(){
	    int t, a, b;
	    cin >> t;
	    while(t--){	//for循环也可, for(int i = 0; i < t; ++i)
	        cin >> a >> b;
	        cout << a+ b << endl;
	    }
	    return 0;
	}


3. 输入多组数据,每组数据个数相同,遇到特殊数据停止输入

	#include <iostream>
	using namespace std;
	
	int main(){
	    int a, b;
	    while(cin >> a >> b){
	        if(a == 0 && b == 0) break;
	        cout << a + b << endl;
	    }
	    return 0;
	}


4. 输入多组数据,每组数据个数不定(提前知道每组输入数据个数),遇到特殊数据停止输入

	#include <iostream>
	using namespace std;
	
	int main(){
	    int n, num;
	    
	    while(true){
	    int sum = 0;
	    cin >> n;
	        if(n == 0) {
	            //cout << n << endl;
	            break;
	        }
	        while(n--){
	            cin >> num;
	            sum += num;
	        }
	        cout << sum << endl;
	    }
	    return 0;
	}
/*
#include <iostream>
using namespace std;

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

5. 输入t组数据,每组数据个数不定(但提前知道每组输入数据个数)

	#include <iostream>
	using namespace std;
	
	int main(){
	    int t, n, num;
	    cin >> t;
	    while(t--){
	    int sum = 0;
	    cin >> n;
	        
	        while(n--){
	            cin >> num;
	            sum += num;
	        }
	        cout << sum << endl;
	    }
	    return 0;
	}


6. 输入多组数据,每组数据个数不定(但提前知道每组数据个数),

	#include <iostream>
	using namespace std;
	
	int main(){
	    int n, num;
	   
	    while(cin >> n){
	    int sum = 0;
	    while(n--){
	        cin >> num;
	        sum += num;
	    }
	        cout << sum << endl;
	    }
	    return 0;
	}

7. 输入不定个数的多组数据,数据之间空格隔开, 重点如何换行输出

	#include <iostream>
	using namespace std;
	
	int main(){
	    int num;
	    int sum = 0;
	    while(cin >> num){
	        sum += num;
	        
	        if(cin.get() == '\n') {
	            cout << sum << endl;
	            sum = 0;
	        }
	    }
	    return 0;
}

8. 输入n个字符串(一行),空格隔开,输出时结尾无空格

//这里注意区分char与string
// 补充:strlen和sizeof的区别:
//sizeof将\0也计算在内,strlen不算在内,遇到\0就停止统计

	#include <iostream>
	#include <cstring>
	#include <algorithm>
	#include <vector>
	using namespace std;
	 
	int main()
	{
	    int N = 0;
	    cin >> N;
	    vector<string> str(N, "0");
	    for (int i = 0; i < N; i++)
	        cin >> str[i];
	     
	    sort(str.begin(), str.end());
	    for (string s : str)
	        cout << s << " ";
	}
/*
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
void Print(const vector<string>& str) {
	for (int i = 0; i < str.size(); ++i) {
		cout << str[i] << ' ';
	}
	//cout << str[str.size() - 1] << endl;
}

int main() {
	int n;
	cin >> n;
	string a;
	vector<string> str;
	while (n--) {
		cin >> a;
		str.push_back(a);
	}
	sort(str.begin(), str.end());
	Print(str);

	system("pause");
	return 0;
}
*/


9. 多行字符串(行数未知),每行字符串个数不定,通过空格隔开,重点在于换行输出

	#include <iostream>
	#include <vector>
	#include <string>
	#include <algorithm>
	using namespace std;
	
	int main(){
	    string s;
	    vector<string> str;
	    while(cin >> s){
	        str.push_back(s);
	        if(cin.get() == '\n'){
	            sort(str.begin(), str.end());
	            for(string i : str){
	                cout << i << ' ';
	            }
	            cout << endl;
	            str.clear();
	        }
	    }
	    return 0;
	}


10. 输入多组字符串,每组字符串个数不定,通过‘,‘隔开,输出也通过’,'隔开

//getline()的三个参数
//第一个参数表示从哪里读取
//第二个参数表示存入哪里
//第三个参数表示分隔符
//getline(cin,str) 每次从cin得到一行,存入str
//istringstream ss(str) 表示把str存入ss这个流,可作为下一个getline()的第一个参数
// isstringstream用法:
//getline(ss,temp,‘,’)表示每次从ss里读取,遇‘,’就停止,每次存到temp里面

#include <iostream>
#include <vector>
#include <sstream>
#include <algorithm>
using namespace std;

void Print(const vector<string>& str){
    for(int i = 0; i < str.size() - 1; i++){
        cout << str[i] << ',';
    }
    cout << str[str.size() - 1] << endl;
}
int main(){
    string s1;
    vector<string> str;
    string s2;
    while(getline(cin, s1)){
        istringstream ss(s1);
        while(getline(ss, s2, ',')){
            str.push_back(s2);
        }
        sort(str.begin(), str.end());
        Print(str);
        str.clear();
    }
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时是石头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值