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
相关推荐
Severus_black2 小时前
【C++初阶】list使用及相关实现,思路顺滑流畅!
c语言·数据结构·c++·stl·list
敲上瘾3 小时前
Qt Widgets入门:项目模板解析与Hello World
开发语言·数据库·c++·qt·ui·qt6.3
旖旎夜光5 小时前
【C++ 基础入门】C++ 多态(中):虚函数表、多态原理与动静绑定
开发语言·c++·笔记·学习
旖旎夜光6 小时前
【C++ 基础入门】C++多态(下):单/多继承虚表、多态补充与 21 道高频题
开发语言·c++·笔记·学习
apcipot_rain16 小时前
C++转Java日记(03)
java·开发语言·c++
布莱克60516 小时前
C++ 中 map 的 insert 与 emplace:key 已存在时会覆盖吗?
开发语言·数据结构·c++·算法·map
xys_67817 小时前
深挖C++:类和对象(中)
开发语言·c++
D e re k ZH19 小时前
C++ 系统学习手记:从语法细节到工程实践
开发语言·数据结构·c++·算法
xiaoye-duck20 小时前
《C++ 项目:手写大模型接入 SDK》Ollama接口设计(上):初始化、全量响应实现和测试
开发语言·网络·c++·语言模型
zaizai100721 小时前
【Effective Modern C++】学习第一章
c++