c++在线oj输入输出练习

c++在线oj输入输出练习

如何控制一行一行读取?cin.get() == ‘\n’

#include <iostream>
using namespace std;

int main() {
    int a;
    int sum = 0;
    while (cin >> a) { // 注意 while 处理多个 case
        sum += a;
        if(cin.get() == '\n'){
            cout<<sum<<endl;
            sum = 0;
        }
    }
}
// 64 位输出请用 printf("%lld")

如何判断当前字符是否为某个字符?cin.get(c);

#include <algorithm>
#include <iostream>
#include <linux/limits.h>
#include <vector>
using namespace std;
int main() {
    string s;
    vector<string> ss;
    char ch;
    string temp;
    while(cin.get(ch)){
        if(ch == ','){
            ss.push_back(temp);
            temp.clear();
            continue;
        }
        else if(ch == '\n'){
            ss.push_back(temp);
            temp.clear();
            sort(ss.begin(),ss.end());
            for(auto iter = ss.begin();iter!=ss.end();iter++){
                if(iter!=ss.end()-1){
                    cout<<*iter<<",";
                }
                else{
                    cout<<*iter<<endl;
                }
            }
            ss.clear();
        }
        else{
            temp.push_back(ch);
        }
    }
}
// 64 位输出请用 printf("%lld")

参考文献

[1] https://www.nowcoder.com/exam/test/71420049/detail?pid=27976983#question OJ在线编程常见输入输出练习。

猜你喜欢

转载自blog.csdn.net/ChenglinBen/article/details/131963788