LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
楼主: scream

如何用c写一个算24的小游戏

[复制链接]
发表于 2002-8-30 14:36:56 | 显示全部楼层
咕嘟,此问题打住,不需要讨论了,我是最近闲的没事。就是知道算法又能如何?我也懒得再回贴了。
发表于 2002-11-4 18:19:50 | 显示全部楼层
fisrt i want to say ,i am in linux ,and my english is poor,but i can't use Chinese!
i think a problem mustn't only have one method,adn pepole have the method to solve the same problem.

fisrt ,i want to say ,it's that ,it's better that you have work out the problem!  Congratulations!!!

Second, we are friends, unite and friendship,is the most important!!!

we are the best!

Thank you!!!
发表于 2002-11-5 01:54:22 | 显示全部楼层
我觉得算法的可能性肯定有遗漏,而且这种罗列的办法比较傻
发表于 2002-11-5 11:53:37 | 显示全部楼层

同志们,我有源码

以下程序是97年(如果我没有记错的话)高程试题,我让单位打字员录入的,未调试。写得不错,谁调试后给我一份。

#include <stdio.h>

int calculate(int**ipt[],char*s);
int search(int a[],int k);


int op[]={'+','-','*','/'};
char*sample[]={"ABCDxyz","ABCxDyz",
              "ABCxyDz","AbxCyDz","AbxCDyz"};
char*format[]={
"%d%c(%d%c(%d%c%d))\n",
"%d%c((%d%c%d)%c%d)\n",
"(%d%c(%d%c%d))%c%d\n",
"((%d%c%d)%c%d)%c%d\n",
"(%d%c%d)%c(%d%c%d)\n"};


main()
{int a[4];
printf("Enter four number!\n");
scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3]);
if(search(a,24)==0)printf("No SOLUTION!!\n");
}


int search(int a[],int k)
{int m;
static int*i1,*i2,*i3,*i4,*s1,*s2,*s3;
int **input[7]={&i1,&i2,&i3,&i4,&s1,&s2,&s3};
int **rule[][7]={{&i1,&s3,&i2,&s2,&i3,&s1,&i4},
{&i1,&s3,&i2,&s1,&i3,&s2,&i4},
                 {&i1,&s2,&i2,&s1,&i3,&s3,&i4},
                 {&i1,&s1,&i2,&s2,&i3,&s3,&i4},
                 {&i1,&s2,&i2,&s3,&i3,&s2,&i4}};
    for(i1=a;i1<a+4;i1++)
    {for (i2=a;i1<a+4;i2++)
        {if (i2==i1)continue;
        for(i3=a;i3<a+4;i3++)
        {if(i3==i1||i3==i2)continue;
        i4=a+6-(i1-a)-(i2-a)-(i3-a);
        for (s1=op;s1<op+4;s1++)
        for (s2=op;s1<op+4;s2++)
        for (s3=op;s1<op+4;s3++)
        for(m=0;m<5;m++)
        if (calculate(input,sample[m])==k)
        {printf(format[m],
                **rule[m][0],**rule[m][1],
                **rule[m][2],**rule[m][3],
                **rule[m][4],**rule[m][5],
                **rule[m][6]);
                return 1;
        }}}}return 0;}


        int calculate(int**ipt[],char*s)
        {int st[4],i,j,r1,r2;
        for (j=i=0;i<7;i++)
        {if (s=='x') st[j++]=**ipt[s-'A'];
        else{r1=st[j-2];r2=st[j-1];
        switch (**ipt[s-'x'+4])
        {
                case '+':st[j-2]=r1+r2;j--;break;
                case '-':if (r1<r2)return -1;
                        st[j-2]=r1-r2;j--;break;
                case '*':st[j-2]=r1*r2;j--;break;
                case '/':if (r2==0||r1%r2!=0)return -1;
                        st[j-2]=r1/r2;j--;break;
}}}
return *st;
}
发表于 2002-11-5 13:05:33 | 显示全部楼层

精神可嘉!!!

好多地方还可以优化,比如可以用个循环,把4个数进行论转,这样可以少写好多行。
发表于 2002-11-5 18:05:37 | 显示全部楼层

看了斑竹的程序就。。。。。。。。

我觉得编程序是想好算法再编
斑竹的程序我实在不敢恭维

太夸张了。。。。。。。。。。

:confused:
发表于 2002-11-6 20:02:19 | 显示全部楼层
兄弟,那么多的if语句我发觉有点不妥哦!
如果用for的嵌套+指针来实现,我觉得会好点,
可以避免遗漏。
发表于 2002-11-22 15:36:40 | 显示全部楼层

地~

列出所有情况不太好。。
发表于 2002-12-5 14:24:19 | 显示全部楼层
请问while(1) 死循环的作用是什么?
发表于 2003-3-16 00:06:15 | 显示全部楼层
建议只对程序本身讨论,无关的话不要说,j兄的话我很赞同,有学习气氛的站点太少了,很多问题只停留在表面,不深入的学习,建议大家多对程序讨论。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表