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
相关推荐
hetao1733837几秒前
2026-01-16~19 hetao1733837 的刷题笔记
c++·笔记·算法
玖釉-7 分钟前
[Vulkan 学习之路] 29 - 加载模型 (Loading Models)
c++·windows·图形渲染
HelloWorld1024!25 分钟前
C++中链表的虚拟头结点:应用场景与使用时机
网络·c++·链表
fakerth26 分钟前
【C++】【Linux】从零实现日志库:LogLib 设计与实现详解
c++·日志库
大闲在人37 分钟前
Trae builder 实战: 让 C++ 函数像 Python 一样返回多个值
c++·python·ai编程
txinyu的博客1 小时前
手写 C++ 高性能 Reactor 网络服务器
服务器·网络·c++
枫叶丹41 小时前
【Qt开发】Qt系统(八)-> Qt UDP Socket
c语言·开发语言·c++·qt·udp
一颗青果1 小时前
c++的异常机制
java·jvm·c++
程序猿编码1 小时前
无状态TCP技术:DNS代理的轻量级实现逻辑与核心原理(C/C++代码实现)
c语言·网络·c++·tcp/ip·dns
期货资管源码1 小时前
智星期货资管子账户软件pc端开发技术栈的选择
c语言·数据结构·c++·vue