??虲++輸入輸出總結(jié)

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

C++輸入輸出

字符串和數(shù)字轉(zhuǎn)換

https://zhuanlan.zhihu.com/p/188390082

atoi(str)
stoi(str)
to_string(int)
C++如何逆序排序呢?
1.先reverse
2.寫新的函數(shù)
3.end,begin??

1.循環(huán)輸入兩個數(shù)相加

輸入描述:
輸入包括兩個正整數(shù)a,b(1 <= a, b <= 10^9),輸入數(shù)據(jù)包括多組。
輸出描述:
輸出a+b的結(jié)果
輸入例子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.先輸入案例數(shù),再輸入案例

計算a+b

打開以下鏈接可以查看正確的代碼

輸入描述:
輸入第一行包括一個數(shù)據(jù)組數(shù)t(1 <= t <= 100)
接下來每行包括兩個正整數(shù)a,b(1 <= a, b <= 10^9)
輸出描述:
輸出a+b的結(jié)果
輸入例子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.

輸入描述:
輸入包括兩個正整數(shù)a,b(1 <= a, b <= 10^9),輸入數(shù)據(jù)有多組, 如果輸入為0 0則結(jié)束輸入
輸出描述:
輸出a+b的結(jié)果
輸入例子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.

輸入描述:
輸入數(shù)據(jù)包括多組。
每組數(shù)據(jù)一行,每行的第一個整數(shù)為整數(shù)的個數(shù)n(1 <= n <= 100), n為0的時候結(jié)束輸入。
接下來n個正整數(shù),即需要求和的每個正整數(shù)。
輸出描述:
每組數(shù)據(jù)輸出求和的結(jié)果
輸入例子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.

輸入描述:
輸入的第一行包括一個正整數(shù)t(1 <= t <= 100), 表示數(shù)據(jù)組數(shù)。
接下來t行, 每行一組數(shù)據(jù)。
每行的第一個整數(shù)為整數(shù)的個數(shù)n(1 <= n <= 100)。
接下來n個正整數(shù), 即需要求和的每個正整數(shù)。
輸出描述:
每組數(shù)據(jù)輸出求和的結(jié)果
輸入例子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.循環(huán)輸入案例

輸入描述:
輸入數(shù)據(jù)有多組, 每行表示一組輸入數(shù)據(jù)。
每行的第一個整數(shù)為整數(shù)的個數(shù)n(1 <= n <= 100)。
接下來n個正整數(shù), 即需要求和的每個正整數(shù)。
輸出描述:
每組數(shù)據(jù)輸出求和的結(jié)果
輸入例子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.完全不告訴有多少個案例,靠空行

輸入描述:
輸入數(shù)據(jù)有多組, 每行表示一組輸入數(shù)據(jù)。

每行不定有n個整數(shù),空格隔開。(1 <= n <= 100)。

輸出描述:
每組數(shù)據(jù)輸出求和的結(jié)果

輸入例子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;//它必須在每行的循環(huán)體中,每行輸出一個;輸出條件為'\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個空格隔開的字符串

輸出描述:
輸出一行排序后的字符串,空格隔開,無結(jié)尾空格

輸入例子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的內(nèi)容,而是下一個內(nèi)容。

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

輸出描述:
對于每組用例輸出一行排序后的字符串,用','隔開,無結(jié)尾空格

輸入例子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;
}
最后編輯于
?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容