题目地址:点击打开链接
全排列加二进制法(其实是三进制啦)
#include <iostream>
#include <algorithm>
using namespace std;
int num[5];
bool check(int val)
{
int temp=num[0];
for(int i=0;i<4;++i)
{
int k=val%3;
val/=3;
switch (k)
{
case 0:
temp+=num[i+1];break;
case 1:
temp-=num[i+1];break;
default:
temp*=num[i+1];break;
}
}
if(temp==23)
return true;
return false;
}
int main()
{
while(true)
{
int x=0;
for(int i=0;i<5;++i)
{
cin>>num[i];
x+=num[i];
}
if(x==0)
break;
sort(num,num+5);
bool flag=true;
do
{
for(int i=0;i<81;++i)
{
if(check(i))
{
cout<<"Possible"<<endl;
flag=false;
break;
}
}
}while (next_permutation(num,num+5)&&flag);
if(flag)
cout<<"Impossible"<<endl;
}
return 0;
}