21点游戏,求解答(不用if/else==)
本帖最后由 zhan1251 于 2013-02-05 16:28:15 编辑 Question:
The game of Blackjack requires the player to achieve a total of 21 or to get as close to 21 as possible without
exceeding that limit. The cards used to play the game are valued 2 to 10 with one special card worth either 1 or 11 and
that choice is left to the player. Given the value of three cards (1 – 10) determine the maximum value of the hand (the
three cards in the possession of the player) where the card valued at 1 can be used as either 1 or 11. Display a value of 0 if
there is no way to make the total of the hand less than or equal to 21.
大意翻译:
1. 输入1-10的3个随意整数;
2. “1”可以当“1”用,也可以当“11”用;
3. 结果为3数之和;
4. 如果结果大于21,输出0; 如果结果小于等于21,输出最好结果。
注意: 1. 不能使用if/else, switch...条件表达;
2. 不能使用&&, ||, !, <, >, ==...
Example #1:
Three values: 10 10 10
Best total: 0
Example #2:
Three values: 5 1 5
Best total: 21
Example #3:
Three values: 1 10 1
Best total: 12
Example #4:
Three values: 10 8 1
Best total: 19
Example #5:
Three values: 1 1 1
Best total: 13 游戏 C语言
[解决办法]
15楼啊,我这块砖头可是秦砖啊。
既然这样,我就把完全正确的代码贴出来吧,省得你纠结:
#include <iostream>
using namespace std;
#define LARGEINT999
int ExistAce(int a, int b, int c)
{
int x = (a / 2) * (b / 2) * (c / 2);
return LARGEINT / (LARGEINT + x);
}
int LargeInt(int a, int b)
{
int x1 = (LARGEINT - b) / (LARGEINT - a);
int x2 = 1 - x1;
return a * x1 + b * x2;
}
int RealValue(int a)
{
return a * ((LARGEINT - a) / (LARGEINT - 21));
}
int BestValue(int a, int b, int c)
{
int R1 = a + b + c;
int R2 = R1 + ExistAce(a, b, c) * 10;
int Rx1 = RealValue(R1);
int Rx2 = RealValue(R2);
return LargeInt(Rx1, Rx2);
}
int main()
{
// 以下是基本原理
printf_s("基本原理一:求两个数中比较大的值\n");
printf_s("A=%d, B=%d, x=%d\n", 15, 18, LargeInt(15, 18));
printf_s("A=%d, B=%d, x=%d\n", 20, 16, LargeInt(20, 16));
printf_s("A=%d, B=%d, x=%d\n", 21, 21, LargeInt(21, 21));
printf_s("\n");
printf_s("基本原理二:大于21就废掉(大于21就取值为0)\n");
printf_s("M=%d, Mx=%d\n", 18, RealValue(18));
printf_s("M=%d, Mx=%d\n", 21, RealValue(21));
printf_s("M=%d, Mx=%d\n", 25, RealValue(25));
printf_s("\n");
// 取五组数来试算
printf_s("实际计算:\n");
int A = 1, B = 5, C = 5;
printf_s("A=%d, B=%d, C=%d, V=%d\n", A, B, C, BestValue(A, B, C));
A = 1, B = 1, C = 5;
printf_s("A=%d, B=%d, C=%d, V=%d\n", A, B, C, BestValue(A, B, C));
A = 1, B = 9, C = 3;
printf_s("A=%d, B=%d, C=%d, V=%d\n", A, B, C, BestValue(A, B, C));
A = 7, B = 6, C = 5;
printf_s("A=%d, B=%d, C=%d, V=%d\n", A, B, C, BestValue(A, B, C));
A = 8, B = 2, C = 1;
printf_s("A=%d, B=%d, C=%d, V=%d\n", A, B, C, BestValue(A, B, C));
A = 5, B = 7, C = 1;
printf_s("A=%d, B=%d, C=%d, V=%d\n", A, B, C, BestValue(A, B, C));
A = 5, B = 1, C = 9;
printf_s("A=%d, B=%d, C=%d, V=%d\n", A, B, C, BestValue(A, B, C));
system("pause");
return 0;
}
基本原理一:求两个数中比较大的值
A=15, B=18, x=18
A=20, B=16, x=20
A=21, B=21, x=21
基本原理二:大于21就废掉(大于21就取值为0)
M=18, Mx=18
M=21, Mx=21
M=25, Mx=0
实际计算:
A=1, B=5, C=5, V=21
A=1, B=1, C=5, V=17
A=1, B=9, C=3, V=13
A=7, B=6, C=5, V=18
A=8, B=2, C=1, V=21
A=5, B=7, C=1, V=13
A=5, B=1, C=9, V=15
请按任意键继续. . .