輸入輸出
cin cout getline stringstream fixed<<setprecision(n)
cin輸入 cout輸出
/*
* zerojudge
* a001. 哈囉
* C++
* AC (3ms, 320KB)
*/
#include <bits/stdc++.h>
using namespace std;
int main(){
string s;
cin >> s;
cout << "hello, " << s << endl;
return 0;
}
cin EOF
/*
* zerojudge
* a004. 文文的求婚
* C++
* AC (8ms, 312KB)
*/
#include <bits/stdc++.h>
using namespace std;
int main(){
int n;
while(cin >> n){
if( (n%4==0 && n%100!=0) || n%400==0){
cout << "閏年" << endl;
}else{
cout << "平年" << endl;
}
}
return 0;
}
getline 整行輸入
/*
* zerojudge
* a011. 00494 - Kindergarten Counting Game
* C++
* AC (2ms, 312KB)
*/
#include<bits/stdc++.h>
using namespace std;
bool f(char c){
if( c<='z' && c>='a' || c<='Z' && c>='A' ){
return 1;
}else{
return 0;
}
}
int main(){
string s;
while(getline(cin,s)){
int sum=0;
int i=0;
bool isEn = false;
for(;i<s.size();i++){
if(isEn){
if(f(s[i])==0){
isEn=0;
}
}else{
if(f(s[i])==1){
sum+=1;
isEn=1;
}
}
}
cout << sum << endl;
}
}
stringsteam 把有空格字串當 cin
/*
* zerojudge
* e997. 升旗典禮抽背課文
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
string line;
getline(cin,line);
stringstream ss(line);
vector<string>v;
string x;
int cnt = 0;
while(ss >> x){
v.push_back(x);
cnt+=1;
}
int c;
cin >> c;
cout << v[cnt-c] << endl;
}
小數點控制
/*
* zerojudge
* a410. 解方程
* C++
* AC (2ms, 328KB)
*/
#include<bits/stdc++.h>
using namespace std;
int main(){
float a,b,c,d,e,f;
cin >> a >> b >> c >> d >> e >> f;
//
if(a*e==d*b){
if(a*f==c*d){
cout << "Too many\n";
}
else{
cout << "No answer\n";
}
}
else{
float x=(c*e-b*f)/(a*e-b*d);
float y=(c*d-a*f)/(b*d-a*e);
//
cout << "x=" << fixed << setprecision(2) << x << endl;
cout << "y=" << fixed << setprecision(2) << y << endl;
}
}
如果cin後面要接getline,記得要加cin.ignore();
cin >> input;
cin.ignore();
getline(cin,s);
Last updated
Was this helpful?