PAT Level B-Little Gambling

Title description
As the saying goes, "Small gambling is happy."

This is a very simple little game:

  1. First, the computer gives the first integer;
  2. Then the player places a bet on whether the second whole number will be larger or smaller than the first one;
  3. After the player bets t chips, the computer gives the second number.
  4. If the player guesses correctly, the system rewards the player with t chips; otherwise, the player deducts t chips.

Note: The number of chips a player can bet cannot exceed the number of chips in their account. When the player has lost all his chips, the game ends.

Input format The
input gives two positive integers T and K in the first line, which are the number of chips that the system gives to the player in the initial state and the number of games that need to be processed.
Then K lines, each line corresponds to a game, and 4 numbers are given in sequence:n1 b t n2

Wherein n1and n2two computer has given [0, 9]an integer, to ensure that two numbers are not equal.
bTo 0represent the players bet , to 1represent the players bet . tIndicates the number of chips the player is betting, which is guaranteed to be within the integer range.

Output format
for each game, based on the following case corresponds to the output (which tis the amount the player bets, xis the amount of chips the player currently held):

  • Player wins, output Win t! Total = x.;
  • Player loses, outputs Lose t. Total = x.;
  • Players bet more than the amount of chips they hold and output Not enough tokens. Total = x.;
  • After the players gambled, output Game Over.and end the program.

Input example 1
100 4
8 0 100 2
3 1 50 1
5 1 200 6
7 0 200 8

输出样例1
Win 100! Total = 200.
Lose 50. Total = 150.
Not enough tokens. Total = 150.
Not enough tokens. Total = 150.

Input example 2
100 4
8 0 100 2
3 1 200 1
5 1 200 6
7 0 200 8

Output sample 2
Win 100! Total = 200.
Lose 200. Total = 0.
Game Over.

Data range
K ≤ 100


Problem solution
simulation:

#include <iostream>
#include <cstdio>
using namespace std;

int main()
{
    
    
	int T, K;
	cin >> T >> K;
	
	while(K --)
	{
    
    
		int n1, n2, b, t;
		cin >> n1 >> b >> t >> n2;
		if(t > T) printf("Not enough tokens.  Total = %d.\n", T);
		else
		{
    
    
			if(b && n1 < n2 || !b && n1 > n2) 
			{
    
    
			    T += t;
				printf("Win %d!  Total = %d.\n", t, T);
			}
			else 
			{
    
    
				T -= t;
				printf("Lose %d.  Total = %d.\n", t, T);
			}
			if(!T) 
			{
    
    
				cout << "Game Over." << endl;
				break;
			}
		}	
	}
	
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_46239370/article/details/114435724