标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
#include<bits/stdc++.h> using namespace std; #define ll long long #define mod 1000000007 #define inf 999999999 #define esp 0.00000000001 //#pragma comment(linker, "/STACK:102400000,102400000") int scan() { int res = 0 , ch ; while( !( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) ) { if( ch == EOF ) return 1 << 30 ; } res = ch - ‘0‘ ; while( ( ch = getchar() ) >= ‘0‘ && ch <= ‘9‘ ) res = res * 10 + ( ch - ‘0‘ ) ; return res ; } int a[5]; int ans; int getnum(string a) { if(a[0]==‘1‘) return 10; if(a[0]>=‘2‘&&a[0]<=‘9‘) return a[0]-‘0‘; if(a[0]==‘A‘) return 1; if(a[0]==‘J‘) return 11; if(a[0]==‘K‘) return 13; if(a[0]==‘Q‘) return 12; } void dfs(int num,int gg,int step) { if(step==4) { if(num==24) ans=1; return; } //不加括号 dfs(num+gg,a[step+1],step+1); dfs(num-gg,a[step+1],step+1); dfs(num*gg,a[step+1],step+1); if(gg!=0&&num%gg==0) dfs(num/gg,a[step+1],step+1); //加括号 if(step!=3) { dfs(num,gg+a[step+1],step+1); dfs(num,gg*a[step+1],step+1); dfs(num,gg-a[step+1],step+1); if(a[step+1]!=0&&gg%a[step+1]==0) dfs(num,gg/a[step+1],step+1); } } string ch[10]; int main() { int x,y,z,i,t; while(cin>>ch[0]>>ch[1]>>ch[2]>>ch[3]) { ans=0; for(i=0;i<4;i++) a[i]=getnum(ch[i]); sort(a,a+4); do { dfs(a[0],a[1],1); } while(next_permutation(a,a+4)); if(ans) printf("Yes\n"); else printf("No\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/jhz033/p/5494837.html