美文网首页
牛客C++输入输出总结

牛客C++输入输出总结

作者: 锦绣拾年 | 来源:发表于2021-09-04 16:51 被阅读0次

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<iostream>
#include<cstdio>
using namespace std;
int main(){
    int a,b;
    while(cin>>a>>b){
        cout<<a+b<<endl;
    }

    return 0;
}

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<iostream>
#include<cstdio>
using namespace std;
int main(){
    int nums;
    cin>>nums;
    for(int i=0;i<nums;i++){
        int a,b;
        cin>>a>>b;
        cout<<a+b<<endl;
    }
    
    return 0;
}

3.

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

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<iostream>
#include<cstdio>
using namespace std;
int main(){
    int nums;
    cin>>nums;
    while(nums!=0){
        int addres=0;
        int tmp;
        for(int i=0;i<nums;i++){
            cin>>tmp;
            addres+=tmp;
        }
        cout<<addres<<endl;
        cin>>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<iostream>
#include<cstdio>
using namespace std;
int main(){
    int nums;
    cin>>nums;
    for(int i=0;i<nums;i++){
        int jlen;
        cin>>jlen;
        int addres=0;
        int tmp;
        for(int j=0;j<jlen;j++){
            cin>>tmp;
            addres+=tmp;
        }
        cout<<addres<<endl;
    }
    
    return 0;
}

6.循环输入案例

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

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<iostream>
using namespace std;
 
int main()
{
    int temp,sum;
      
    while(cin>>temp)
    {  
        sum+=temp;
        if(cin.get()=='\n')////cin.get()是保留回车在输入流队列中的,可以识别空行和回车,正常输入后一定会有空格和回车
        {
            cout<<sum<<endl;//它必须在每行的循环体中,每行输出一个;输出条件为'\n'
            sum=0;//需要放在输出后面,防止多行加在一起
        }
          
    }
 
}

2)使用getline(cin,s),

stringstream 再输出

需要加入头 #include<bits/stdc++.h>

#include<iostream>
#include<bits/stdc++.h>
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<<sum<<endl;
    }
    
    return 0;
}

8.字符串

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

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

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

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

输出例子1:
a bb c d e
#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;

int main()
{
    int n;
    cin >> n;
    string s;
    vector<string>res;
    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<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    string s;
    vector<string> 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<tmp.size();i++){
                    cout<<tmp[i]<<" ";
                }
                cout<<endl;
                tmp.clear();
            }
        }
    }
    
    return 0;
}

10.逗号分割字符串


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

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

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

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

↓我的做法

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
    vector<string> tmp;
    string s;
    int begin=0;
    int len=0;
    while(getline(cin,s)){
        for(int i=0;i<s.length();i++){
            if(s[i]==','){
                tmp.push_back(s.substr(begin,len));
                begin=i+1;
                len=0;
            }else{
                len+=1;
            }
        }
        tmp.push_back(s.substr(begin,len));
        sort(tmp.begin(),tmp.end());
        for(int i=0;i<tmp.size()-1;i++){
            cout<<tmp[i]<<",";
        }
        cout<<tmp[tmp.size()-1]<<endl;
        tmp.clear();
        begin=0;
        len=0;
    }
    
    
    return 0;
}

看起来stringstream会更方便

#include <bits/stdc++.h>
using namespace std;

int main()
{
    string s;
    while(getline(cin, s)){
        stringstream str(s);
        string temp;
        vector<string> res;
        while(getline(str, temp,',')){
            res.push_back(temp);
        }
        sort(res.begin(), res.end());
        for(int i=0; i<res.size(); i++){
            if(i==res.size()-1){
                cout<<res[i]<<endl;
            }
            else{
                cout<<res[i]<<',';
            }
        }
    }
    return 0;
}

相关文章

  • 牛客C++输入输出总结

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

  • 输入输出函数总结

    1. C++中各种输入输出函数总结 与输入输出流操作相关的类关系 2. 键盘输入输出函数 2.1. scanf函数...

  • c++ 常用STL整理

    最近在练习C++编程,做了一些牛客和力扣上面的题目,发现常用的C++ STL 有以下几种,对此进行简要总结,以便自...

  • 牛客Python输入输出

    1、基础编码结构 2、获得数据的内容 3、将数据内容转成List Python ACM模式输入输出样例题目来自牛客...

  • c++输入输出,堆空间,inline

    c++输入输出 c++输入输出类包含在iostream头文件的namespace std命名空间中 count输出...

  • C++资源库(实例)

    C++ 实例 C++ 实例 - 输出 "Hello, World!" C++ 实例 - 标准输入输出 C++ 实例...

  • 牛客网处理输入输出

    //单行示例 while(line=readline()){ var lines = line.split('...

  • 浅谈C++常用输入输出

    浅谈C++常用输入输出 在编写C++程序的时候,经常因为输入输出头疼,所以在这里做一个小结,记录一下常用的输入输出...

  • C++ 输入输出总结

    C++支持两种I/O,第一种是从C语言继承来的,一种是由C++定义的面向对象I/O系统。 从C继承来的I/O操作 ...

  • C++输入输出流的控制符

    C++输入输出流的控制符

网友评论

      本文标题:牛客C++输入输出总结

      本文链接:https://www.haomeiwen.com/subject/pxbrwltx.html