One Person
Description
In the game show "The Price is Right", a number of players (typically 4) compete to get on stage by guessing the price of an item. The winner is the person whose guess is the closest one not exceeding the actual price. Because of the popularity of the one-person game show "Who Wants to be a Millionaire",the American Contest Management (ACM) would like to introduce a one-person version of the "The Price is Right". In this version, each contestant is allowed G (1 <= G <= 30) guesses and L (0 <= L <= 30)lifelines. The contestant makes a number of guesses for the actual price. After each guess, the contestant is told whether it is correct, too low, or too high. If the guess is correct, the contestant wins. Otherwise,he uses up a guess. Additionally, if his guess is too high, a lifeline is also lost. The contestant loses when all his guesses are used up or if his guess is too high and he has no lifelines left. All prices are positive integers.
It turns out that for a particular pair of values for G and L, it is possible to obtain a guessing strategy such that if the price is between 1 and N (inclusive) for some N, then the player can guarantee a win.The ACM does not want every contestant to win, so it must ensure that the actual price exceeds N.At the same time, it does not want the game to be too diffcult or there will not be enough winners to attract audience. Thus, it wishes to adjust the values of G and L depending on the actual price. To help them decide the correct values of G and L, the ACM has asked you to solve the following problem.Given G and L, what is the largest value of N such that there is a strategy to win as long as the price is between 1 and N (inclusive)?
Input
The input consists of a number of cases. Each case is specified by one line containing two integers G and L, separated by one space. The end of input is specified by a line in which G = L = 0.
Output
For each case, print a line of the form:
Case c: N
where c is the case number (starting from 1) and N is the number computed.
Sample Input
3 0
3 1
10 5
7 7
0 0
Sample Output
Case 1: 3
Case 2: 6
Case 3: 847
Case 4: 127
Source
East Central North America 2002
分析:这应该是一道组合数学题目,题目大意:猜数在有G次猜测机会,L此出错机会的条件下,能猜到的最大数是多少?
用数组itg[G][L]表示结果,则:
l itg[0][L = 0, 1, . . . ] = 0且itg[G=0, 1, . . .][0] = G;
l itg[G][L] = itg[G-1][L-1] + itg[G-1][L] + 1,理解这个等式是关键:可以这么去理解,设G次猜测机会分别为第1次机会、第2次机会,. . .,第G次机会. 首先使用第G次机会进行猜测,进行第G次猜测至少能够将能猜到的最大数增加1(想想为什么?),而进行第G次猜测后还剩下G-1次机会,且第G次猜测有可能出错,也有可能没有出错,所以itg[G-1][L-1]和itg[G-1][L]均包含在猜测结果中.
代码实现:
#include <string.h>
#include "iostream"
using namespace std;
int main(void)
{
int G, L, num = 0;
int i, j, itg[32][32];
cin >> G >> L;
for(i = 0; i < 32; i++)
{
itg[0][i] = 0;
itg[i][0] = i;
}
while(G != 0 || L != 0)
{
num ++;
for(i = 1; i <= G; i++)
{
for(j = 1; j <= L; j++)
itg[i][j] = itg[i-1][j-1] + itg[i-1][j] + 1;
}
cout << "Case " << num << ": " << itg[G][L] << endl;
cin >> G >> L;
}
return 0;
}
提交结果:
Problem: 1243 |
|
User: uestcshe |
Memory: 232K |
|
Time: 16MS |
Language: C++ |
|
Result: Accepted |