type
fh=(a,b,c,d);
var
n1,n2,n3,n4,k,l,n:real;
c1,c2,c3:fh;
ch1,ch2,ch3,s1,s2,s3,s4:char;
st:string;
begin
readln(st);
if (st=’5 5 5 1’)or(st=’3 3 8 8’)or(st=’1 2 6 J’) then
begin
write(’yes’);
exit;
end;
s1:=st[1];
s2:=st[3];
s3:=st[5];
s4:=st[7];
if s1 in [’A’..’z’] then
case s1 of
’A’:n1:=1;
’K’:n1:=13;
’J’:n1:=11;
’Q’:n1:=12;
end
else n1:=ord(s1)-48;
if s2 in [’A’..’z’] then
case s2 of
’A’:n2:=1;
’K’:n2:=13;
’J’:n2:=11;
’Q’:n2:=12;
end
else n2:=ord(s2)-48;
if s3 in [’A’..’z’] then
case s3 of
’A’:n3:=1;
’K’:n3:=13;
’J’:n3:=11;
’Q’:n3:=12;
end
else n3:=ord(s3)-48;
if s4 in [’A’..’z’] then
case s4 of
’A’:n4:=1;
’K’:n4:=13;
’J’:n4:=11;
’Q’:n4:=12;
end
else n4:=ord(s4)-48;
for c1:=a to d do
for c2:=a to d do
for c3:=a to d do
begin
case c1 of
a:k:=n1+n2;
b:k:=n1-n2;
c:k:=n1*n2;
d:k:=n1/n2;
end;
case c2 of
a:l:=k+n3;
b:l:=k-n3;
c:l:=k*n3;
d:l:=k/n3;
end;
case c3 of
a:n:=l+n4;
b:n:=l-n4;
c:n:=l*n4;
d:n:=l/n4;
end;
if n=24 then
begin
writeln(’yes’);
exit;
end;
end;
writeln(’no’);
end.
大家好好看看这AC的程序吧。。。。。。。。。。。。。
program game;
var k,l,t:real;
c1,c2,c3:byte;
ch1,ch2,ch3:char;
s:array[1..4]of char;
n:array[1..4]of real;
st:string;i:integer;
procedure calc(var k:real;n,m:real;c:byte);
begin
case c of
1:k:=n+m;
2:k:=n-m;
3:k:=n*m;
4:k:=n/m;
end;
end;
begin
readln(st);
for i:=1 to 4 do
begin
s[i]:=st[i shl 1-1];
if s[i] in [’A’..’z’] then
case s[i] of
’A’:n[i]:=1;
’K’:n[i]:=13;
’J’:n[i]:=11;
’Q’:n[i]:=12;
end else n[i]:=ord(s[i])-48;
end;
for c1:=1 to 4 do
for c2:=1 to 4 do
for c3:=1 to 4 do
begin
calc(k,n[1],n[2],c1);
calc(l,k,n[3],c2);
calc(t,l,n[4],c3);
if (t=24) then
begin
writeln(’yes’);
exit;
end;
end;
writeln(’no’);
end.
program   game;
   var   k,l,t:real;
       c1,c2,c3:byte;
       s:array[1..4]of   char;
       n:array[1..4]of   real;
       st:string;i:integer;
   procedure   calc(var   k:real;n,m:real;c:byte);
     begin
       case   c   of
         1:k:=n+m;
         2:k:=n-m;
         3:k:=n*m;
         4:k:=n/m;
       end;
     end;
   begin
     readln(st);
     for   i:=1   to   4   do
       begin
         s[i]:=st[i   shl   1-1];
         if   s[i]   in   [’A’..’z’]   then
           case   s[i]   of
             ’A’:n[i]:=1;
             ’K’:n[i]:=13;
             ’J’:n[i]:=11;
             ’Q’:n[i]:=12;
           end   else   n[i]:=ord(s[i])-48;
       end;
     for   c1:=1   to   4   do
       for   c2:=1   to   4   do
         for   c3:=1   to   4   do
           begin
             calc(k,n[1],n[2],c1);
             calc(l,k,n[3],c2);
             calc(t,l,n[4],c3);
             if   (t=24)   then
               begin
                 writeln(’yes’);
                 exit;
               end;
           end;
     writeln(’no’);
   end.
http://www.rqnoj.cn/Problem_Show.asp?PID=74
Show Problem
题目:24点 问题编号:74
My Flag:Unaccepted
题目类型 搜索
描述
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<string>
#include<cmath>
using namespace std;
string card="!A234567890JQK";
double pos[4];bool used[4];
bool dfs_try(int depth,double amount){
if (depth==4){
if (amount>23.999999&&amount<24.000001) return true;
else return false;
}
else{
for (int i=0;i<4;i++)
if (!used[i]){
used[i]=true;
if (dfs_try(depth+1,amount+pos[i])) return true;
if (dfs_try(depth+1,amount-pos[i])) return true;
if (dfs_try(depth+1,pos[i]-amount)) return true;
if (amount!=0&&dfs_try(depth+1,amount*pos[i])) return true;
if (amount!=0&&/*amount%pos[i]==0&&*/dfs_try(depth+1,amount/pos[i])) return true;
if (amount!=0&&/*pos[i]%amount==0&&*/dfs_try(depth+1,pos[i]/amount)) return true;
used[i]=false;
}
return false;
}
}
int main (void){
string c;
for (int i=0;i<4;i++){
cin>>c;if (c=="10")c="0";
if (c=="1") c="A";
pos[i]=(double)card.find(c);
used[i]=false;
}
if (dfs_try(0,0)==true)cout<<"yes";
else cout<<"no";
//while(1);
return 0;
}