给你n个数,1,2,3,,,n。然后每次可以取两个进行+或-或*,然后问最后能否使得结果为24。能就输出一种合法过程。
游戏题,也算数学题吧,构造题,规律题,反正我不擅长的题。
首先,n<=3,肯定是不能的。然后n=4,是可以有的,然后n=5,就是(5-3)*(2+1)*4。
然后,对于n>5,如果是奇数,先把前面那5个得到24,然后后面的两两一对,相减都是1,然后24*1*1*1就可以了。
如果n是偶数,也一样先把前面那4个得到24,然后后面的两两一对,相减都是1,然后24*1*1*1就可以了。
代码和思路都是参考的网上的
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define ll long long
void print(int n)
{
if (n == 4)
{
printf("1 * 2 = 2\n");
printf("2 * 3 = 6\n");
printf("6 * 4 = 24\n");
}
else if (n == 5)
{
printf("5 - 3 = 2\n");
printf("2 + 1 = 3\n");
printf("2 * 3 = 6\n");
printf("6 * 4 = 24\n");
}
else if (n > 4 && (n & 1) == 0)
{
print(4);
while (n > 4)
{
printf("%d - %d = 1\n", n, n - 1);
printf("1 * 24 = 24\n");
n -= 2;
}
}
else
{
print(5);
while (n > 5)
{
printf("%d - %d = 1\n", n, n - 1);
printf("1 * 24 = 24\n");
n -= 2;
}
}
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int n;
scanf("%d", &n);
if (n <= 3)
printf("NO\n");
else
{
printf("YES\n");
print(n);
}
//system("pause");
//while (1);
return 0;
}

本文介绍了一种解决经典24点游戏问题的算法。该算法适用于整数集合,通过加、减、乘操作使结果等于24。文章详细解释了针对不同数量输入的有效策略,并提供了实现代码。
826

被折叠的 条评论
为什么被折叠?



