HDU-1427-速算24點
4個數通過 +,—,*,/和加括號,計算得24,
枚舉數字和運算符,DFS即可,注意題目要求計算過程中都不能出現小數,所以做除法時稍作處理
枚舉數組可用algorithm裡的next_permutation
The next_permutation() function attempts to transform the given range of elements [start,end) into the next lexicographically greater permutation of elements. If it succeeds, it returns true, otherwise, it returns false.
[cpp]
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
int flag;
int num[4];
int cmp(const void *a,const void *b)
{
return *(int *)a-*(int *)b;
}
void dfs(int sum,int cur,int m)
{
if(flag)
return;
if(m==3)
{
if(sum+cur==24||sum-cur==24||sum*cur==24)
flag=1;
if(cur!=0&&sum%cur==0&&sum/cur==24)
flag=1;
return;
}
dfs(sum+cur,num[m+1],m+1); //先計算前一部分
dfs(sum-cur,num[m+1],m+1);
dfs(sum*cur,num[m+1],m+1);
if(cur!=0&&sum%cur==0)
dfs(sum/cur,num[m+1],m+1);
dfs(sum,cur+num[m+1],m+1); //先計算後一部分,相當於加括號
dfs(sum,cur-num[m+1],m+1);
dfs(sum,cur*num[m+1],m+1);
if(num[m+1]!=0&&cur%num[m+1]==0)
dfs(sum,cur/num[m+1],m+1);
}
int main()
{
int i;
char str[5];
while(scanf("%s",str)!=EOF)
{
if(strlen(str)==2)
num[0]=10;
else
{
if(str[0]=='A')
num[0]=1;
else if(str[0]=='J')
num[0]=11;
else if(str[0]=='Q')
num[0]=12;
else if(str[0]=='K')
num[0]=13;
else
num[0]=str[0]-'0';
}
for(i=1;i<=3;i++)
{
scanf("%s",str);
if(strlen(str)==2)
num[i]=10;
else
{
if(str[0]=='A')
num[i]=1;
else if(str[0]=='J')
num[i]=11;
else if(str[0]=='Q')
num[i]=12;
else if(str[0]=='K')
num[i]=13;
else
num[i]=str[0]-'0';
}
}
qsort(num,4,sizeof(num[0]),cmp);
flag=0;
do
{
dfs(num[0],num[1],1);
}while(next_permutation(num,num+4)&&!flag);
if(flag)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}