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
相关推荐
hansang_IR3 小时前
【题解】LC:倍增 / 区间并查集(Range Parallel Unionfind)
c++·算法·并查集
东东最爱敲键盘3 小时前
day7.c++继承
c++
沫璃染墨3 小时前
《从零入门Linux系统篇(十五):系统工具篇·六——GDB调试器详解:从程序执行控制到高级调试技巧》
linux·运维·服务器·c语言·c++
吃着火锅x唱着歌4 小时前
Effective C++ 学习笔记 条款40 明智而审慎地使用多重继承
c++·笔记·学习
醉城夜风~4 小时前
(超详细)C++ STL list容器详解:从使用方法到双向链表底层原理全面解析
c++·windows
烟花落o5 小时前
C++ 从 C 到进阶 ②:引用、inline 、nullptr
c++·引用·inline·nullptr·c++入门
liulilittle5 小时前
MOE路由:路由(logits: top-k/8)
c++·人工智能·算法·机器学习·llm
旖旎夜光5 小时前
LeetCode 11:盛最多水的容器(双指针问题) —— 题解
数据结构·c++·算法·leetcode·双指针
我不会插花弄玉7 小时前
15.map,set下:AVL树,红黑树和map,set的封装【由浅入深-C++】
c++·stl
hold?fish:palm7 小时前
20 旋转图像
c++·算法·leetcode