24點
阿新 • • 發佈:2018-12-30
dep pac col lse i++ esp spa easy namespace
superwyh是一個非常瘋狂的24點愛好者,空閑時總是自己拿出撲克來算24點,24點的規則很簡單,就是給你4張撲克(從1至13,用A代替1,J代替11,Q代替12,K代替13)通過加減乘除來求得24,各位oier幫了superwyh好多忙,為了報答大家superwyh就和大家做個24點的遊戲,superwyh給大家4張牌大家告訴superwyh能不能湊成24就行。
[renqing PS:這道題很easy,是送分的題]
輸入格式4張牌的牌面(1<=n<=13)。輸出格式如果能湊成輸出”yes”反之輸出”no”。
樣例輸入
A 2 3 4
輸出
yes
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int flag[5]={0},num=0,a[5]; string c; int cd[5]; bool used[5]; bool dfs1(int depth,double amount) { if (depth==4) { if (amount>23.999999&&amount<24.000001) return 1; else return 0; } else { for (int i=0;i<4;i++) if (!used[i]) { used[i]=1; if (dfs1(depth+1,amount+cd[i])) return 1; if (dfs1(depth+1,amount-cd[i])) return 1; if (dfs1(depth+1,cd[i]-amount)) return 1; if (amount!=0&&dfs1(depth+1,amount*cd[i])) return 1; if (amount!=0&&dfs1(depth+1,amount/cd[i])) return 1; if (amount!=0&&dfs1(depth+1,cd[i]/amount)) return 1; used[i]=0; } return 0; } } int main() { for(int i=0;i<=3;i++) { cin>>c; if(c[0]==‘A‘)cd[i]=1; else if(c[0]==‘J‘)cd[i]=11; else if(c[0]==‘Q‘)cd[i]=12; else if(c[0]==‘K‘)cd[i]=13; else if(c[0]==‘1‘ && c[1]==‘0‘)cd[i]=10; else cd[i]=c[0]-‘0‘; } if(dfs1(0,0)) cout<<"yes"<<endl; else cout<<"no"<<endl; return 0; }
24點