Ultraviolet A 10344-23 out of 5

Source: Internet
Author: User

Problem I

23 out of 5

Input:
Standard Input

Output:
Standard output

Time limit:1 second

Memory limit:
32 MB

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
. At the beginning of recursion, each time a number is lifted without a symbol, and then the current number is combined with the symbol of the previous number before the operation, so that I am confused .....
Put the number and symbol in the list first, and then calculate the number and symbol in the list again, 5! * 3 ^ 4 scale
#include<stdio.h>#include<string.h>int f,visit[10],a[10],b[10];int sort(int n,int num){int i; if ((n==5)&&(num==23)) f=1; if ((n>=5)||(f)) return 0; for (i=1;i<=5;i++) if (visit[i]==0) {visit[i]=1;  sort(n+1,num+a[i]);  sort(n+1,num-a[i]);  sort(n+1,num*a[i]);  visit[i]=0; }}int main(){int i; while (scanf("%d",&a[1])) {  for (i=2;i<=5;i++)  scanf("%d",&a[i]);  if ((a[1]==0)&&(a[2]==0)&&(a[3]==0)&&(a[4]==0)&&(a[5]==0)) break;  memset(visit,0,sizeof(visit));  memset(b,0,sizeof(b));  f=0;  for (i=1;i<=5;i++)  {visit[i]=1;   sort(1,a[i]);   visit[i]=0;  }  if (f) printf("Possible\n");    else printf("Impossible\n"); } 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.