UVA 10344 23 out of 5
找23点。。用深搜。。直到找到23点为止。。
每个数字有3种情况 + - *
找到5个数字就判断。。
#include <algorithm>
using namespace std;
#include <stdio.h>
#include <string.h>
int num[5];
int judge;
void dfs(int star, int sum)
{
if (judge == 1)
return;
if (star == 5)
{
if(sum == 23)
judge = 1;
return;
}
dfs(star + 1, sum + num[star]);
dfs(star + 1, sum - num[star]);
dfs(star + 1, sum * num[star]);
}
int main()
{
while(scanf("%d%d%d%d%d", &num[0], &num[1], &num[2], &num[3], &num[4]) != EOF && num[0])
{
judge = 0;
sort(num,num + 5);
dfs(1, num[0]);
while (next_permutation(num, num + 5))
{
dfs(1,num[0]);
if (judge == 1)
break;
}
if (judge == 1)
{
printf("Possible\n");
}
else
{
printf("Impossible\n");
}
}
return 0;
}