题解

167 条题解

  • 3
    @ 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

  • 1
    @ 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
    readln(s);s:=s+' ';
    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.

  • 0
    @ 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;
    }
    
  • 0
    @ 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;

    }

  • 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
    readln(s);
    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

  • 0
    @ 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.

  • 0
    @ 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;
    }
    ```

  • 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");
    }

  • 0
    @ 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);
    readln(s);
    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

  • 0
    @ 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.

  • 0
    @ 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

      传说中的随机化算法?

  • 0
    @ 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;
    }

  • 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.

  • 0
    @ 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"

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

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

  • 0
    @ 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;

    }

    }
    }

  • 0
    @ 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的精度损失

  • 0
    @ 2014-08-02 21:53:57

    ###
    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导致的精度损失

  • 0
    @ 2014-01-22 20:50:01

    特地看了一下教程,然后弄成了这样,虽然不是太好,但还是略尽人意。
    再次申明:该程序并不是我自己写的,而且也无法AC这道题目,只是提供一些思路,提供一个模版罢了。

    type
    arr=array[1..4] of integer;
    var
    i,n,len,g:integer;
    d:arr;
    r:array[1..3,1..4] of integer;

    procedure print;
    var
    i,j:integer;
    begin
    for i:=1 to 3 do
    begin
    for j:=1 to 3 do
    if j<>2 then write(r[i,j])
    else
    case r[i,j] of
    1:write('+');
    2:write('-');
    3:write('*');
    4:write('/');
    end;
    writeln('=',r[i,4]);
    end;
    halt;
    end;

    procedure try(k:integer;d:arr);
    var
    a,b,i,j,l,t:integer;
    e:arr;
    begin
    if k=1 then
    begin
    if d[1]=24 then print;
    exit;
    end;
    for i:=1 to k-1 do
    for j:=i+1 to k do
    begin
    a:=d[i];
    b:=d[j];
    if a<b then
    begin
    t:=a;
    a:=b;
    b:=t;
    end;
    t:=0;
    for l:=1 to k do
    if (l<>i) and (l<>j) then
    begin
    inc(t);
    e[t]:=d[l];
    end;
    r[5-k,1]:=a;
    r[5-k,3]:=b;
    r[5-k,4]:=-1;
    for l:=1 to 4 do
    begin
    case l of
    1:r[5-k,4]:=a+b;
    2:r[5-k,4]:=a-b;
    3:r[5-k,4]:=a*b;
    4:if b<>0 then
    if a mod b=0 then
    r[5-k,4]:=a div b;
    end;
    r[5-k,2]:=l;
    if r[5-k,4]<>-1 then
    begin
    e[t+1]:=r[5-k,4];
    try(k-1,e);
    end;
    end;
    end;
    end;

    begin
    for i:=1 to 4 do read(d[i]);
    try(4,d);
    writeln('No answer!');
    end.

  • 0
    @ 2014-01-22 20:41:30

    感觉Naylon的说法有问题,如果是从左到右依次尝试的话,有一些情况是全排列之后也过不了的。设四个数为x、y、z、w,如果x*y+z*w=24,那么按照他的这种办法算出来的就是(x*y+z)*w,就无法AC了,不过估计数据太弱,没有出现这种情况。下面附上另一份24点程序,只要将输入和输出改一下就可以A掉这道题目了。
    type
    arr=array[1..4] of integer;
    var
    i,n,len,g:integer;
    d:arr;
    r:array[1..3,1..4] of integer;

    procedure print;
    var
    i,j:integer;
    begin
    for i:=1 to 3 do
    begin
    for j:=1 to 3 do
    if j<>2 then write(r[i,j])
    else
    case r[i,j] of
    1:write('+');
    2:write('-');
    3:write('*');
    4:write('/');
    end;
    writeln('=',r[i,4]);
    end;
    halt;
    end;

    procedure try(k:integer;d:arr);
    var
    a,b,i,j,l,t:integer;
    e:arr;
    begin
    if k=1 then
    begin
    if d[1]=24 then print;
    exit;
    end;
    for i:=1 to k-1 do
    for j:=i+1 to k do
    begin
    a:=d[i];
    b:=d[j];
    if a<b then
    begin
    t:=a;
    a:=b;
    b:=t;
    end;
    t:=0;
    for l:=1 to k do
    if (l<>i) and (l<>j) then
    begin
    inc(t);
    e[t]:=d[l];
    end;
    r[5-k,1]:=a;
    r[5-k,3]:=b;
    r[5-k,4]:=-1;
    for l:=1 to 4 do
    begin
    case l of
    1:r[5-k,4]:=a+b;
    2:r[5-k,4]:=a-b;
    3:r[5-k,4]:=a*b;
    4:if b<>0 then
    if a mod b=0 then
    r[5-k,4]:=a div b;
    end;
    r[5-k,2]:=l;
    if r[5-k,4]<>-1 then
    begin
    e[t+1]:=r[5-k,4];
    try(k-1,e);
    end;
    end;
    end;
    end;

    begin
    for i:=1 to 4 do read(d[i]);
    try(4,d);
    writeln('No answer!');
    end.
    //注:该程序不能A掉这道题目,但是可以算出24点,并且可以输出过程,唯一的要求就是读入必须是整数,想要AC掉这道题目必须要对读入和输出进行修改。(另:该程序并非由我原创,而是来自于一本教材)

    • @ 2014-01-22 20:42:16

      好吧,没有想到粘上去后就成了这个样子,只能麻烦大家自己根据分号来换行了。

信息

ID
1134
难度
8
分类
搜索 点击显示
标签
(无)
递交数
8494
已通过
760
通过率
9%
上传者