耪痖荩客網(wǎng)輸入輸出練習

抛粮颍客網(wǎng)采用的是從標準輸入(鍵盤)讀取數(shù)據(jù)蜓堕,標準輸出(屏幕)數(shù)據(jù)

講解見牛客網(wǎng)輸入輸出理解博其,使用ie瀏覽器打開可以避免安裝flash套才,直接播放

練習題見牛客OJ輸入輸出練習

目錄

輸入輸出練習題目目錄

A+B(1)

A+B(1)
#include <iostream>
using namespace std;

int main(){
    int a,b;
    while(cin>>a>>b)
        cout<<a+b<<endl;
    return 0;
}

A+B(2)

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

A+B(3)

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

A+B(4)

A+B(4)
#include <iostream>
using namespace std;

int main(){
    int n,a,num=0;
    while(cin>>n){
        if(n==0) 
            return 0;
        for(int i=0;i<n;i++){
            cin>>a; // 把每個數(shù)字輸入理解為慕淡,輸入一個數(shù)字后背伴,按一次回車,從而讀到一個數(shù)字
            num+=a;
        }
        cout<<num<<endl; //注意要輸出換行符7逅琛I导拧!
        num=0;
    }
    return 0;
}

A+B(5)

A+B(5)
#include <iostream>
using namespace std;
int main(){
    int t,n,num=0,temp;
    cin>>t;
    while(t--){//使用while控制讀的循環(huán)
        cin>>n;
        for(int i=0;i<n;i++){
            cin>>temp;
            num+=temp;
        }
        cout<<num<<endl;
        num=0;
    }
    return 0;
}

A+B(6)

A+B(6)
#include <iostream>
using namespace std;

int main(){
    int n,temp,num=0;
    while(cin>>n){ // 通過while(cin>>n)讀入空行時自動退出
        for(int i=0;i<n;i++){
            cin>>temp;
            num+=temp;
        }
        cout<<num<<endl;
        num=0;
    }
    
    return 0;
}

A+B(7)

A+B(7)
#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)

字符串排序(1)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

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

字符串排序(2)

字符串排序(2)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;

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

字符串排序(3)

字符串排序(3)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
using namespace std;

int main(){
    string s,str;
    vector<string> strs;
    while(getline(cin,s)){
        stringstream ss(s);
        while(getline(ss,str,','))
            strs.push_back(str);
        sort(strs.begin(),strs.end());
        if(strs.size()>1){ // 最后一個不會輸出','
            for(int i=0;i<strs.size()-1;i++)
                cout<<strs[i]<<',';
            cout<<strs[strs.size()-1]<<endl;
        }
        else
            cout<<strs[0]<<endl;
        strs.clear(); // 與之前每行的num清0一樣
    }
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末儿普,一起剝皮案震驚了整個濱河市崎逃,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌眉孩,老刑警劉巖个绍,帶你破解...
    沈念sama閱讀 219,366評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異浪汪,居然都是意外死亡巴柿,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,521評論 3 395
  • 文/潘曉璐 我一進店門死遭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來广恢,“玉大人,你說我怎么就攤上這事呀潭《っ裕” “怎么了?”我有些...
    開封第一講書人閱讀 165,689評論 0 356
  • 文/不壞的土叔 我叫張陵钠署,是天一觀的道長糠聪。 經(jīng)常有香客問我,道長谐鼎,這世上最難降的妖魔是什么舰蟆? 我笑而不...
    開封第一講書人閱讀 58,925評論 1 295
  • 正文 為了忘掉前任,我火速辦了婚禮狸棍,結(jié)果婚禮上身害,老公的妹妹穿的比我還像新娘。我一直安慰自己草戈,他們只是感情好塌鸯,可當我...
    茶點故事閱讀 67,942評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著唐片,像睡著了一般丙猬。 火紅的嫁衣襯著肌膚如雪丢习。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,727評論 1 305
  • 那天淮悼,我揣著相機與錄音,去河邊找鬼揽思。 笑死袜腥,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的钉汗。 我是一名探鬼主播羹令,決...
    沈念sama閱讀 40,447評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼损痰!你這毒婦竟也來了福侈?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,349評論 0 276
  • 序言:老撾萬榮一對情侶失蹤卢未,失蹤者是張志新(化名)和其女友劉穎肪凛,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體辽社,經(jīng)...
    沈念sama閱讀 45,820評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡伟墙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,990評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了滴铅。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片戳葵。...
    茶點故事閱讀 40,127評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖汉匙,靈堂內(nèi)的尸體忽然破棺而出拱烁,到底是詐尸還是另有隱情,我是刑警寧澤噩翠,帶...
    沈念sama閱讀 35,812評論 5 346
  • 正文 年R本政府宣布戏自,位于F島的核電站,受9級特大地震影響绎秒,放射性物質(zhì)發(fā)生泄漏浦妄。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,471評論 3 331
  • 文/蒙蒙 一见芹、第九天 我趴在偏房一處隱蔽的房頂上張望剂娄。 院中可真熱鬧,春花似錦玄呛、人聲如沸阅懦。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,017評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽耳胎。三九已至惯吕,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間怕午,已是汗流浹背废登。 一陣腳步聲響...
    開封第一講書人閱讀 33,142評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留郁惜,地道東北人堡距。 一個月前我還...
    沈念sama閱讀 48,388評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像兆蕉,于是被迫代替她去往敵國和親羽戒。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,066評論 2 355