search

Home  >  Q&A  >  body text

c++ - ACM小程序问题

题目链接在这里
http://chjsgl.openjudge.cn/pr...

赋上我的代码:

#include<stdio.h>
#include<string.h>
char a[12][3];
char b[12][3];
int n, m;
int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++)
        scanf("%s", a[i]);
    for (int i = 0; i < m; i++)
        scanf("%s", b[i]);
    int cnta1 = 0;
    int cnta2 = 0;
    int cntjqk1 = 0;
    int cntjqk2 = 0;
    int sum1 = 0;
    int sum2 = 0;
    int flag1 = 0;
    int flag2 = 0;
    for (int i = 0; i < n; i++)
    {
        if (strcmp(a[i], "A") == 0)
        {
            sum1 += 1;
            cnta1++;
        }
        else if (strcmp(a[i], "10") == 0)
        {
            sum1 += 10;
            cntjqk1++;
        }
        else if (strcmp(a[i], "J") == 0)
        {
            sum1 += 10;
            cntjqk1++;
        }
        else if (strcmp(a[i], "Q") == 0)
        {
            sum1 += 10;
            cntjqk1++;
        }
        else if (strcmp(a[i], "K") == 0)
        {
            sum1 += 10;
            cntjqk1++;
        }
        else
            sum1 += a[i][0] - '0';
    }
    if (n == 2 && cnta1 == 1 && cntjqk1 == 1)
        flag1 = 1;
    while (cnta1 != 0 && sum1 <= 21)
    {
        sum1 += 10;
        cnta1--;
    }
    for (int i = 0; i < m; i++)
    {
        if (strcmp(b[i], "A") == 0)
        {
            sum2 += 1;
            cnta2++;
        }
        else if (strcmp(b[i], "10") == 0)
        {
            sum2 += 10;
            cntjqk2++;
        }
        else if (strcmp(b[i], "J") == 0)
        {
            sum2 += 10;
            cntjqk2++;
        }
        else if (strcmp(b[i], "Q") == 0)
        {
            sum2 += 10;
            cntjqk2++;
        }
        else if (strcmp(b[i], "K") == 0)
        {
            sum2 += 10;
            cntjqk2++;
        }
        else
            sum2 += b[i][0] - '0';
    }
    if (m == 2 && cnta2 == 1 && cntjqk2 == 1)
        flag2 = 1;
    while (cnta2 != 0 && sum2 <= 21)
    {
        sum2 += 10;
        cnta2--;
    }
    if (sum2 > 21)
        puts("win");
    else if (sum1 > 21)
        puts("lose");
    else if (flag1 == 1 && flag2 == 0)
        puts("win");
    else if (flag1 == 0 && flag2 == 1)
        puts("lose");
    else if (flag1 == 1 && flag2 == 1)
        puts("draw");
    else if (sum1 > sum2)
        puts("win");
    else if (sum1 < sum2)
        puts("lose");
    else if (sum1 == sum2)
        puts("draw");
    return 0;
}

题目给的那个测试用例也过了,但是提交总是不对,不知道问题出现在哪里,还请高手帮帮忙!!谢谢啦!!O(∩_∩)O~

PHPzPHPz2810 days ago829

reply all(1)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 15:37:25

    I can’t understand the input and output example of this question

    2
        A
        10    A=11,11+10 = 21
    3
        5
        10
        6    5+10+6 = 21

    I calculated a draw!

    I wrote a code without verification, but you can take a look.

    #include <stdio.h>
    
    int calc_score(int n,int* p)
    {
        int ACount = 0;    // A 计数
        int score = 0;    // 成绩
        for(int i=0;i<n;++i){
            switch(p[i]){
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                    score += p[i] - '0';    // 2-10就是牌面值
                    break;
                case '1':
                    score += 10;    // 这个1实际是10
                    break;
                case 'J':
                case 'Q':
                case 'K':
                    score += 10;    // JQK都是10
                    if(ACount && n == 2){
                        score = 10000;    // 黑杰克就是10000分
                        return score;
                    }
                    break;
                case 'A':
                    ACount += 1;
                    score += 11;    // A 都先按照11分算
                    break;
            }
        }
    
        // 不是黑杰克,继续算点数
        while(score > 21 && ACount > 0){
            score -= 10;
            ACount -= 1;
        }
        return score > 21:-1:score;    // 大于21分,则算是-1分
    }
    
    int main()
    {
        int n,m,nscore,mscore;
        int pn[16],pm[16];
        char buf[1024];
        scanf("%d",&n);
        for(int i=0;i<n;++i){
            scanf("%s",buf);
            pn[i] = buf[0];
        }
        scanf("%d",&m);
        for(int i=0;i<m;++i){
            scanf("%s",buf);
            pm[i] = buf[0];
        }
        nscore = calc_score(n,pn);
        mscore = calc_score(m,pm);
    
        printf("%s",(nscore == mscore)?"draw":((nscore < mscore)?"lose":"win"));
    }

    reply
    0
  • Cancelreply