• 2011年NOIP普及组复赛题解


    题目涉及算法:

    • 数字反转:模拟;
    • 统计单词数:模拟;
    • 瑞士轮:模拟/排序;
    • 表达式的值:后缀表达式/DP。

    数字反转

    题目链接:https://www.luogu.org/problem/P1307
    这道题目是一道基础的模拟题,只需要模拟将数字进行翻转就可以了,注意 (0) 和 负数。
    实现代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    void solve(int num) {
        if (num < 0) { putchar('-'); num = -num; }
        if (num == 0) { cout << 0 << endl; return; }
        int t = 0;
        while (num > 0) {
            t = t * 10 + num % 10;
            num /= 10;
        }
        cout << t << endl;
    }
    int n;
    int main() {
        cin >> n;
        solve(n);
        return 0;
    }
    

    统计单词数

    题目链接:https://www.luogu.org/problem/P1308
    这道题目就是一道简单的字符串匹配问题。
    关于字符串匹配问题有一些经典的算法(比如KMP等),但是我们这道题目数据量比驾小所以直接暴力比较就可以了。
    需要注意的是字符串的读入( char 数组使用 getsstring 使用 cin.getline
    实现代码如下:

    #include <bits/stdc++.h>
    using namespace std;
    char s[12], t[1000010];
    int n, m, cnt, idx;
    int main() {
        gets(s); gets(t);
        for (n = 0; s[n]; n ++) s[n] = tolower(s[n]);
        for (m = 0; t[m]; m ++) t[m] = tolower(t[m]);
        for (int i = 0; i+n-1 < m; i ++) {
            if ((i+n == m || t[i+n] == ' ') && (i == 0 || t[i-1] == ' ')) {
                bool flag = true;
                for (int j = 0; j < n; j ++) if (s[j] != t[i+j]) {
                    flag = false;
                    break;
                }
                if (flag) {
                    cnt ++;
                    if (cnt == 1) idx = i;
                }
            }
        }
        if (cnt) printf("%d %d
    ", cnt, idx);
        else puts("-1");
        return 0;
    }
    

    瑞士轮

    题目链接:https://www.luogu.org/problem/P1309
    这道题目就是按照题目描述来模拟一下就可以了。
    首先我们先设计我们的比较函数:分数从高到低排,分数相同时按编号从小到大排。
    然后呢,循环R轮。
    在最开始需要使用比较函数对这 2n 个人进行一下排序。
    然后接下来(R轮里面的)每一轮,我都比较相邻的两个元素,然后实力高一点的那个加一分;然后再按照比较函数拍一下序。
    实现代码如下(开了O2优化):

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 200200;
    int n, R, Q;
    struct Node {
        int id, s, w;
    } a[maxn];
    bool cmp(Node a, Node b) { return a.s > b.s || a.s == b.s && a.id < b.id; }
    int main() {
        scanf("%d%d%d", &n, &R, &Q);
        n *= 2;
        for (int i = 0; i < n; i ++) a[i].id = i+1;
        for (int i = 0; i < n; i ++) scanf("%d", &a[i].s);
        for (int i = 0; i < n; i ++) scanf("%d", &a[i].w);
        sort(a, a+n, cmp);
        while (R --) {
            for (int i = 0; i < n; i += 2) {
                if (a[i].w > a[i+1].w) a[i].s ++;
                else a[i+1].s ++;
            }
            sort(a, a+n, cmp);
        }
        printf("%d
    ", a[Q-1].id);
        return 0;
    }
    

    表达式的值

    题目链接:https://www.luogu.org/problem/P1310
    本题涉及算法:前缀表达式转后缀表达式,计算结果的时候有用到递归思想。
    题解地址:https://www.cnblogs.com/codedecision/p/11746099.html

    作者:zifeiy

  • 相关阅读:
    fastlane 自动化打包不同的target,以及手动传版本号参数
    xcode 添加Account报错 This action could not be completed,Try again
    iOS 如何在模拟器中安装APP
    git 基础总结
    cnpm run build 报错 Node Sass could not find a binding for your current environment: OS X 64-bit with Node.js 9.x
    iOS 静态库sdk项目依赖到工程项目测试
    Windows下程序的自删除
    80x86指令码查询表(转载)
    C语言实现Win32第一个窗口
    Visual Studio的Unicode和ASCII
  • 原文地址:https://www.cnblogs.com/codedecision/p/11746114.html
Copyright © 2020-2023  润新知