2020-08-23

第二周周报

牛客网输入输出总结

每年互联网公司的招聘都必不可少会有算法题,这里面用的比较多的刷题网站是leetcode和牛客网。leetcode是一个专门用来刷算法题的网站,里面的算法题覆盖面很广,但牛客网经常要自己输入输出,有时候会发现这里面也有一点点坑,会让自己不能专心把问题放在要处理的算法上面,所以应该平时多多练习。

数字输入

1.输入单个数字
输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据包括多组。
1 5
10 20

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

3.输入包括两个正整数a,b(1 <= a, b <= 10^9),输入数据有多组, 如果输入为0 0则结束输入

1 5
10 20
0 0

#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.输入数据包括多组。
每组数据一行,每行的第一个整数为整数的个数n(1 <= n <= 100), n为0的时候结束输入。
接下来n个正整数,即需要求和的每个正整数。
4 1 2 3 4
5 1 2 3 4 5
0

#include<iostream>
using namespace std;
int main()
{
    int n,sum;
    while(cin>>n)
    {
        if(n==0)
        {break;}
        else
        {
            for(int i=0;i<n;i++)
            {int temp=0;
            cin>>temp;
            sum+=temp;}
            cout<<sum<<endl;
            sum=0;
        }
    }
    return 0;
}

5.输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
4 1 2 3 4
5 1 2 3 4 5

#include<iostream>
using namespace std;
int main()
{
    int m,sum;
    while(cin>>m)
    {
        for(int i=0;i<m;i++)
        {
            int temp;
            cin>>temp;
            sum+=temp;
        }
        cout<<sum<<endl;
        sum=0;
    }
    
    
    
    return 0;
}

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

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

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

字符串输入

1.输入有两行,第一行n

第二行是n个空格隔开的字符串
5
c d a bb e

#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
int main()
{
    int n;
    cin>>n;
    vector<string>a;
    string temp;
    for(int i=0;i<n;i++)
    {cin>>temp;
    a.push_back(temp);
    }
    sort(a.begin(),a.end());
    for(int i=0;i<n;i++)
    {
        cout<<a[i]<<" ";
    }
    return 0;
}

2.对输入的字符串进行排序后输出
a c bb
f dddd
nowcoder

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
    string temp;
    vector<string>a;
    while(cin>>temp)
    {
        a.push_back(temp);
        if(cin.get()=='\n')
        {
            sort(a.begin(),a.end());
            for(int i=0;i<a.size();i++)
            {
                cout<<a[i]<<" ";
            }
            cout<<endl;
            a.clear();
        }
    }
    
 return 0; 
}

3.多个测试用例,每个测试用例一行。
每行通过,隔开,有n个字符,n<100
对于每组用例输出一行排序后的字符串,用’,'隔开,无结尾空格
a,c,bb
f,dddd
nowcoder

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

输出

C++的输出cout与输入cin一样都是用流来控制的,cin和cout都在iostream这个头文件中,命名空间为std,因此使用的时候都要加上头文件和命名空间。输出相对来说简单一些,不会出现输入的一些问题,相反,cout输出有时候还可以帮助你解决题目,一下列举一些常见输出语句:

/输入一个数字再输出
int num;//定义
cin>>num;//输入
cout<<num<<endl;//输出并且换行

//输入一个字符串再输出
string input;//定义
cin>>input;//输入
cout<<input<<endl;//输出并且换行

//输入输出多个数据
int num1,num2;
string s1,s2;
cin>>num1>>s1;
cin>>num2>>s2;
cout<<num1<<s1<<' '<<num2<<s2<<endl;

总之都是一些小细节,可以平时多练习,这样笔试的时候不至于忙中出错。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值