牛客ACM模式输入输出练习题,1-11

牛客ACM模式输入输出练习题


2023年4月11日 19:46:28

牛客竞赛_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ

ACM模式输入输出练习,总用时两小时多。

参考:
https://blog.csdn.net/weixin_44671418/article/details/125352042
https://www.nowcoder.com/discuss/460145181203931136?sourceSSR=home


已知输入组数

【链接】(文章,视频,题目)登录—专业IT笔试面试备考平台_牛客网

【第一想法与实现(困难)】

【看后想法】

  • 已知组数,用while (t–)的写法

【实现困难】

【自写代码】

#include <iostream>

int main() {
    int t;
    int a, b;
    std::cin >> t;
    while (t--) {
        std::cin >> a >> b;
        std::cout << a + b << std::endl;
    }
    return 0;
}

【收获与时长】


未知多组+组内给定n数据

【链接】(文章,视频,题目)登录—专业IT笔试面试备考平台_牛客网

【第一想法与实现(困难)】

【看后想法】

  • 给定了组内的n组数据,每次用sum处理当前组的和

【实现困难】

【自写代码】

#include <iostream>

int main() {
    int n;
    int a, b;
    while (std::cin >> n && n != 0) {
        int num(0), sum(0);
        while (n--) {
            std::cin >> num;
            sum += num;
        }
        std::cout << sum << std::endl;
    }
    return 0;
}

【收获与时长】


未知多组+空格隔开的未知多数

【链接】(文章,视频,题目)

登录—专业IT笔试面试备考平台_牛客网

【第一想法与实现(困难)】

  • 比较麻烦的点在于一行不知道有多少个数据,要用行的结束来判断一组数据的结束

    • if (std::cin.get() = ‘\n’)

【看后想法】

【实现困难】

【自写代码】

#include<iostream>

int main() {
    int num(0), sum(0);
    while (std::cin >> num) {
        sum += num;
        if (std::cin.get() == '\n') { // 一组数据结束了
            std::cout << sum << std::endl;
            sum = 0;
        }
    }
    return 0;
}

【收获与时长】


未知多行+行内空格分开的字符

【链接】(文章,视频,题目)

登录—专业IT笔试面试备考平台_牛客网

【第一想法与实现(困难)】

【看后想法】

  • 由于是行内空格分开,一个可以获得一个单词

  • 行末用if (std::cin.get() == ‘\n’)来判断

  • 对于打印输出,手动写一个PrintStringVector函数,注意第一个字符串前没有空格

  • 需要手动多包含一些常用库函数

【实现困难】

【自写代码】

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

void PrintStringVector(const std::vector<std::string>& str_vec) {
    if (!str_vec.empty()) {
        std::cout << str_vec.front();
    }
    for (int i = 1; i < str_vec.size(); ++i) {
        std::cout << ' ' << str_vec[i];
    }
    std::cout << std::endl;
}

int main() {
    std::string str;
    std::vector<std::string> str_vec;
    while (std::cin >> str) {
        str_vec.emplace_back(str);
        if (std::cin.get() == '\n') {
            std::sort(str_vec.begin(), str_vec.end());
            PrintStringVector(str_vec);
            str_vec.clear();
        }
    }
    return 0;
}

【收获与时长】


未知多行+行内逗号隔开

登录—专业IT笔试面试备考平台_牛客网

【第一想法与实现(困难)】

【看后想法】

  • 由于使用逗号隔开,不是空格或者tab,一次只能获取完整的一行,line

  • line中每个字符,用逗号判断是否单词字符串结束

  • 每个单词字符串str,加入到这组数据(一行)的str_vec中

  • 注意line结束之后还会有最后一个没有用逗号结束的单词字符串

【实现困难】

【自写代码】

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

void PrintStringVector(const std::vector<std::string>& str_vec) {
    if (!str_vec.empty()) {
        std::cout << str_vec.front();
    }
    for (int i =1; i < str_vec.size(); ++i) {
        std::cout << ',' << str_vec[i];
    }
    std::cout << std::endl;
}

int main() {
    std::string line;
    while (std::cin >> line) {
        std::string str;
        std::vector<std::string> str_vec;
        for (const char c : line) {
            if (c == ',') {
                str_vec.emplace_back(str);
                str.clear();
            } else {
                str += c;
            }
        }
        // 加入最后一个单词str
        str_vec.emplace_back(str);
        std::sort(str_vec.begin(), str_vec.end());
        PrintStringVector(str_vec);
    }
    return 0;
}

【收获与时长】

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值