2017年PTA乙级秋考A题 小赌怡情 分数 15 两次过 题型:模拟

112 阅读2分钟

PTA | 程序设计类实验辅助教学平台 (pintia.cn)

坑点

仔细读题,玩家下注小的时候不是 玩家比 庄家小 玩家就赢了,而是看庄家比玩家小玩家才赢,如果庄家比玩家大,玩家就输了。玩家下注大的时候同理。

#include<iostream>
using namespace std;

int main()
{
    int chip,k;cin>>chip>>k;  //赠送玩家的筹码数,k行

    int n1,b,t,n2; 
    
    int total=chip;
    for(int i=0;i<k;++i)
    {
        cin>>n1>>b>>t>>n2;
        
         if(total<=0)  //当筹码量为0时就输光了。
        {
            printf("Game Over.\n");
            break; //一定要退出循环,不然1,4,5过不去
        }
        else  //筹码不为0的情况
        {    
            if(t>total)   //下注的筹码量大于持有的筹码量
           {
               printf("Not enough tokens.  Total = %d.\n",total);
              continue;
           }
        
         
           if(b==0)   //赌小的情况
           {
                  if(n2>n1)  //赌小赌输了
                 {
                     total=total-t;
                     printf("Lose %d.  Total = %d.\n",t,total);
                 }
                else  //赌小赌赢了
                {
                    total=total+t;  //默认筹码量+下注筹码量
                    printf("Win %d!  Total = %d.\n",t,total);
                }
           }
         else  //赌大的情况
         {
               if(n2>n1)  //赌大赌赢了
                {
                    total=total+t;
                    printf("Win %d!  Total = %d.\n",t,total);
                }
                else   //赌大赌输了
                {
                    total=total-t;
                    printf("Lose %d.  Total = %d.\n",t,total);
                }
         }
           
        }
    }
        return 0;
}

第二种格式

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int total, k; cin >> total >> k;
    int   n1, b, t, n2;
    for (int i = 0; i < k; i++)
    {
        cin >> n1 >> b >> t >> n2;

         if (total <= 0)
        {
            printf("Game Over.");
            break;
        }
        //下注大于筹码量
        if (t > total)
        {
            printf("Not enough tokens.  Total = %d.\n", total);
            continue;
        }
     
        //玩家赌小的情况
        if (b == 0)
        {
            //玩家赌赢了
            if (n2 < n1)
            {
                total += t;
                printf("Win %d!  Total = %d.\n",t, total);
                continue;

            }
            else if (n2 > n1)
            {
                total -= t;
                printf("Lose %d.  Total = %d.\n",t, total);
               continue;
            }

        }
        //玩家赌大的情况
        else if (b == 1)
        {
            if (n2 > n1)
            {
                total += t;
                printf("Win %d!  Total = %d.\n",t, total);
                continue;
            }
            else if (n2 < n1)
            {
                total -= t;
                printf("Lose %d.  Total = %d.\n",t, total);
                continue;
             }
        }
    }
    return 0;
}