牛客网ACM模式输入输出11道题目的C++解答(C标准IO版)

26 篇文章 0 订阅

tags: C++ Interview

写在前面

之前写过关于牛客网的输入输出的题目, 但是是用C++的标准IO写的, 虽然方便, 但是据说速度会很慢, 这里还是再用C重写一遍, 主要用到了scanfprintf.

地址:
牛客竞赛_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ (nowcoder.com);

不列出题目了, 直接看链接即可.

11道题的题解

1

#include <cstdio>

using namespace std;

int main(void) {
    int a, b;
    while (~scanf("%d %d", &a, &b)) printf("%d\n", a + b);
    return 0;
}

2

#include <cstdio>

using namespace std;

int main(void) {
    int a, b, n;
    scanf("%d", &n);
    while (n--) scanf("%d %d", &a, &b), printf("%d\n", a + b);
    return 0;
}

3

#include <cstdio>

using namespace std;

int main(void) {
    int a, b;
    while (~scanf("%d %d", &a, &b)) {
        if (!a || !b) break;
        printf("%d\n", a + b);
    }
    return 0;
}

4

#include <cstdio>

using namespace std;

int main(void) {
    int a, n, sum;
    while (~scanf("%d", &n)) {
        if (!n) break;
        sum = 0;
        while (n--) scanf("%d", &a), sum += a;
        printf("%d\n", sum);
    }
    return 0;
}

5

#include <cstdio>

using namespace std;

int main(void) {
    int a, m, n, sum;
    scanf("%d", &m);
    while (m--) {
        scanf("%d", &n);
        sum = 0;
        while (n--) scanf("%d", &a), sum += a;
        printf("%d\n", sum);
    }
    return 0;
}

6

#include <cstdio>

using namespace std;

int main(void) {
    int a, n, sum;
    while (~scanf("%d", &n)) {
        sum = 0;
        while (n--) scanf("%d", &a), sum += a;
        printf("%d\n", sum);
    }
    return 0;
}

7

#include <cstdio>

using namespace std;

int main(void) {
    int a, sum{};
    while (~scanf("%d", &a)) {
        sum += a;
        if (getchar() == '\n') {
            printf("%d\n", sum);
            sum = 0;
        }
    }
    return 0;
}

8

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

using namespace std;

int main(void) {
    int n;
    char tmp[1000]; // 必须给出数据范围
    scanf("%d", &n);
    vector<string> ans(n);
    while (n--) {
        scanf("%s", tmp);
        ans[n] = string(tmp);
    }
    sort(ans.begin(), ans.end());
    for (auto s : ans) cout << s << " ";
    return 0;
}

9

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

using namespace std;

int main(void) {
    int n;
    char tmp[1000]; // 必须给出数据范围
    vector<string> ans;
    while (~scanf("%s", tmp)) {
        ans.emplace_back(string(tmp));
        if (getchar() == '\n') {
            sort(ans.begin(), ans.end());
            for (auto s : ans) cout << s << " ";
            putchar('\n');
            ans.clear();
        }
    }

    return 0;
}

10

写的有点复杂了, 感觉可以用scanf的格式化读取直接读.

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

using namespace std;

int main(void) {
    int n;
    char tmp[1000]; // 必须给出数据范围
    vector<string> ans;
    while (~scanf("%s", tmp)) {
        string stmp = string(tmp), ss{};
        for (int i{}; i < stmp.size(); ++i) {
            if (isalpha(stmp[i]))
                ss += stmp[i];
            else
                ans.emplace_back(ss), ss.clear();
        }
        ans.emplace_back(ss);
        sort(ans.begin(), ans.end());
        for (int i{}; i < ans.size(); ++i)
            cout << ans[i] << (i == ans.size() - 1 ? "\n" : ",");
        ans.clear();
    }

    return 0;
}

纯scanf版: 用到了scanf的高级用法

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

using namespace std;

int main(void) {
    int n;
    char tmp[100]; // 必须给出数据范围
    vector<string> ans;
    // %[^,]遇到逗号停止
    while (~scanf("%[^,\n]", tmp)) {
        ans.emplace_back(string(tmp));
        if (getchar() == '\n') {
            sort(ans.begin(), ans.end());
            for (auto it = ans.begin(); it != ans.end(); ++it)
                cout << *it << (it == ans.end() - 1 ? "\n" : ",");
            ans.clear();
        }
    }
    return 0;
}

11

#include <cstdio>

using namespace std;

int main(void) {
    long a, b;
    while (~scanf("%ld%ld", &a, &b)) printf("%ld\n", a + b);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zorchp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值