ACM模式 常见输入输出格式【数组】

第一类
用空格隔开,确定n组数据
用逗号隔开,依然可以使用此方法

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

输入数组,空格隔开
int main(){

    int n=3;
    vector<vector<int>> res(n);
    for(int i=0;i<n;i++){
        vector<int> tmp;
        int a;
        while(cin>>a){
            tmp.push_back(a);
            if(cin.get()=='\n')
                break;
        }
        if(tmp.size()!=0)
            res[i]=tmp;
    }
    //输出结果
    cout<<"输出结果"<<endl;
    for(int i=0;i<res.size();i++){
        cout<<"第 "<<i<<" 组  ";
        for(int j=0;j<res[i].size();j++){
            cout<<res[i][j]<<" ";
        }
        cout<<endl;
    }
	system("pause");
    return 0;
}

输入输出结果

//空格隔开
1 2 3 4 5
11 12 13 14 15
21 22 23 24 25
输出结果
第 01 2 3 4 5111 12 13 14 15221 22 23 24 25
进程已结束,退出代码为 0
//逗号隔开的
1,2,3,4,5,6,7
1 2 3 4
1,2 3 4,5,6,7
输出结果
第 01 2 3 4 5 6 711 2 3 421 2 3 4 5 6 7

第二类
输入一组数,用逗号‘ ,’隔开,确定n组数据

1,2,3,4,5,6,7
0,2,3,4,5,7,3

存入数组w,v

#include <bits/stdc++.h>
using namespace std;
int main(){

    vector<int> w;
    vector<int> v;

    string s1,s2;
    cin>>s1;
    cin>>s2;

    string tmp;
    stringstream ss;
    ss<<s1;

    while (getline(ss,tmp,',')){
        w.push_back(stoi(tmp));
    }
    ss.clear();
    ss<<s2;
    while(getline(ss,tmp,',')){
        v.push_back(stoi(tmp));
    }
    system("pause");
    return 0;
}

第三类
用空格隔开,不确定数据

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

//输入数组,空格隔开,不定多少行
int main(){

    vector<vector<int>> res;
    int a;
    vector<int> tmp;
    while(cin>>a){
        tmp.push_back(a);
        if(cin.get()=='\n'){
            if(tmp.size()!=0)
                res.push_back(tmp);
            tmp.clear();
        }
    }
    //输出结果
    cout<<"输出结果"<<endl;
    for(int i=0;i<res.size();i++){
        cout<<"第 "<<i<<" 组  ";
        for(int j=0;j<res[i].size();j++){
            cout<<res[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}

输入输出结果

//任意组数,任意长度
1 1 1 1
1 2 3 4 5 6 7 8 9 10
100 10
99
a
输出结果
第 01 1 1 111 2 3 4 5 6 7 8 9 102100 10399
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值