牛客C++输入输出总结

https://ac.nowcoder.com/acm/contest/5657#question

C++输入输出

字符串和数字转换

https://zhuanlan.zhihu.com/p/188390082

atoi(str)
stoi(str)
to_string(int)
C++如何逆序排序呢?
1.先reverse
2.写新的函数
3.end,begin??

1.循环输入两个数相加

输入描述:
输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组。
输出描述:
输出a+b的结果
输入例子1:
1 5
10 20
输出例子1:
6
30
#include
#include
using namespace std;
int main(){
    int a,b;
    while(cin>>a>>b){
        cout<

2.先输入案例数,再输入案例

计算a+b

打开以下链接可以查看正确的代码

输入描述:
输入第一行包括一个数据组数t(1 <= t <= 100)
接下来每行包括两个正整数a,b(1 <= a, b <= 10^9)
输出描述:
输出a+b的结果
输入例子1:
2
1 5
10 20
输出例子1:
6
30
#include
#include
using namespace std;
int main(){
    int nums;
    cin>>nums;
    for(int i=0;i>a>>b;
        cout<

3.

输入描述:
输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入
输出描述:
输出a+b的结果
输入例子1:
1 5
10 20
0 0
输出例子1:
6
30
#include
#include
using namespace std;
int main(){
    long a,b;
    while(1){
        cin>>a>>b;
        if(a+b==0)
            return 0;
        cout<

4.

输入描述:
输入数据包括多组。
每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
接下来n个正整数,即需要求和的每个正整数。
输出描述:
每组数据输出求和的结果
输入例子1:
4 1 2 3 4
5 1 2 3 4 5
0
输出例子1:
10
15
#include
#include
using namespace std;
int main(){
    int nums;
    cin>>nums;
    while(nums!=0){
        int addres=0;
        int tmp;
        for(int i=0;i>tmp;
            addres+=tmp;
        }
        cout<>nums;
    }
    
    return 0;
}

5.

输入描述:
输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。
接下来t行, 每行一组数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
输出描述:
每组数据输出求和的结果
输入例子1:
2
4 1 2 3 4
5 1 2 3 4 5
输出例子1:
10
15
#include
#include
using namespace std;
int main(){
    int nums;
    cin>>nums;
    for(int i=0;i>jlen;
        int addres=0;
        int tmp;
        for(int j=0;j>tmp;
            addres+=tmp;
        }
        cout<

6.循环输入案例

输入描述:
输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
输出描述:
每组数据输出求和的结果
输入例子1:
4 1 2 3 4
5 1 2 3 4 5
输出例子1:
10
15
#include
#include
using namespace std;
int main(){
    int a,b;
    while(cin>>a){
        int sumres=0;
        for(int i=0;i>b;
            sumres+=b;
        }
        cout<

7.完全不告诉有多少个案例,靠空行

输入描述:
输入数据有多组, 每行表示一组输入数据。

每行不定有n个整数,空格隔开。(1 <= n <= 100)。

输出描述:
每组数据输出求和的结果

输入例子1:
1 2 3
4 5
0 0 0 0 0

输出例子1:
6
9
0

1)通过cin.get=='\n'得到

cin.get()是保留回车在输入流队列中的,

而cin是丢弃回车的。

#include
using namespace std;
 
int main()
{
    int temp,sum;
      
    while(cin>>temp)
    {  
        sum+=temp;
        if(cin.get()=='\n')////cin.get()是保留回车在输入流队列中的,可以识别空行和回车,正常输入后一定会有空格和回车
        {
            cout<

2)使用getline(cin,s),

stringstream 再输出

需要加入头 #include

#include
#include
using namespace std;
int main(){
    
    string str;
    while(getline(cin,str)){
        stringstream s(str);
        int sum=0;
        int num=0;
        while(s>>num){
            sum+=num;
        }
        cout<

8.字符串

输入描述:
输入有两行,第一行n

第二行是n个空格隔开的字符串

输出描述:
输出一行排序后的字符串,空格隔开,无结尾空格

输入例子1:
5
c d a bb e

输出例子1:
a bb c d e
#include 
#include 
#include
using namespace std;

int main()
{
    int n;
    cin >> n;
    string s;
    vectorres;
    while (cin >> s)
    {
        res.push_back(s);

    }
    sort(res.begin(), res.end());
    for (int i = 0; i < n; i++)
    {
        cout << res[i] << ' ';
    }

}

9.

输入描述:
多个测试用例,每个测试用例一行。

每行通过空格隔开,有n个字符,n<100

输出描述:
对于每组测试用例,输出一行排序过的字符串,每个字符串通过空格隔开

输入例子1:
a c bb
f dddd
nowcoder

输出例子1:
a bb c
dddd f
nowcoder

cin.get()不是当前cin的内容,而是下一个内容。

cin.get()是保留回车在输入流队列中的,

而cin是丢弃回车的。

#include
#include
#include
#include
using namespace std;
int main(){
    string s;
    vector tmp;
    while(cin>>s){
        tmp.push_back(s);
        if(cin.get()=='\n'){
            if(tmp.size()>0){
                sort(tmp.begin(),tmp.end());
                for(int i=0;i

10.逗号分割字符串


输入描述:
多个测试用例,每个测试用例一行。
每行通过,隔开,有n个字符,n<100

输出描述:
对于每组用例输出一行排序后的字符串,用','隔开,无结尾空格

输入例子1:
a,c,bb
f,dddd
nowcoder

输出例子1:
a,bb,c
dddd,f
nowcoder

↓我的做法

#include
#include
#include
#include
using namespace std;
int main(){
    vector tmp;
    string s;
    int begin=0;
    int len=0;
    while(getline(cin,s)){
        for(int i=0;i

看起来stringstream会更方便

#include 
using namespace std;

int main()
{
    string s;
    while(getline(cin, s)){
        stringstream str(s);
        string temp;
        vector res;
        while(getline(str, temp,',')){
            res.push_back(temp);
        }
        sort(res.begin(), res.end());
        for(int i=0; i

你可能感兴趣的:(牛客C++输入输出总结)