题意:给你一些数,问你是否能通过+ ,-, *,让结果等于23,注意的地方是:这些数可以任意排序
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int a[5];
int vis[5];
int flag;
void dfs(int cur,int sum)
{
if (cur == 5 && sum == 23)
{
flag = 1;
return;
}
for (int i = 0 ; i < 5 ; i++)
{
if (!vis[i])
{
vis[i] = 1;
dfs(cur+1,sum+a[i]);
dfs(cur+1,sum-a[i]);
dfs(cur+1,sum*a[i]);
vis[i] = 0 ;
}
}
}
int main()
{
while (1)
{
flag = 0;
for (int i = 0 ; i< 5 ; i++)
cin>>a[i];
for (int i = 0 ; i < 5 ; i++)
if (a[i])
flag = 1 ;
if (!flag)
break;
flag = 0;
for (int i = 0 ; i < 5 ; i++)
{
memset(vis,0,sizeof(vis));
vis[i] = 1 ;
dfs(1,a[i]);
}
if (flag)
cout << "Possible" << endl;
else
cout << "Impossible" << endl;
}
return 0 ;
}