面向牛客的输入输出总结


输入输出典型题链接

int类

1(典型)

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int a,b;
    while(cin>>a>>b)
    {
        cout<<a+b<<endl;
    }
}

2(典型)

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int a,b;
        cin>>a>>b;
        cout<<a+b<<endl;
    }
    return 0;
}

3(典型)

在这里插入图片描述

#include<bits/stdc++.h>
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<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    while(cin>>t)
    {
        if(t==0)break;
        int res=0;
        while(t--)
        {
            int x;
            cin>>x;
            res+=x;
        }
        cout<<res<<endl;
    }
    return 0;
}

5

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int x;
        cin>>x;
        int res=0;
        while(x--)
        {
            int y;
            cin>>y;
            res+=y;
        }
        cout<<res<<endl;
    }
    return 0;
}

6

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int x;
    while(cin>>x)
    {
        int res=0;
        while(x--)
        {
            int y;
            cin>>y;
            res+=y;
        }
        cout<<res<<endl;
    }
    return 0;
}

7

在这里插入图片描述

//方法1
/*
cin.get()可以接受单个字符(包括回车空格)
注意:循环后要先用x,因为调用cin.get()时就读入下一个输入了
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int x;
    int res=0;
    while(cin>>x)
    {   
        res+=x;
        if(cin.get()=='\n') 
        {
            cout<<res<<endl;
            res=0;
        }        
    }
}
//方法2 getline 提取整行 也可以用stringstream和getline结合使用 见string类第三题
/*
#include<bits/stdc++.h>
using namespace std;
int main()
{
    string str;
    while(getline(cin,str))
    {
        int res=0;
        for(int i=0;i<str.size();i++)
        {
            if(str[i]>='0'&&str[i]<='9')
                res+=str[i]-'0'; //注意:不能直接int(),这样转化过来的时asii码
        }
        cout<<res<<endl;
    }
    return 0;
}
*/

string

1

在这里插入图片描述

/*
cin读到空格
*/
#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    set<string> res;
    while(n--)
    {
        string x;
        cin>>x;
        res.insert(x);
    }
    int i=0;
    for(auto elem:res)
    {
        i++;
        cout<<elem;
        if(i==n-1) cout<<endl;
        else cout<<" ";
    }
    return 0;
}

2(重点)

在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string x;
    set<string> res;
    while(cin>>x)
    {
        res.insert(x);
        if(cin.get()=='\n')
        {
            int i=0;
            for(auto elem:res)
            {
                i++;
                cout<<elem;
                if(i==res.size()) 
                {
                    res.clear();
                    cout<<endl;
                }
                else cout<<" ";
            }
        }
    }
    return 0;
}

3(重点)

在这里插入图片描述

/*
stringstream与getline连用实现自定义的split(间隔#间隔,等等)
链接:https://blog.csdn.net/qq_36743440/article/details/91999615
*/
#include <bits/stdc++.h>
using namespace std;
int main(){
    string input;
    while(getline(cin, input)){
        stringstream iss(input);
        string temp;
        vector<string> vs;
        while(getline(iss, temp, ',')){
            vs.push_back(temp);
        }
        sort(vs.begin(), vs.end());
        int len = vs.size();
        for(int i = 0; i < len - 1; i++){
            cout << vs[i] << ",";
        }
        cout << vs[len - 1] << endl;
    }
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值