24: 00, 24: 00
Problem description: four numbers from 1 to 10 are given. The number is 24 after addition, subtraction, multiplication, and division.
Input:
Four numbers from 1 to 10. [Repeated numbers are allowed, and test cases ensure no exceptional numbers]
Output:
True or False
For example, enter:
7 2 1 10
Output:
True
/****************************************************************************** Copyright (C), 2001-2013, Huawei Tech. Co., Ltd. ****************************************************************************** File Name : Version : Author : Created : 2013/03/12 Last Modified : Description : Function List : History : 1.Date : 2013/03/12 Author : Modification: Created file******************************************************************************/#include <stdlib.h>#include <string.h>#include <stdio.h>//#include<iostream>//using namespace std;static char op[]={'+','-','*','/'};int cal(int a,int b,int c,int d,int i,int j,int k){int sum=0;if(i==0)sum=a+b;else if(i==1)sum=a-b;else if(i==2)sum=a*b;else if(i==3)sum=a/b;if(j==0)sum=sum+c;else if(j==1)sum-=c;else if(j==2)sum*=c;else if(j==3)sum/=c;if(k==0)sum+=d;else if(k==1)sum-=d;else if(k==2)sum*=d;else if(k==3)sum/=d;return sum;}bool Game24Points(int a, int b, int c, int d){int i,j,k;for(i=0;i<4;i++)for(j=0;j<4;j++)for(k=0;k<4;k++){if(cal(a,b,c,d,i,j,k)==24) return true;if(cal(a,b,d,c,i,j,k)==24)return true;if(cal(a,c,b,d,i,j,k)==24)return true;if(cal(a,c,d,b,i,j,k)==24)return true;if(cal(a,d,b,c,i,j,k)==24)return true;if(cal(a,d,c,b,i,j,k)==24)return true;if(cal(b,a,c,d,i,j,k)==24)return true;if(cal(b,a,d,c,i,j,k)==24)return true;if(cal(b,c,a,d,i,j,k)==24)return true;if(cal(b,c,d,a,i,j,k)==24)return true;if(cal(b,d,a,c,i,j,k)==24)return true;if(cal(b,d,c,a,i,j,k)==24)return true;if(cal(c,a,b,d,i,j,k)==24)return true;if(cal(c,a,d,b,i,j,k)==24)return true;if(cal(c,b,a,d,i,j,k)==24)return true;if(cal(c,b,d,a,i,j,k)==24)return true;if(cal(c,d,a,b,i,j,k)==24)return true;if(cal(c,d,b,a,i,j,k)==24)return true;if(cal(d,a,b,c,i,j,k)==24)return true;if(cal(d,a,c,b,i,j,k)==24)return true;if(cal(d,b,a,c,i,j,k)==24)return true;if(cal(d,b,c,a,i,j,k)==24)return true;if(cal(d,c,a,b,i,j,k)==24)return true;if(cal(d,c,b,a,i,j,k)==24)return true;}return false;}int main(){int a=Game24Points(7,2,1,10);printf("%d\n", a);return 0;}
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.