快速上手ACM,常见输入输出的写法

istream& getline (istream& is, string& str, char delim);

istream& getline (istream& is, string& str);

is是一个流,例如cin

str是一个string类型的引用,读入的字符串将直接保存在str里面

delim是结束标志,默认为换行符

1、A+B

 代码实现

#include<iostream>
using namespace std;
int main(){
    int a, b;
    while(cin>>a>>b){ // 注意while处理多个case
        cout<<a+b<<endl;
    }
    return 0;
}

2、A+B

 

代码实现

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

3、A+B

 

代码实现

#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、A+B

 

 代码实现

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

5、A+B

 

 代码实现

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

6、A+B

 代码实现

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

7、A+B

 

代码实现

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

8、字符串排序

代码实现

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main(){
    int n;
    cin>>n;
    vector<string> vec;
    while(n--){
        string s;
        cin>>s;
        vec.push_back(s);
    }
    sort(vec.begin(),vec.end());
    for(auto c:vec){
        cout<<c<<' ';
    }
}

9、字符串排序

 

代码实现

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

10、字符串排序

 

代码实现

#include<bits/stdc++.h>
using namespace std;
int main(){
    
    string s,t;
    while(cin>>s){
        stringstream sstream(s);   // 记录一行的输入流,为后序做准备
        vector<string> vec;
        while(getline(sstream, t, ',')){
            vec.push_back(t);
        }
        sort(vec.begin(), vec.end());
        for(int i=0;i<vec.size()-1;++i){
            cout<<vec[i]<<',';
        }
        cout<<vec.back()<<endl;   
    }
    return 0;
}

ACM笔试的输入

#pragma once
#include <vector>
#include <map>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <iostream>
#include <stack>
#include <queue>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <memory>

using namespace std;

/*
* 去空格输入至vec
*
* 输入:"1 2 3 4 5"
* 输出:vector<int>
*/
vector<int> inputToVecInt() {
	string line, word;
	getline(cin, line);

	stringstream sin(line);

	vector<int> ret;
	while (sin >> word) {
		ret.push_back(stoi(word));
	}
	return ret;
}


/*
* 去空格输入至vec
*
* 输入:"ab bc cd de e"
* 输出:vector<string>
*/
vector<string> inputToVecString() {
	string line, word;
	getline(cin, line);
	stringstream sin(line);
	vector<string> ret;
	while (sin >> word) {
		ret.push_back(word);
	}
	return ret;
}

/* 输入 
4
1 2
3 4
5 6
7 8
*/

vector<int> getVecInt() {
    vector<int> ret;
    string line, word;
    getline(cin, line);
    stringstream sin(line);
    while (sin >> word) {
        ret.push_back(stoi(word));
    }
    return ret;
}
int main() {
    int n;
    cin >> n;
    getchar();

    vector<vector<int>> areas;

    for (int i = 0; i != n; ++i) {
        areas.emplace_back(getVecInt());
    }
    cout << Solution().solve(areas) << endl;
    return 0;
}
// 如果是分别取出每一行,main函数有变化
int main() {
    int n;
    cin >> n;
    getchar();

    vector<vector<int>> areas;

    for (int i = 0; i != n; ++i) {
        areas.emplace_back(getVecInt());
    }

    for(const auto& area:areas){
        cout << Solution().solve(area) << endl;
    }

    return 0;
}



/* 输入两个字符串 
第一行为数据组数,接下来的每组数据中的第一行为字符串s,第二行为字符串t
4
hello
hello
hello
helloo
hello
hlllloo
hello
helo
*/

int main() {
    int n;
    cin >> n;
    getchar();
    vector<pair<string, string>> groups;
    for (int i = 0; i != n; ++i) {
       string s, t;
       getline(cin, s);
       getline(cin, t);
       groups.push_back({ s,t });
    }
for (const auto& pair : groups) {
     int flag = Solution().isLongName(pair.first, pair.second);
     if (flag) {
       cout << "YES";
     }
     else {
       cout << "NO";
     }
 }
 return 0;
}

// 输入 [1 ,2, 3, 4, 5]
vector<int> getVecInt() {
    vector<int> ret;
    string line, word;
    getline(cin, line);

    string subLine = line.substr(1, line.size()-2);

    stringstream sin(subLine);
    while (getline(sin, word, ',')) {
        ret.push_back(stoi(word));
    }
    return ret;
}


// 输入 [[1,2,3],[4,5,6],[7,8,9]]
vector<vector<int>> inputTwoMatrix(){
	string line, word;
	getline(cin, line);
	string subLine = line.substr(1, line.size() - 2);
	stringstream sin1(subLine);
	vector<vector<int>> map;
	int count = 0;
	while (getline(sin1, word, ']')) {
		string subWord;
		if (count == 0) {
			subWord = word.substr(1, word.size() - 1);
		}
		else {
			subWord = word.substr(2, word.size() - 2);
		}
		stringstream sin2(subWord);
		vector<int> vec;
		while (getline(sin2, word, ',')) {
			vec.push_back(stoi(word));
		}
		map.push_back(vec);
		++count;
	}
	return map;
}

ACM笔试的输出

/*
* 从vec输出数字+空格
*
* 输入:vector<int>
* 输出:"1 2 3 4 5"
*/
void printVecWithSpace(const vector<int>& vec) {
	for (int i = 0; i != vec.size(); ++i) {
		cout << vec[i];
		if (i != vec.size() - 1) cout << " ";
	}
	cout << endl;
}


/*
* 从vec输出字符串+空格
*
* 输入:vector<string>
* 输出:"ab bc cd de e"
*/
void printVecWithSpace(const vector<string>& vec) {
	for (int i = 0; i != vec.size(); ++i) {
		cout << vec[i];
		if (i != vec.size() - 1) cout << " ";
	}
	cout << endl;
}

猜你喜欢

转载自blog.csdn.net/weixin_47887421/article/details/126236181