C++ > 牛客OJ在线编程常见输入输出练习场

C++处理输入输出

牛客OJ在线编程常见输入输出练习场

https://ac.nowcoder.com/acm/contest/5647

A A+B(1)

cpp 复制代码
#include <iostream>
#include <sstream>
using namespace std;

int main() {
    string line;
    while (getline(cin, line)) {
        istringstream iss(line);
        int a, b;
        iss >> a >> b;
        cout << a + b << endl;
    }
    return 0;
}

B A+B(2)

cpp 复制代码
#include <iostream>
#include <sstream>
using namespace std;

int main() {
    int t;
    cin >> t;
    cin.ignore(); // 忽略换行符

    for (int i = 0; i < t; i++) {
        string line;
        getline(cin, line);
        istringstream iss(line);
        int a, b;
        iss >> a >> b;
        cout << a + b << endl;
    }
    return 0;
}

C A+B(3)

cpp 复制代码
#include <iostream>
#include <sstream>
using namespace std;

int main() {
    string line;
    while (getline(cin, line)) {
        istringstream iss(line);
        int a, b;
        iss >> a >> b;
        if (a == 0 && b == 0) {
            break;
        }
        cout << a + b << endl;
    }
    return 0;
}

D A+B(4)

cpp 复制代码
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;

int main() {
    string line;
    while (getline(cin, line)) {
        istringstream iss(line);
        int n;
        iss >> n;
        if (n == 0) {
            break;
        }
        int sum = 0;
        int num;
        while (iss >> num) {
            sum += num;
        }
        cout << sum << endl;
    }
    return 0;
}

E A+B(5)

cpp 复制代码
#include <iostream>
#include <sstream>
using namespace std;

int main() {
    int t;
    cin >> t;
    cin.ignore(); // 忽略换行符

    for (int i = 0; i < t; i++) {
        string line;
        getline(cin, line);
        istringstream iss(line);
        int n;
        iss >> n;
        int sum = 0;
        int num;
        while (iss >> num) {
            sum += num;
        }
        cout << sum << endl;
    }
    return 0;
}

F A+B(6)

cpp 复制代码
#include <iostream>
#include <sstream>
using namespace std;

int main() {
    string line;
    while (getline(cin, line)) {
        istringstream iss(line);
        int n;
        iss >> n;
        int sum = 0;
        int num;
        while (iss >> num) {
            sum += num;
        }
        cout << sum << endl;
    }
    return 0;
}

G A+B(7)

cpp 复制代码
#include <iostream>
#include <sstream>
using namespace std;

int main() {
    string line;
    while (getline(cin, line)) {
        istringstream iss(line);
        int sum = 0;
        int num;
        while (iss >> num) {
            sum += num;
        }
        cout << sum << endl;
    }
    return 0;
}

H 字符串排序(1)

cpp 复制代码
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    int n;
    cin >> n;
    cin.ignore(); // 忽略换行符

    string line;
    getline(cin, line);
    istringstream iss(line);

    vector<string> strs;
    string word;
    while (iss >> word) {
        strs.push_back(word);
    }

    sort(strs.begin(), strs.end());

    for (size_t i = 0; i < strs.size(); i++) {
        if (i > 0) cout << " ";
        cout << strs[i];
    }
    cout << endl;

    return 0;
}

I 字符串排序(2)

cpp 复制代码
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    string line;
    while (getline(cin, line)) {
        istringstream iss(line);
        vector<string> strs;
        string word;
        while (iss >> word) {
            strs.push_back(word);
        }

        sort(strs.begin(), strs.end());

        for (size_t i = 0; i < strs.size(); i++) {
            if (i > 0) cout << " ";
            cout << strs[i];
        }
        cout << endl;
    }
    return 0;
}

J 字符串排序(3)

链接:https://ac.nowcoder.com/acm/contest/5647/J

输入描述:

多个测试用例,每个测试用例一行。

每行通过,隔开,有n个字符,n<100

输出描述:

对于每组用例输出一行排序后的字符串,用','隔开,无结尾空格

cpp 复制代码
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
    string line;
    while (getline(cin, line)) {
        vector<string> strs;
        size_t pos = 0;
        string token;

        // 手动按逗号分割
        while ((pos = line.find(',')) != string::npos) {
            token = line.substr(0, pos);
            strs.push_back(token);
            line.erase(0, pos + 1);
        }
        // 添加最后一个元素
        if (!line.empty()) {
            strs.push_back(line);
        }

        sort(strs.begin(), strs.end());

        for (size_t i = 0; i < strs.size(); i++) {
            if (i > 0) cout << ",";
            cout << strs[i];
        }
        cout << endl;
    }
    return 0;
}

C++输入输出要点总结

场景 写法
引入头文件 #include <iostream>, <sstream>
使用命名空间 using namespace std;
读取一行 getline(cin, line)
创建字符串流 istringstream iss(line)
从流中读取整数 iss >> num
忽略换行符 cin.ignore()
字符串分割(空格) 使用istringstream配合>>运算符
字符串分割(其他分隔符) 使用find()和substr()手动分割
向量排序 sort(vec.begin(), vec.end())
输出带分隔符 循环输出,条件判断是否添加分隔符
编译命令 clang++ -std=c++18 file.cpp -o output
相关推荐
郝学胜_神的一滴18 小时前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
卷无止境3 天前
C++ 的Eigen 库全解析
c++
卷无止境3 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端
郝学胜_神的一滴3 天前
CMake 27:缓存变量的特性、语法、类型与实操全解
c++·cmake
博客18005 天前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴5 天前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
众少成多积小致巨6 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
clint45610 天前
C++进阶(1)——前景提要
c++
夜悊10 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴10 天前
CMake 021: IF 条件判据详诠
c++·cmake