# 170 条题解

• @ 2013-04-11 10:37:32

AC+1 ^_^

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<iostream>
using namespace std;
double w[100];
bool ok[100];
string s;
void DFS(double sum,int k)
{
if(k==4)
{
if((sum-24)*(sum-24)<0.00000001)
{
printf("1");
exit(0);
}
}
for(int i=1;i<=4;i++)
if(ok[i])
{
double a=sum,b=w[i];
ok[i]=false;
DFS(a+b,k+1);
DFS(a*b,k+1);
DFS(a-b,k+1);
DFS(b-a,k+1);
if(b!=0) DFS(a/b,k+1);
if(a!=0) DFS(b/a,k+1);
ok[i]=true;
}
}
int main()
{
for(int i=1;i<=4;i++)
{
cin>>s;
if(s=="10")
{
w[i]=10;
continue ;
}
if(s[0]=='A') w[i]=1;
if(s[0]<'A') w[i]=s[0]-'0';
if(s[0]=='J') w[i]=11;
if(s[0]=='Q') w[i]=12;
if(s[0]=='K') w[i]=13;

}
memset(ok,true,sizeof(ok));
for(int i=1;i<=4;i++)
{
ok[i]=false;
DFS(w[i],1);
ok[i]=true;

}
printf("0");
}

• @ 2014-07-31 21:42:07

这个代码漂亮

• @ 2018-08-08 10:53:20

1 3 4 6
你的代码输出1
但是应该是0

• @ 2021-01-23 09:17:36
``````#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;

namespace dts
{
typedef long long ll;

const ll size=4,key=24;
ll flag;
vector<ll> a;

void dfs(vector<ll> rec)
{
if (rec.size()>1)
{
vector<ll> num;
num.resize(rec.size()-1);
for (ll i=0;i<rec.size();i++)
for (ll j=i+1;j<rec.size();j++)
{
for (ll k=0,l=0;k<rec.size();k++)
if (k!=i&&k!=j)
num[l++]=rec[k];
num[num.size()-1]=rec[i]+rec[j];
dfs(num);
num[num.size()-1]=rec[i]*rec[j];
dfs(num);
num[num.size()-1]=rec[i]-rec[j];
dfs(num);
num[num.size()-1]=rec[j]-rec[i];
dfs(num);
if (rec[j]!=0)
if (rec[i]%rec[j]==0)
{
num[num.size()-1]=rec[i]/rec[j];
dfs(num);
}
if (rec[i]!=0)
if (rec[j]%rec[i]==0)
{
num[num.size()-1]=rec[j]/rec[i];
dfs(num);
}
}
}
else if (rec[0]==key)
flag=1;
}

void main()
{
a.resize(size);
for (ll i=0;i<size;i++)
{
char s[4];
memset(s,0,sizeof(s));
scanf("%s",s);
if (strcmp(s,"A")==0)
a[i]=1;
else if (strcmp(s,"J")==0)
a[i]=11;
else if (strcmp(s,"Q")==0)
a[i]=12;
else if (strcmp(s,"K")==0)
a[i]=13;
else
sscanf(s,"%lld\n",&a[i]);
}
flag=0;
dfs(a);
printf("%lld\n",flag);
}
};

int main()
{
dts::main();
}
``````
• @ 2018-02-23 20:52:17

PASCAL
DFS
var
a:array[1..4]of integer;
b:array[1..4]of boolean;
s,temp:string;
p,i:longint;
procedure solve(h,f:longint);
var i:longint;
begin
if (f=4) then
begin
if h=24 then
begin
writeln(1);
halt;
end;
end
else
for i:=1 to 4 do
if b[i] then
begin
b[i]:=false;
solve(h+a[i],f+1);
if h>0 then
begin
solve(h-a[i],f+1);
solve(a[i]-h,f+1);
solve(h*a[i],f+1);
if h mod a[i]=0 then
solve(h div a[i],f+1);
if a[i] mod h=0 then
solve(a[i] div h,f+1);
end;
b[i]:=true;
end;
end;
begin
fillchar(b,sizeof(b),#1);
for i:=1 to 4 do
begin
p:=pos(' ',s);
temp:=copy(s,1,p-1);
if temp='A' then a[i]:=1
else if temp='J' then a[i]:=11
else if temp='Q' then a[i]:=12
else if temp='K' then a[i]:=13
else val(temp,a[i]);
delete(s,1,p);
end;
solve(0,0);
writeln(0);
end.

• @ 2017-07-21 15:13:40

//由于没有运算符优先级此题无法按照全排列方式直接解出
//运用递归方法
//此题4个数相互运算无需考虑时间复杂度

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
char s[5];
int a[5],ans;
void dfs(int n,int *t)
{
int c[5];
if (n==1)
{
if (t[1]==24)
{
printf("1");
exit(0); //退出大函数
}
return ;
}
for(int i=1;i<=n;i++)
{
for (int j=1;j<=n;j++)
{
if (i!=j)
{
int x=t[i];
int y=t[j];
if (x<y) swap(x,y);
int cnt=1;
for (int k=1;k<=n;k++)
{
if (k!=i&&k!=j)
{
c[cnt++]=t[k];
}
}
ans=x+y;
c[cnt]=ans;
dfs(n-1,c);//加

ans=x-y;
c[cnt]=ans;
dfs(n-1,c);//减

ans=x*y;
c[cnt]=ans;
dfs(n-1,c);//乘

if (y!=0&&x%y==0)//除
{
ans=x/y;
c[cnt]=ans;
dfs(n-1,c);
}
}
}
}

}
int main()
{
for (int i=1;i<=4;i++)
{
scanf("%s",s);
if (s[1]=='0')
{
a[i]=10;
}

else if(s[0]=='J')
{
a[i]=11;
}
else if (s[0]=='Q')
{
a[i]=12;
}else if (s[0]=='K')
{
a[i]=13;
}else if (s[0]=='A')
{
a[i]=1;
}
else
{
a[i]=s[0]-'0';
}
}
dfs(4,a);
cout<<"0";
return 0;

}

• @ 2022-10-01 16:16:46
1. * 1. * > > *************
``````

```cpp

```cpp

```cpp

```cpp

```cpp

```cpp

```cpp

```cpp

``````
``````
``````
``````
``````
``````
``````
``````
``````
• @ 2021-07-23 08:47:26

上50分代码：

``````#include <bits/stdc++.h>
#define eps 1e-7 //因为实数计算会有误差，但误差范围应该是小于10^-7的。
using namespace std ;
int x[10010] ;//答案，判重
int h,ans ;
int r[10],a[10] ;
double habs(double x){ //好像abs被占用了，忘记那个abs怎么打的了，手打算了。
return x<0?-x:x ;
}
inline void add(int a1,int a2,int a3,int a4,int a5,int a6,int a7){ //后缀表达式形式
x[++h]=(a1|a2<<3|a3<<6|a4<<9|a5<<12|a6<<15|a7<<18);
//因为他说a,b,c,d最大是100，100是三位，那就让一个数上占用三个位置
}
double calc(double x,double y,int f) //f表示符号(+-*/)，相当于 a (f) b
{
if (f==0) return x+y ;
else if (f==1) return x-y ;
else if (f==2) return x*y ;
else return x/y ;
}
inline bool find(int a,int b){
if (a==0) return b>1;
if (a==2) return b<2;
return 1;
}
void check(int a1,int a2,int a3,int a4)
{
for (int i=0;i<=3;i++)
for (int j=0;j<=3;j++)
for (int k=0;k<=3;k++)
{
bool f1=find(k,j),f2=find(j,i);
//一下是5种加括号方式
if (habs( calc ( calc ( calc (a[a1],a[a2],i) ,a[a3],j) ,a[a4],k) -24)<eps)

if (f1 && habs(calc ( calc (a[a1],a[a2],i), calc (a[a3],a[a4],j),k)-24)<eps)

if (f1 && habs( calc (a[a1], calc ( calc (a[a2],a[a3],i),a[a4],j),k)-24)<eps)

if (f2 && habs( calc ( calc (a[a1], calc (a[a2],a[a3],i),j),a[a4],k)-24)<eps)

if (f1 && f2 && habs( calc (a[a1], calc (a[a2], calc (a[a3],a[a4],i),j),k)-24)<eps)
}
}
int main()
{
for (int i=0;i<=3;i++) scanf("%d",&a[i]) ;
for (int i=0;i<=3;i++)
for (int j=0;j<=3;j++)
if (a[i]<a[j]) r[i]++ ;//r数组表示第i个数是第几大的(最大的标号是0)。
for (int i=0;i<=3;i++)
for (int j=0;j<=3;j++)
for (int k=0;k<=3;k++)
for (int l=0;l<=3;l++)
if (i!=j && i!=k && i!=l && j!=k && j!=l && k!=l) //写的有点冗(Rong)余，但是肯定对的
check(i,j,k,l) ;
sort(x+1,x+h+1) ;
if (h) ans=1 ;//如果有值，那ans=1，之后的再判重
for (int i=2;i<=h;i++) if (x[i]!=x[i-1]) ans++ ;
printf("%d",ans) ;
}

``````
• @ 2018-08-08 11:11:03

本题击中了我的弱点，也就是搜索模型的建立。
我写了一个非常模拟的搜索，直接模拟各个位置的运算符和各个数字的顺序，然后处理，这样不仅麻烦，而且我发现只调换顺序不能完全起到括号的效果。

括号的本质就是告诉你本次要选择哪两个数进行运算，因此我们直接枚举组合生成新数，进入到下一个阶段继续dfs就行了
每个阶段各开一个数组

``````#include <bits/stdc++.h>
using namespace std;
#define FOR(i,n) for (int i=1;i<=n;i++)
#define REP(i,a,b) for (int i=a;i<=b;i++)
#define pb push_back
#define mp make_pair
#define ll long long
const int N=10000+10;
const int inf=0x3f3f3f3f;
const ll mod=7654321;
const double PI=3.1415926;
const double eps=1e-8;

char ch;
int a[5];
int b[5][5];
int ans;
void dfs(int n,int a[]) {
if (n==1) {
if (a[1]==24) ans=1;
return;
}
FOR(i,n) FOR(j,n) if (i!=j) {
int cnt;
cnt=0;
FOR(k,n) if (k!=i&&k!=j) b[n-1][++cnt]=a[k];
b[n-1][++cnt]=a[i]+a[j];
dfs(n-1,b[n-1]);
if (ans) return;
cnt=0;
FOR(k,n) if (k!=i&&k!=j) b[n-1][++cnt]=a[k];
b[n-1][++cnt]=a[i]-a[j];
dfs(n-1,b[n-1]);
if (ans) return;
cnt=0;
FOR(k,n) if (k!=i&&k!=j) b[n-1][++cnt]=a[k];
b[n-1][++cnt]=a[i]*a[j];
dfs(n-1,b[n-1]);
if (ans) return;
if (a[j]==0||a[i]%a[j]) continue;
cnt=0;
FOR(k,n) if (k!=i&&k!=j) b[n-1][++cnt]=a[k];
b[n-1][++cnt]=a[i]/a[j];
dfs(n-1,b[n-1]);
if (ans) return;
}
}
int main() {
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
FOR(i,4) {
string ch;
cin>>ch;
if (ch=="J") a[i]=11;
else if (ch=="Q") a[i]=12;
else if (ch=="K") a[i]=13;
else if (ch=="A") a[i]=1;
else if (ch=="10") a[i]=10;
else a[i]=ch[0]-'0';
}
FOR(i,4) b[4][i]=a[i];
dfs(4,b[4]);
cout<<ans<<endl;
return 0;
}
``````
• @ 2017-04-11 16:27:25

dfs！dfs！

program ex24d;
var
a:array[1..4] of integer;
b:array[1..4] of boolean;
temp:string;
s:string;
sum,n,m,i,j,k:longint;
procedure solve(h,f:integer);
var
i:longint;
begin
if (h=24) and (f=4) then
begin
writeln(1);
halt;

end;
for i:=1 to 4 do
begin
if b[i] then
begin
b[i]:=false;

solve(h+a[i],f+1);
b[i]:=true;

end;
if (b[i]) and (h>0) then
begin
b[i]:=false;

solve(h-a[i],f+1);
b[i]:=true;

end;
if (b[i]) and (h>0) then
begin
b[i]:=false;
solve(a[i]-h,f+1);
b[i]:=true;
end;
if (b[i]) and (h>0) then
begin
b[i]:=false;

solve(h*a[i],f+1);
b[i]:=true;

end;
if (b[i]) and (h>0) and (h mod a[i]=0) then
begin
b[i]:=false;

solve(h div a[i],f+1);
b[i]:=true;

end;
if (b[i]) and (h<>0) and (a[i] mod h=0) then
begin
b[i]:=false;
solve(a[i] div h,f+1);
b[i]:=true;
end;

end;
end;
begin
s:=s+' ';
for i:=1 to 4 do
begin
temp:=copy(s,1,pos(' ',s)-1);
if temp='A' then a[i]:=1
else if temp='J' then a[i]:=11
else if temp='Q' then a[i]:=12
else if temp='K' then a[i]:=13
else val(temp,a[i]);
delete(s,1,pos(' ',s));
end;
fillchar(b,sizeof(b),true);
solve(0,0);
writeln('0');

end

• @ 2017-02-08 09:04:38

...
我也就不说啥了

测试数据 #0: Accepted, time = 1 ms, mem = 768 KiB, score = 10
测试数据 #1: Accepted, time = 1 ms, mem = 763 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 768 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 768 KiB, score = 10
测试数据 #4: Accepted, time = 0 ms, mem = 764 KiB, score = 10
测试数据 #5: Accepted, time = 1 ms, mem = 764 KiB, score = 10
测试数据 #6: Accepted, time = 1 ms, mem = 764 KiB, score = 10
测试数据 #7: Accepted, time = 3 ms, mem = 768 KiB, score = 10
测试数据 #8: Accepted, time = 0 ms, mem = 764 KiB, score = 10
测试数据 #9: Accepted, time = 2 ms, mem = 768 KiB, score = 10
Accepted, time = 34 ms, mem = 768 KiB, score = 100
代码
```Pascal Var A:integer; Begin Randomize; A:=Random(2); Writeln(A); End. ```

• @ 2016-12-14 09:18:58

这是正确的题解
```c++
#include <bits/stdc++.h>
using namespace std;

typedef int arr[4];
bool flag;
arr d;
int r[100][100];
void dfs(int n,arr d)
{
int a,b,i,j,k,p,t;
arr c;
if (n==1 && d[0]==24) {
cout<<1<<endl;
exit(0) ;
}
else
for (i=0;i<n-1;i++)
for (j=i+1;j<n;j++){
a=d[i] ; b= d[j];
if (a<b) swap(a,b);
t=0;
for (k=0;k<n;k++)
if (k!=i && k!=j) c[t++]=d[k];
r[4-n][0]=a;
r[4-n][2]=b;
r[4-n][3]=-1;

for ( p=0;p<4;p++){
switch(p){
case 0:r[4-n][3]=a+b; break;
case 1:r[4-n][3]=a-b; break;
case 2:r[4-n][3]=a*b; break;
case 3: if (b!=0) r[4-n][3]=a/b;break;
}
if (p==3 && b==0) break;
if (p==3 && a % b!=0) break;
r[4-n][1]=p;
c[t]=r[4-n][3];
dfs(n-1,c);
}
}

}

int main()
{
int i;
char hehe[3];
for (i=0;i<4;i++) {
cin>>hehe;
if(strlen(hehe)>1) d[i]=10;
else if(hehe[0]=='J') d[i]=11;
else if(hehe[0]=='Q') d[i]=12;
else if(hehe[0]=='K') d[i]=13;
else if(hehe[0]=='A') d[i]=1;
else d[i]=hehe[0]-'0';
}

dfs(4,d);
cout<<"0"<<endl;
return 0;
}
```

• @ 2015-10-04 19:27:37

###貌似还是错的啊~~~

P113424点游戏Accepted
记录信息
评测状态 Accepted
题目 P1134 24点游戏
递交时间 2015-10-04 19:26:24
代码语言 C++
评测机 VijosEx
消耗时间 84 ms
消耗内存 1472 KiB
评测时间 2015-10-04 19:26:25
评测结果
编译成功

foo.cpp: In function 'int main()':
foo.cpp:78:12: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
if(x!=0&&(double)y/x==24||y!=0&&(double)x/y==24)ans=1;
^
测试数据 #0: Accepted, time = 0 ms, mem = 1464 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 1464 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 1468 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 1468 KiB, score = 10
测试数据 #4: Accepted, time = 15 ms, mem = 1464 KiB, score = 10
测试数据 #5: Accepted, time = 15 ms, mem = 1464 KiB, score = 10
测试数据 #6: Accepted, time = 12 ms, mem = 1464 KiB, score = 10
测试数据 #7: Accepted, time = 12 ms, mem = 1460 KiB, score = 10
测试数据 #8: Accepted, time = 15 ms, mem = 1464 KiB, score = 10
测试数据 #9: Accepted, time = 15 ms, mem = 1472 KiB, score = 10
Accepted, time = 84 ms, mem = 1472 KiB, score = 100
代码
#include <iostream>
#include <cstdio>
#include <math.h>
#include <string.h>
#include <string>
using namespace std;
bool used[5];
int a[5];
bool mem[1000];
double qiku[120][1001];
int ans;
void work(double now,int k)
{
if(k==5){
if(now==24)ans=1;
return;
}
int tot=0;
if(k==3){
for(int i=1;i<=4;i++){
if(used[i]==true)tot+=1<<(i-1);
}
int s=++qiku[tot][0];
qiku[tot][s]=now;
}

if(ans==1)return;
for(int i=1;i<=4;i++){
if(used[i])continue;
used[i]=true;
work(now+a[i],k+1);
work(now-a[i],k+1);
work(a[i]-now,k+1);
work(now*a[i],k+1);
if(now!=0)
work(a[i]/now,k+1);
if(a[i]!=0)
work(now/a[i],k+1);
used[i]=false;
}
}
int main()
{
string s;
for(int i=1;i<=4;i++)
{
cin>>s;
if(s=="10")
{a[i]=10;continue;}
if(s[0]=='A') a[i]=1;
if(s[0]<'A') a[i]=s[0]-'0';
if(s[0]=='J') a[i]=11;
if(s[0]=='Q') a[i]=12;
if(s[0]=='K') a[i]=13;
}
ans=0;
memset(used,0,sizeof(used));
memset(qiku,0,sizeof(0));
for(int i=1;i<=4;i++)
{
used[i]=true;
work(a[i],2);
work(-a[i],2);
used[i]=false;
}
if(!ans)
for(int i=1;i<=1;i+=2)
for(int j=2;j<=4;j+=2){
int tot=(1<<(i-1))+(1<<(j-1));
int b=15-tot;
for(int k=1;k<=qiku[tot][0];k++)
{
double x=qiku[tot][k];
for(int l=1;l<=qiku[b][0];l++)
{
double y=qiku[b][l];
if(x+y==24||x-y==24||y-x==24||x*y==24)ans=1;
if(x!=0&&(double)y/x==24||y!=0&&(double)x/y==24)ans=1;
}
}
}
if(ans)
printf("1\n");
else printf("0\n");
}

• @ 2015-08-04 19:59:31

暴力； 对了八个点； 求大牛改错；

program exam;
var i,j,m,n,k,l:longint;
s:ansistring;
a,b,c:string;
e,d:array[1..4] of longint;
f:array[0..4] of boolean;
function jia(k,x,y:longint):double;
begin
if k=1 then exit(x-y);
if k=2 then exit(x+y);
if k=3 then exit(x*y);
if (k=4) and (y<>0) then exit(x/y);
end;
procedure dfs(k:longint);
var i,x,y:longint;
begin
if k>4 then
begin
for x:=1 to 4 do
for y:=1 to 4 do
begin
if (jia(x,d[1],d[2]))*(jia(y,d[3],d[4]))=24 then begin writeln(1); halt; end;
if (jia(x,d[1],d[2]))-(jia(y,d[3],d[4]))=24 then begin writeln(1); halt; end;
if (jia(x,d[1],d[2]))+(jia(y,d[3],d[4]))=24 then begin writeln(1); halt; end;
if (jia(y,d[3],d[4])<>0) and ((jia(x,d[1],d[2]))/(jia(y,d[3],d[4]))=24) then begin writeln(1); halt; end;
end;
end;
for i:=1 to 4 do
if f[i] then
begin
d[k]:=e[i];
f[i]:=false;
dfs(k+1);
f[i]:=true;
end;
end;

begin
fillchar(f,sizeof(f),true);
a:=copy(s,1,pos(' ',s)-1);
delete(s,1,pos(' ',s));
b:=copy(s,1,pos(' ',s)-1);
delete(s,1,pos(' ',s));
c:=copy(s,1,pos(' ',s)-1);
delete(s,1,pos(' ',s));
if a='J' then e[1]:=11 else
if a='Q' then e[1]:=12 else
if a='K' then e[1]:=13 else
val(a,e[1]);
if b='J' then e[2]:=11 else
if b='Q' then e[2]:=12 else
if b='K' then e[2]:=13 else
val(b,e[2]);
if c='J' then e[3]:=11 else
if c='Q' then e[3]:=12 else
if c='K' then e[3]:=13 else
val(c,e[3]);
if s='J' then e[4]:=11 else
if s='Q' then e[4]:=12 else
if s='K' then e[4]:=13 else
val(s,e[4]);
dfs(1);
writeln(0);
end.

• @ 2015-08-04 20:09:09

jfklawehtgioywo[rg sd,ng l
u grweg nsd

• @ 2015-08-04 19:54:57

哈哈哈 RP ~~~~~~~
测试数据 #0: Accepted, time = 0 ms, mem = 768 KiB, score = 10
测试数据 #1: Accepted, time = 0 ms, mem = 764 KiB, score = 10
测试数据 #2: Accepted, time = 0 ms, mem = 768 KiB, score = 10
测试数据 #3: Accepted, time = 0 ms, mem = 768 KiB, score = 10
测试数据 #4: Accepted, time = 1 ms, mem = 764 KiB, score = 10
测试数据 #5: Accepted, time = 3 ms, mem = 764 KiB, score = 10
测试数据 #6: Accepted, time = 1 ms, mem = 764 KiB, score = 10
测试数据 #7: Accepted, time = 2 ms, mem = 760 KiB, score = 10
测试数据 #8: Accepted, time = 0 ms, mem = 764 KiB, score = 10
测试数据 #9: Accepted, time = 27 ms, mem = 768 KiB, score = 10
Accepted, time = 34 ms, mem = 768 KiB, score = 100
代码
var a:integer;
begin
randomize;
a:=random(2);
if a=1 then writeln(a)
else writeln('0');
end.

• @ 2015-11-14 11:30:44

666666

• @ 2016-05-18 19:57:27

6666666

• @ 2016-08-12 18:34:00

6666666666

• @ 2016-11-17 19:39:31

666666

• @ 2017-02-08 08:57:47

800/120等于多少啊~

• @ 2015-08-01 15:45:34

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define random(x) (rand()%x)
using namespace std;
int main()
{
int st;
while(1)
{
st=random(2);
if(st==1)
{
printf("1");
return 0;
}
if(st==2)
{
printf("0");
return 0;
}
}
return 0;
}

• @ 2015-08-26 07:49:27

???怎么不懂？？

• @ 2015-08-26 07:50:53

貌似过不了啊

• @ 2015-12-10 19:30:54

传说中的随机化算法？

• @ 2015-08-01 15:45:28

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define random(x) (rand()%x)
using namespace std;
int main()
{
int st;
while(1)
{
st=random(2);
if(st==1)
{
printf("1");
return 0;
}
if(st==2)
{
printf("0");
return 0;
}
}
return 0;
}

• @ 2015-08-01 15:40:38

var a:integer;
begin
randomize;
a:=random(2);
if a=1 then writeln(a)
else writeln('0');
end.

• @ 2014-10-24 16:08:35

flag = 0
use = [0 for x in range(7)]
num = [0 for x in range(7)]
char = [" " for x in range(7)]
def dfs(nowsum,step):
global flag
if flag == 1:return
if step == 4 and (nowsum - 24)**2 < 0.000001:
flag = 1
print "1"
return
for i in range(0,4):
if use[i] == 0:
use[i] = 1
dfs(nowsum+num[i],step+1)
dfs(nowsum-num[i],step+1)
dfs(num[i]-nowsum,step+1)
dfs(num[i]*nowsum,step+1)
dfs(nowsum/num[i],step+1)
if nowsum != 0: dfs(num[i]/nowsum,step+1)
use[i] = 0
return

char = [str(x) for x in raw_input().split()]
for i in range(0,4):
if char[i] == 'A': num[i] = 1
elif char[i] == 'J': num[i] = 11
elif char[i] == 'Q': num[i] = 12
elif char[i] == 'K': num[i] = 13
else: num[i] = int(char[i])
for i in range(0,4):
if use[i] == 0:
use[i] = 1
dfs(float(num[i]),1)
use[i] = 0
if flag == 0 : print "0"

• @ 2014-08-18 09:33:12

begin
write('0');
end.
可以先看出几个1几个0，然后在编出来
var a:longint;
begin
randomize；
a:=random(2);
writeln(a);
end.

• @ 2014-08-02 21:56:56

###
#include <iostream>
#include <string>
using namespace std;
int num[4];
bool ok[4];
int flag=0;
int main()
{
for(int i=0;i!=4;++i)
ok[i]=true;
string t1,t2,t3,t4;
cin>>t1>>t2>>t3>>t4;
int change(string &);
num[0]=change(t1);
num[1]=change(t2);
num[2]=change(t3);
num[3]=change(t4);
void dfs(double,int);
for(int i=0;i!=4;++i)
{ ok[i]=false;
dfs((double)num[i],1);
ok[i]=true;
}
if(!flag) cout<<0<<endl;
return 0;
}

int change(string &n)
{
if(n=="10") return 10;
if(n[0]=='A') return 1;
if(n[0]=='J') return 11;
if(n[0]=='Q') return 12;
if(n[0]=='K') return 13;
if(n[0]<'A') return n[0]-'0';
}

void dfs(double sum,int k)
{ if(flag==1) return;
if(k==4&&(sum-24)*(sum-24)<=0.000001)
{ cout<<1<<endl;
flag=1;
return;
}

for(int i=0;i!=4;++i)
{
if(ok[i])
{
ok[i]=false;
dfs(sum+num[i],k+1);
dfs(sum-num[i],k+1);
dfs(sum*num[i],k+1);
dfs(num[i]-sum,k+1);
if(num[i]!=0) dfs(sum/num[i],k+1);
if(sum!=0) dfs(num[i]/sum,k+1);
ok[i]=true;

}

}
}

• @ 2014-08-02 21:55:30

### code

#include <iostream>
#include <string>
using namespace std;
int num[4];
bool ok[4];
int flag=0;
int main()
{
for(int i=0;i!=4;++i)
ok[i]=true;
string t1,t2,t3,t4;
cin>>t1>>t2>>t3>>t4;
int change(string &);
num[0]=change(t1);
num[1]=change(t2);
num[2]=change(t3);
num[3]=change(t4);
void dfs(double,int);
for(int i=0;i!=4;++i)
{ ok[i]=false;
dfs((double)num[i],1);
ok[i]=true;
}
if(!flag) cout<<0<<endl;
return 0;
}

int change(string &n)
{
if(n=="10") return 10;
if(n[0]=='A') return 1;
if(n[0]=='J') return 11;
if(n[0]=='Q') return 12;
if(n[0]=='K') return 13;
if(n[0]<'A') return n[0]-'0';
}

void dfs(double sum,int k)
{ if(flag==1) return;
if(k==4&&(sum-24)*(sum-24)<=0.000001)
{ cout<<1<<endl;
flag=1;
return;
}

for(int i=0;i!=4;++i)
{
if(ok[i])
{
ok[i]=false;
dfs(sum+num[i],k+1);
dfs(sum-num[i],k+1);
dfs(sum*num[i],k+1);
dfs(num[i]-sum,k+1);
if(num[i]!=0) dfs(sum/num[i],k+1);
if(sum!=0) dfs(num[i]/sum,k+1);
ok[i]=true;

}

}
}

小心int的精度损失

ID
1134

8

(无)

9082

852

9%

7