牛客网acm模式输入输出11道题目的c++解答(c标准io版)

 
Category: C_C++

写在前面

之前写过关于牛客网的输入输出的题目, 但是是用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;
}