UVa – 10344 – 23 out of 5

Problem Link : 10344 – 23 out of 5

#include <iostream>
#include <algorithm>
using namespace std;

int a[5];
char ans[2][12] = { "Impossible", "Possible" };
int find(int sum, int i) {
	if (i == 5)
		return sum == 23 ;
	return find(sum + a[i], i + 1) || find(sum - a[i], i + 1) || find(sum * a[i], i + 1) ;
}
int main() {
	while (cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4])
		if (a[0] || a[1] || a[2] || a[3] || a[4]) {
			sort(a, a + 5);
			int found = 0;
			do {
				if (found |= find(a[0], 1))
					break;
			} while (next_permutation(a, a + 5));

			cout << ans[found] << endl;
		}
	return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: