10344-23 out of 5
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=1285
Your task is to write a program so can decide whether can find a arithmetic expression consisting of five given Num Bers (1<=i<=5) that would yield the value 23. For this problem we'll only consider arithmetic expressions of the following from:
Where: {1,2,3,4,5}-> {1,2,3,4,5} is a bijective function
and {+,-, *} (1<=I<=4)
Input
The Input consists of 5-tupels of positive integers, each between 1 and 50. The Input is terminated by a line containing five zero ' s. This line should is processed.
Output
This article URL address: http://www.bianceng.cn/Programming/sjjg/201410/45364.htm
For each 5-tupel print "Possible" (without quotes) if their exists a arithmetic expression (as described) that above DS 23. Otherwise print "impossible".
Sample Input
1 1 1 1 1
1 2 3 4 5
2 3 5 7 11
0 0 0 0 0
Sample Output
Impossible
Possible
Possible
Water problem.
Complete code:
/*0.279s*/
#include <cstdio>
#include <algorithm>
using namespace std;
int a[5];
BOOL Flag;
void Dfs (int cur, int ans)
{
if (cur = 5)
{
if (ans = =) flag = true;
return;
}
if (!flag) DFS (cur + 1, ans + a[cur]);
if (!flag) DFS (cur + 1, ans-a[cur]);
if (!flag) DFS (cur + 1, ans * a[cur]);
int main ()
{while
(scanf ("%d%d%d%d%d", &a[0], &a[1], &a[2], &a[3], &a[4), a[0])
{
flag = false;
Sort (A, a + 5);
Do
{
dfs (1, a[0]);
if (flag) break;
while (Next_permutation (A, A + 5));
Puts (flag?) "Possible": "Impossible");
return 0;
}