zoukankan      html  css  js  c++  java
  • 洛谷 P1042 【乒乓球】题解

    算法:模拟

    此题说实在话细节上的问题还是比较多的,对得起 pj-的难度。

    细节1、在判断一场比赛是否结束时,如果是 11-10 的比分,比赛还要继续,只有到双方分差大于 2 分且至少有一方得到了大于等于 11 分的成绩,一场比赛才可以结束(假设在 11 分制下)。

    细节2、在输出时,如果一局比赛的比分为 0-0 (也就是还没有开始),也要输出!!!


    细节3、在输出时中间要换行一下!

    细节4、并非所有时候华华的比分都比对手的比分高!所以在判断比赛是否结束的时候写的微微有些复杂。

    其实还有很多需要注意的地方,这里就不再阐述了。

    $ m code $

    # include <bits/stdc++.h>
    # define ll long long
    # define rg register
    # define il inline
    using namespace std;
    const bool oj = true;
    il void getin(int &x) {
        x = 0; rg int f = 1; rg char ch = getchar();
        while(ch < '0' || ch > '9') f = ch == '-' ? -1 : f, ch = getchar();
        while(ch >= '0' && ch <= '9') x = x * 10 + ch - '0', ch = getchar();
        x *= f;
    }
    il int Max(int a, int b) {return a > b ? a : b;}
    il int Min(int a, int b) {return a < b ? a : b;}
    
    const int maxN = 10000 + 10;
    int s1[maxN], s2[maxN], s3[maxN], s4[maxN];
    void file() {
        freopen("1.in", "r", stdin);
        freopen("1.out", "w", stdout);
    }
    int main() {
        if(!oj) file();
        char ch; int a = 0, b = 0, c = 0, d = 0, tot1 = 0, tot2 = 0;
        //ch 是读入的字符
        //a 和 b 是在 11 分制下的比分,c 和 d 则是在 21 分制下的比分
        //tot1 指存放 11 分制的比分的数组的当前下标,tot2 指存放 21 分制的比分的数组的当前下标
        while(1) { 
            cin >> ch; //一个一个字符地读入
            if(ch == 'E') { //如果比赛信息结束了
                s1[++ tot1] = a, s2[tot1] = b;
                s3[++ tot2] = c, s4[tot2] = d;
                //先将最后的比分信息保存下来,然后退出 while 循环
                break;
            }
            if(ch == 'W') ++ a, ++ c;
            if(ch == 'L') ++ b, ++ d;
            //统计比分
            if((a >= 11 || b >= 11) && abs(a - b) >= 2) { //如果在 11 分制下一场比赛结束,将比分信息保存至数组,并将比分重新赋值为 0-0
                s1[++ tot1] = a, s2[tot1] = b;
                a = b = 0;
            }
            if((c >= 21 || d >= 21) && abs(c - d) >= 2) { //基本和上面的解释相同,这里不再阐述
                s3[++ tot2] = c, s4[tot2] = d;
                c = d = 0;
            }
        } 
        for(rg int i = 1; i <= tot1; ++ i)
            printf("%d:%d
    ", s1[i], s2[i]);
        puts("");
        for(rg int i = 1; i <= tot2; ++ i)
            printf("%d:%d
    ", s3[i], s4[i]);
        //最后,输出比分信息
        return 0;
    }
  • 相关阅读:
    查询手机内联系人
    加载媒体库里的音频
    用ContentProvider获取通讯录联系人
    TensorFlow学习笔记:保存和读取模型
    如何「优雅」地标数据
    Bagging, Boosting, Bootstrap
    3D中的旋转变换
    PCA算法浅析
    SQL Server数据库邮件配置
    浅谈checkpoint与内存缓存
  • 原文地址:https://www.cnblogs.com/Xray-luogu/p/11006451.html
Copyright © 2011-2022 走看看