uva10344 - 23 out of 5

下面的东西,你知道几个:

1、不同的排列相同的运算符得到的结果不同,

2、在生成排列前必须要排序数据。

好了,对于这个题,也没有什么好说的,直接贴代码把;

代码如下:

#include <cstdio>
#include <algorithm>
using namespace std;
int num[5], possible;
void print_product(int cur, int product)
{
    if(possible)return;
    if(cur==5) { if(product==23&&!possible)
    {possible = 1; } return;}

    print_product(cur+1,product+num[cur]);
    print_product(cur+1,product-num[cur]);
    print_product(cur+1,product*num[cur]);
}
int main ()
{
    int sum = 1;
    while(1)
    {
        possible = sum = 0;
        for(int i = 0; i < 5; i++)
        {scanf("%d",&num[i]); sum+=num[i];}
        if(!sum)break;
        sort(num,num+5);
        do
        {
            print_product(1,num[0]);

        }while(next_permutation(num,num+5));
        if(possible)puts("Possible");
        else puts("Impossible");
    }
    return 0;
}


posted on 2013-02-22 18:36  Primo...  阅读(149)  评论(0)    收藏  举报