抛粮颍客網(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;
}