Uva10344-23 out of 5

Source: Internet
Author: User
Tags time 0

Your task is to write a program that can decide whether you can find an arithmetic expression consisting of five given numbers (1 <= I <= 5) that will yield the value 23.
For this problem we will 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.
Input is terminated by a line containing five zero's. This line shoshould not be processed.

Output

For each 5-tupel print "possible" (without quotes) if their exists an arithmetic expression (as described abve) that yields 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

 

// Question: enter 5 integers and sort them in a certain order and then perform the plus (+), minus (-), or minus (*) to make the final result 23. Determine whether there is a solution
// Algorithm: backtracking

Time 1.692

# Include <cstdio> # include <cstring> # include <iostream> # include <string> # include <algorithm> using namespace STD; int A [5]; int B [5]; char op [5]; int vis [5]; int possible; void DFS (INT D, int s) {If (D = 5) {If (S = 23) {Possible = 1; # ifndef online_judge printf ("(% d", B [0]); For (INT I = 1; I <4; I ++) printf ("% C % d)", op [I], B [I]); printf ("% C % d ", OP [4], B [4]); printf ("\ n"); # endif} return;} For (INT I = 0; I <5; I ++) {// The first number if (D = 0) {If (! Vis [I]) {vis [I] = 1; B [d] = A [I]; DFS (D + 1, a [I]); vis [I] = 0 ;}} else {If (! Vis [I]) {vis [I] = 1; B [d] = A [I]; OP [d] = '+'; DFS (D + 1, S + A [I]); OP [d] = '-'; DFS (D + 1, S-A [I]); OP [d] = '*'; DFS (D + 1, S * A [I]); vis [I] = 0 ;}}} int main () {# ifndef online_judge freopen (". /uva10344.in "," r ", stdin); # endif while (scanf (" % d ", A, A + 1, A + 2, A + 3, A + 4) = 5 & (A [0] | A [1] | A [2] | A [3] | A [4]) {Possible = 0; DFS (0, 0); If (possible) puts ("possible"); else puts ("impossible");} return 0 ;}

Return immediately after finding, time 0.945

Learning point: when DFS is used to determine the return value of the extended point, if it has been successful, it will be returned directly.

# Include <cstdio> # include <cstring> # include <iostream> # include <string> # include <algorithm> using namespace STD; int A [5]; int vis [5]; bool DFS (int d, int s) {If (D = 5) return S = 23; For (INT I = 0; I <5; I ++) if (! Vis [I]) {vis [I] = 1; // The first number if (D = 0) {If (DFS (D + 1, a [I]) return true;} else {If (DFS (D + 1, S + A [I]) return true; If (DFS (D + 1, s-A [I]) return true; If (DFS (D + 1, S * A [I]) return true;} vis [I] = 0 ;} return false;} int main () {# ifndef online_judge freopen (". /uva10344.in "," r ", stdin); # endif while (scanf (" % d ", A, A + 1, A + 2, A + 3, A + 4) = 5 & (A [0] | A [1] | A [2] | A [3] | A [4]) {memset (VIS, 0, sizeof (VIS); If (DFS (0, 0) puts ("possible"); else puts ("impossible ");} return 0 ;}

 

Next_permutation calculation almost times out: 2.388

#include<cstdio>#include<cstring>#include<iostream>#include<string>#include<algorithm>using namespace std;int a[5];bool check(int k){    int s=a[0];    for(int j=1;j<5;j++)    {        switch(k%3)        {            case 0:                s+=a[j]; break;            case 1:                s-=a[j]; break;            case 2:                s*=a[j]; break;        }        k/=3;    }    return s==23;}int main(){#ifndef ONLINE_JUDGE    freopen("./uva10344.in", "r", stdin);#endif    while(scanf("%d%d%d%d%d", a, a+1, a+2, a+3, a+4)==5             && (a[0] || a[1] || a[2] || a[3] || a[4]))    {        sort(a, a+5);        int ok=0;        do        {            for(int i=0;i<81;i++) if(check(i))            {                ok=1;                break;            }        }while(!ok && next_permutation(a, a+5));        if(ok)            puts("Possible");        else            puts("Impossible");    }    return 0;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.