笔试ACM模式下的输入输出联系(C++)
cin用法
#include
using namespace std;
int main(){
int a, b;
while(cin >> a >> b){
cout << a + b << endl;
}
return 0;
}
#include
using namespace std;
int main(){
int t, a, b;
cin >> t;
while(t--){ //for循环也可, for(int i = 0; i < t; ++i)
cin >> a >> b;
cout << a+ b << endl;
}
return 0;
}
#include
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
using namespace std;
int main(){
int n, num;
while(true){
int sum = 0;
cin >> n;
if(n == 0) {
//cout << n << endl;
break;
}
while(n--){
cin >> num;
sum += num;
}
cout << sum << endl;
}
return 0;
}
/*
#include
using namespace std;
int main(){
int n, num;
while(cin >> n){
if(n == 0) break;
int sum = 0;
while(n--){
cin >> num;
sum += num;
}
cout << sum << endl;
}
return 0;
}
*/
#include
using namespace std;
int main(){
int t, n, num;
cin >> t;
while(t--){
int sum = 0;
cin >> n;
while(n--){
cin >> num;
sum += num;
}
cout << sum << endl;
}
return 0;
}
#include
using namespace std;
int main(){
int n, num;
while(cin >> n){
int sum = 0;
while(n--){
cin >> num;
sum += num;
}
cout << sum << endl;
}
return 0;
}
#include
using namespace std;
int main(){
int num;
int sum = 0;
while(cin >> num){
sum += num;
if(cin.get() == '\n') {
cout << sum << endl;
sum = 0;
}
}
return 0;
}
//这里注意区分char与string
// 补充:strlen和sizeof的区别:
//sizeof将\0也计算在内,strlen不算在内,遇到\0就停止统计
#include
#include
#include
#include
using namespace std;
int main()
{
int N = 0;
cin >> N;
vector<string> str(N, "0");
for (int i = 0; i < N; i++)
cin >> str[i];
sort(str.begin(), str.end());
for (string s : str)
cout << s << " ";
}
/*
#include
#include
#include
#include
using namespace std;
void Print(const vector& str) {
for (int i = 0; i < str.size(); ++i) {
cout << str[i] << ' ';
}
//cout << str[str.size() - 1] << endl;
}
int main() {
int n;
cin >> n;
string a;
vector str;
while (n--) {
cin >> a;
str.push_back(a);
}
sort(str.begin(), str.end());
Print(str);
system("pause");
return 0;
}
*/
#include
#include
#include
#include
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(string i : str){
cout << i << ' ';
}
cout << endl;
str.clear();
}
}
return 0;
}
//getline()的三个参数
//第一个参数表示从哪里读取
//第二个参数表示存入哪里
//第三个参数表示分隔符
//getline(cin,str) 每次从cin得到一行,存入str
//istringstream ss(str) 表示把str存入ss这个流,可作为下一个getline()的第一个参数
// isstringstream用法:
//getline(ss,temp,‘,’)表示每次从ss里读取,遇‘,’就停止,每次存到temp里面
#include
#include
#include
#include
using namespace std;
void Print(const vector<string>& str){
for(int i = 0; i < str.size() - 1; i++){
cout << str[i] << ',';
}
cout << str[str.size() - 1] << endl;
}
int main(){
string s1;
vector<string> str;
string s2;
while(getline(cin, s1)){
istringstream ss(s1);
while(getline(ss, s2, ',')){
str.push_back(s2);
}
sort(str.begin(), str.end());
Print(str);
str.clear();
}
return 0;
}