笔试IO处理收集

场景1.求a+b的和

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

输入3行,其中第一行为N,其他两行为数据

int main(){
    int N;
    cin >> N;
    
    vector<int> A;
    vector<int> B;
    
    int x;
    while(cin>>x)
    {
        A.push_back(x);
        if(getchar()=='\n')
            break;
    }
    
    while(cin>>x)
    {
        B.push_back(x);
        if(getchar()=='\n')
            break;
    }
    return 0;
}


场景2.给出N阶方阵所有的数,求方阵中所有数的和

Input:
3
1 2 3
2 1 3
3 2 1
Output:
18
#include <iostream>
#include <cstdio>

using namespace std;

int main(){
    //freopen("1.in","r",stdin);
    int n,ans = 0;
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            int x; scanf("%d",&x);
            ans += x;
        }
    }
    cout << ans << endl;
    return 0;
}

3.输入字符串

#include<iostream>
using namespace std;
int main()
{
    char buf[ 255 ];
    while(cin.getline( buf, 255 ));
}

getline

用法

string s;
getline(cin,s);

getline会忽略行末的空格


4.输入不说明有多少组数据,但以某个特殊输入为结束标志

#include<iostream>
using namespace std;
int main()
{
    int a ,b;
    while(cin>>a>>b&&(a||b))
    {cout<<a+b<<endl;}
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值