C++输入输出
1.不确定多少行,每行不确定多少个数
输入:
1 2 3
4 5
0 0 0 0 0
输出:
6
9
0
int main() {
int num, sum = 0;
while (cin >> num) {
sum += num;
if (cin.get() == '\n') {
cout << sum << endl;
sum = 0;
}
}
return 0;
}
2.字符串输入
a c bb
f dddd
nowcoder
a bb c
dddd f
nowcoder
int main(){
vector<string> strs;
string str;
while (cin >> str) {
strs.emplace_back(str);
if (cin.get() == '\n') {
sort(strs.begin(), strs.end());
for (string s: strs) cout << s << ' ';
cout << endl;
strs.clear();
}
}
return 0;
}
3.字符串输入,逗号分隔
a,c,bb
f,dddd
nowcoder
a,bb,c
dddd,f
nowcoder
int main() {
string str;
vector<string> strs;
while (getline(cin, str)) {
stringstream ss(str);
string substr;
while (getline(ss, substr, ',')) {
strs.push_back(substr);
}
sort(strs.begin(), strs.end());
for (int i = 0; i < strs.size() - 1; ++i) {
cout << strs[i] << ',';
}
cout << strs.back() << endl;;
strs.clear();
}
return 0;
}