I started to use the backtracing TLE, and then switched to the full arrangement + DFS AC.
CodeAs follows:
(0.728 S ):
# Include <iostream> # include <algorithm> # include <cstring> # include <cstdlib> # include <cstdio> # include <cmath> using namespace STD; int A [5], flag; void solve (INT cur, int sum) {If (cur = 5) {If (sum = 23) Flag = 1; return;} solve (cur + 1, sum + A [cur]); If (FLAG) return; solve (cur + 1, Sum-A [cur]); If (FLAG) return; solve (cur + 1, sum * A [cur]); If (FLAG) return;} int main () {# ifdef test freopen ("sampl E.txt "," r ", stdin); # endif int B [5], s [5]; while (1) {for (INT I = 0; I <5; I ++) scanf ("% d", & B [I]); If (! B [0]) break; For (INT I = 0; I <5; I ++) s [I] = I; flag = 0; do {for (INT I = 0; I <5; I ++) A [s [I] = B [I]; solve (1, a [0]); if (FLAG) break;} while (next_permutation (S, S + 5); If (FLAG) printf ("Possible \ n "); else printf ("impossible \ n");} return 0 ;}
Sorting and processing will be simpler, optimized (0.432 S ):
# include
# include
# include
# include
# include
# include
using namespace STD; int A [5], flag; void solve (INT cur, int sum) {If (cur = 5) {If (sum = 23) Flag = 1; return ;} solve (cur + 1, sum + A [cur]); If (FLAG) return; solve (cur + 1, Sum-A [cur]); If (FLAG) return; solve (cur + 1, sum * A [cur]); If (FLAG) return;} int main () {# Ifdef test freopen ("sample.txt", "r", stdin); # endif while (1) {for (INT I = 0; I <5; I ++) scanf ("% d", & A [I]); If (! A [0]) break; flag = 0; sort (A, A + 5); // sort the order do {solve (1, a [0]); if (FLAG) break;} while (next_permutation (A, A + 5); If (FLAG) printf ("Possible \ n "); else printf ("impossible \ n") ;}return 0 ;}