1. 程式人生 > 實用技巧 >牛客OJ程式設計-輸入輸出

牛客OJ程式設計-輸入輸出

------------恢復內容開始------------

連結:https://ac.nowcoder.com/acm/contest/5647/A
來源:牛客網

輸入描述:

輸入包括兩個正整數a,b(1 <= a, b <= 10^9),輸入資料包括多組。

輸出描述:

輸出a+b的結果
示例1

輸入

複製
1 5
10 20

輸出

複製
6
30


#include<iostream>
using namespace std;

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

int main(){
    int a,b;
    while(scanf("%d %d", &a ,&b)!=EOF){
        printf("%d\n",a+b);
    }
    return 0;
}

  

輸入描述:

輸入第一行包括一個數據組數t(1 <= t <= 100)
接下來每行包括兩個正整數a,b(1 <= a, b <= 10^9)

輸出描述:

輸出a+b的結果
示例1

輸入

複製
2
1 5
10 20

輸出

複製
6
30


#include<iostream>
using namespace std;

int main(){
    int t,a,b;
    cin>>t;
    while(t>0)
    {
       while(t--)
       {
           cin>>a>>b;
           cout<<a+b<<endl;
       }
    }
    return 0;
}
#include<iostream>
using namespace
std; int main() { int t,a,b; scanf("%d",&t); for(int i=0;i<t;i++) { scanf("%d %d",&a,&b); printf("%d\n",a+b); } return 0; }


連結:https://ac.nowcoder.com/acm/contest/5647/C
來源:牛客網

輸入描述:

輸入包括兩個正整數a,b(1 <= a, b <= 10^9),輸入資料有多組, 如果輸入為0 0則結束輸入

輸出描述:

輸出a+b的結果
示例1

輸入

1 5
10 20
0 0

輸出

6
30
#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;
}
#include<iostream>
using namespace std;

int main()
{
    int a,b;
    while(scanf("%d %d",&a,&b)){
        if(a==0 && b==0) break;
        printf("%d\n",a+b);
    }
    
    return 0;
}

連結:https://ac.nowcoder.com/acm/contest/5647/D
來源:牛客網

輸入描述:

輸入資料包括多組。
每組資料一行,每行的第一個整數為整數的個數n(1 <= n <= 100), n為0的時候結束輸入。
接下來n個正整數,即需要求和的每個正整數。

輸出描述:

每組資料輸出求和的結果
示例1

輸入

4 1 2 3 4
5 1 2 3 4 5
0

輸出

10
15
#include<iostream>
using namespace std;

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

int main()
{
    int n;
    while(scanf("%d",&n) != EOF)
    {
        if(n==0) break;
        int m,sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&m);
            sum +=m;
        }
        printf("%d\n",sum);
    }
    return 0;
}


連結:https://ac.nowcoder.com/acm/contest/5647/E
來源:牛客網

輸入描述:

輸入的第一行包括一個正整數t(1 <= t <= 100), 表示資料組數。
接下來t行, 每行一組資料。
每行的第一個整數為整數的個數n(1 <= n <= 100)。
接下來n個正整數, 即需要求和的每個正整數。

輸出描述:

每組資料輸出求和的結果
示例1

輸入

2
4 1 2 3 4
5 1 2 3 4 5

輸出

10
15
#include<iostream>
using namespace std;

int main()
{
    int t;
    cin>>t;
    while(t>0 && t<=100)
    {
        int n;
        cin>>n;
        int a,sum=0;
        for(int i=0;i<n;i++)
        {
            cin>>a;
            sum +=a;
        }
        t--;
        cout<<sum<<endl;
    }
    return 0;
}

#include<iostream>
using namespace std;

int main()
{
    int t;
    scanf("%d",&t);
    while(t>0 && t<=100)
    {
        int n,a,sum=0;
        scanf("%d",&n);
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a);
            sum +=a;
        }
        printf("%d\n",sum);
        t--;
    }
    return 0;
}














輸入描述:

輸入資料有多組, 每行表示一組輸入資料。
每行的第一個整數為整數的個數n(1 <= n <= 100)。
接下來n個正整數, 即需要求和的每個正整數。

輸出描述:

每組資料輸出求和的結果
示例1

輸入

4 1 2 3 4
5 1 2 3 4 5

輸出

10
15
#include<iostream>
using namespace std;

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

int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        int a,sum=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a);
            sum +=a;
        }
        printf("%d\n",sum);
    }
    return 0;
}


連結:https://ac.nowcoder.com/acm/contest/5647/G
來源:牛客網

輸入描述:

輸入資料有多組, 每行表示一組輸入資料。

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

輸出描述:

每組資料輸出求和的結果
示例1

輸入

1 2 3
4 5
0 0 0 0 0

輸出

6
9
0
#include<iostream>
using namespace std;

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

int main(){
    int n,sum=0;
    while(scanf("%d",&n)!=EOF)
    {
        sum +=n;
        if(getchar()=='\n')
        {
            printf("%d\n",sum);
            sum=0;
        }
    }
}


連結:https://ac.nowcoder.com/acm/contest/5647/H
來源:牛客網

輸入描述:

輸入有兩行,第一行n

第二行是n個空格隔開的字串

輸出描述:

輸出一行排序後的字串,空格隔開,無結尾空格
示例1

輸入

5
c d a bb e

輸出

a bb c d e
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

int main(){
    int n;
    cin>>n;
    string s;
    vector<string>strs;
    while(cin>>s)
    {
        strs.push_back(s);
        
        if(cin.get()=='\n')
        {
            sort(strs.begin(),strs.end());
            for(int i=0;i<strs.size()-1;i++)
            {
                cout<<strs[i]<<' ';
            }
            cout<<strs[strs.size()-1]<<endl;
            strs.clear();
        }
    }
    
}

連結:https://ac.nowcoder.com/acm/contest/5647/I
來源:牛客網

輸入描述:

多個測試用例,每個測試用例一行。

每行通過空格隔開,有n個字元,n<100

輸出描述:

對於每組測試用例,輸出一行排序過的字串,每個字串通過空格隔開
示例1

輸入

a c bb
f dddd
nowcoder

輸出

a bb c
dddd f
nowcoder
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

int main(){
    string s;
    vector<string> str;
    while(cin>>s)
    {
        str.push_back(s);
        if(cin.get()=='\n')
        {
            sort(str.begin(),str.end());
            for(int i=0;i<str.size();i++)
            {
                cout<<str[i]<<' ';
            }
            cout<<endl;
            str.clear();
        }
    }
    return 0;
}

連結:https://ac.nowcoder.com/acm/contest/5647/J
來源:牛客網

輸入描述:

多個測試用例,每個測試用例一行。
每行通過,隔開,有n個字元,n<100

輸出描述:

對於每組用例輸出一行排序後的字串,用','隔開,無結尾空格
示例1

輸入

a,c,bb
f,dddd
nowcoder

輸出

a,bb,c
dddd,f
nowcoder

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;
int main()
{
    string s;
    while(getline(cin,s,'\n'))
    {
        string temp;
        vector<string> res;
        for(int i=0;i<s.size();i++)
        {
            if(s[i]!=',')
            {
                temp.push_back(s[i]);
            }
            else{
                res.push_back(temp);
                temp.clear();
            }
        }
        res.push_back(temp);
        sort(res.begin(), res.end());
        for(int i=0;i<res.size();i++)
        {
            cout<<res[i];
            if(i==res.size()-1) {
                cout<<endl;
            }
            else {
                cout<<',';
            }
        }
        
    }
    return 0;
}