| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1478 人关注过本帖
标题:一个写的24点,可是输出得到的结果不怎么对,有兴趣的大神帮看看一下呗,实 ...
只看楼主 加入收藏
dd7403
Rank: 1
等 级:新手上路
帖 子:12
专家分:0
注 册:2016-4-27
结帖率:100%
收藏
已结贴  问题点数:18 回复次数:2 
一个写的24点,可是输出得到的结果不怎么对,有兴趣的大神帮看看一下呗,实在谢谢了
程序有些长   各位大神拜托了
#include<math.h>
#include<string.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#define MAX 4;/*最大输入个数*/
int k=0;/*全局变量 判定是否有解*/
char tp[4]={0},tpt[2000]={0},tptt[2000]={0},tre[2000]={0};/*用来消除重复与记录表达式中的运算符*/
char op[4]={'+','-','*','/'};
void count(float a[],char tem[][25],int n);/*a[]中存放输入的4个数,tem[][25]中存放计算的步骤,n是a[]中元素的个数*/
int q=0,p=0,e=0,w=0,t=0,r=0,g=0,h=0,v=0,u=0,tu=0,te=0;/*用于消除重复*/
float group[2]={0},flow[20]={0},flo[20]={0};
void count(float a[],char tem[][25],int n)
{
    float b[4];
    char tm[4][25];/*b[4]中存放输入的4个数,tm[][25]中存放计算的步骤(把没用到的数也放到b[]与tm[]中)*/
    int i,j,l,x,y;/*i,j用来做双循环,l用来做加减乘除的开关,x,y为后面准备递归数据*/
    /*输出部分*/
   
    if(n==1)
    {
        if(fabs(a[0]-24)<0.00001)/*要考虑到除法,用fabs函数求绝对值与24比较,相当于等于24时的情况*/
        {
            /*以下限定输出条件*/
            if(tp[3]=='-'&&tp[2]=='-'&&tp[1]=='*')
            {
                if(te==0)
                {
                    printf("\n有解为%s=24",tem[0]);/*最后的表达式存在tem[0]中*/
                    k=1;
                    te=1;
                }
            }/*对运算符全为'*'的只输出一遍*/
            else if(tp[3]=='+'&&(tp[2]=='+'||tp[0]=='+')&&tp[1]=='+')
            {
                if(t==0)
                {
                    printf("\n有解为%s=24",tem[0]);/*最后的表达式存在tem[0]中*/
                    k=1;
                    t=1;
                }
            }/*对运算符全为'+'的只输出一遍*/
            else if(tp[3]=='+'&&(tp[2]=='*'||tp[0]=='*')&&tp[1]=='+')
            {
                if(g==0)
                {
                    printf("\n有解为%s=24",tem[0]);/*最后的表达式存在tem[0]中*/
                    k=1;
                    g=1;
                }
            }
            else if((tp[3]=='-'&&tp[2]=='+')||(tp[3]=='+'&&tp[2]=='-'))
            {
                if(w==0)
                {
                    printf("\n有解为%s=24",tem[0]);/*最后的表达式存在tem[0]中*/
                    k=1;
                    w=1;
                }
            }/*对于第二次运算时取到b[0]时,第一第二个运算符是'+','-'或'-','+'的只输出一遍*/
            else if(tp[3]=='+'&&tp[2]=='+')
            {
                if(u==0)
                {
                    printf("\n有解为%s=24",tem[0]);/*最后的表达式存在tem[0]中*/
                    k=1;
                    u=1;
                }
            }/*对于第二次运算时取到b[0]时,第一第二个运算符都是'+'的只输出一遍*/
            else if(tp[3]=='*'&&tp[0]=='*'&&tp[1]=='+')
            {
                if(tu=0)
                {
                    printf("\n有解为%s=24",tem[0]);/*最后的表达式存在tem[0]中*/
                    k=1;
                    tu=1;
                }
            }
            else
            {for(h=0;h<q;h++)
            {
            if(group[0]==flow[h])
            {
            if(tp[3]==tpt[h])
            {
            if(tp[2]==tptt[h])
            {
            if(group[1]==flo[h])
            {
            if(p==0)
            {
                printf("\n有解为%s=24",tem[0]);
                k=1;
                p=1;
            }
            }
            }
            }
            }
            else if (h==q-1)
            {
                printf("\n有解为%s=24",tem[0]);
                k=1;
            }
            }
            }
            flo[q]=group[1];/*限定条件初始化*/
            flow[q]=group[0];/*限定条件初始化*/
            tpt[q]=tp[3];/*限定条件初始化*/
            tptt[q]=tp[2];/*限定条件初始化*/
            tre[q]=tp[1];/*限定条件初始化*/
            tp[3]=0;
            tp[2]=0;
            tp[1]=0;
            tp[0]=0;
                    q=q+1;
        }
    }
                     
     /*运算部分*/
    if(n>1)/*该条件语句用来结束一次递归*/
    {
        for(i=0;i<n;i++)/*对n个数做'+','-','*','/'遍历*/
        {
            for(j=0;j<n;j++)
            {
                if(i==j)
                    continue;
                for(l=0;l<4;l++)/*进行四则运算*/
                {
                    switch(l)
                    {
                    case(0):if(n==2)
                            {
                    b[0]=a[0]+a[1];
                    break;
                }
                else  b[0]=a[i]+a[j];
                break;
                case(1):if(n==2)
                        {
                            b[0]=a[0]*a[1];
                            break;
                        }
                    else b[0]=a[i]*a[j];
                    break;
                    /*对加和乘做遍历时除去a[i]+a[j]与a[j]+a[i]或a[i]*a[j]与a[j]*a[i]的相同情况*/
                case(2):
                    b[0]=a[i]-a[j];
                    break;
                case(3):
                    if(a[j]==0)
                        break;
                    b[0]=a[i]/a[j];
                    break;
                    /*对减和除做遍历时考虑a[i]-a[j]与a[j]-a[i]或a[i]/a[j]与a[j]/a[i]的不同*/
                default:break;
            }
            
                if(l!=3||a[j]!=0)
               
            {   
                {
                    if(l==0||l==2||l==3)
                    sprintf(tm[0],"(%s%c%s)",tem[i],op[l],tem[j]);/*用sprintf函数将表达式输入到数组tm[]中,同时把括号也输入tm[]中*/
                    if(l==1)
                        sprintf(tm[0],"%s%c%s",tem[i],op[l],tem[j]);
                }
                {
                    if(n==3)/*提取第一次运算的运算符和运算结果*/
                    {
                        group[1]=b[0];
                        if(i!=0&&j!=0)
                            tp[0]=op[l];
                        else
                            tp[n-1]=op[l];
                    }
                    else if(n==4)/*提取第二次运算的运算符和运算结果*/
                    {
                        tp[n-1]=op[l];
                        group[0]=b[0];
                    }
                    else/*提取第三次运算的运算符和运算结果*/
                        tp[n-1]=op[l];
                }
                for(x=0,y=1;x<n;x++)/*备份没用到的数据(为递归用)*/
                {
                    if(x!=i&&x!=j)
                    {
                    b[y]=a[x];
                    strcpy(tm[y],tem[x]);/*复制字符串*/
                    y++;
                    }
                }
                }
                count(b,tm,n-1);                           
        }/*第三层for循环结束*/
    }/*第二层for循环结束*/
    }/*for循环结束*/
    }/*if 结束*/
    }/*count函数结束*/
   
   
        void main()
{
    char br=1;
    int d;
    while (br==1)
    {
        float number[4];/*放输入的4个数*/
        char temp[4][25];/*放表达式,初始化放4个数*/
    printf("请输入4个数\n");
    for(d=0;d<4;d++)
      scanf("%f",&number[d]);
    for(d=0;d<4;d++)
    {
        sprintf(temp[d],"%d",(int) number[d]);
    }/*初始化temp[d]*/
    count(number,temp,4);/*调用count函数*/
    if(k==0)
        printf("没有结果\n");
    printf("\n按0结束\n");
    scanf("%d",&br);
    }
   
}
2016-05-04 14:07
wanglianyi1
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:14
帖 子:647
专家分:2067
注 册:2015-6-18
收藏
得分:9 
是不是很多输出的算式结果不对。应该是你的算法设计有问题。再好好考虑下。这个可能需要用到排列组合方面的数学知识,反正挺麻烦的,因为可能的情况很多。可能需要优化下算法。


[此贴子已经被作者于2016-5-5 10:06编辑过]

2016-05-05 09:56
mege
Rank: 2
来 自:湖南株洲
等 级:论坛游民
帖 子:9
专家分:37
注 册:2016-5-4
收藏
得分:9 
参考一下吧,使用排列组合
#include "stdio.h"
int action(int a,int b,int c,char d)
{
    int ans=0;
    switch(d)
    {
        case '+':
            if(c==0)
                ans=a;
            else
                ans=a+b;
            break;
        case '-':
            if(c==0)
                ans=a;
            else
                ans=b-a;
            break;   
        case '*':
            if(c==0)
                ans=a;
            else
                ans=a*b;
            break;
        case '/':
            if(c==0)
                ans=a;
            else if(b%a)
                ans=1000000;
            else
                ans=b/a;
            break;
        default:
            break;
    }
    return ans;
}
void main()
{
    int num[4]={0},num1[4]={0},n,res;
    int i0,i1,i2,i3;
    int f0,f1,f2,f3;
    char flg[4]={'+','-','*','/'};
    char flag[4]={0};
    while(1)
    {
    printf("输入第一个数等于1000时退出程序\n请输入 4 个整数\n");
    scanf("%d%d%d%d",&num[0],&num[1],&num[2],&num[3]);
    printf("运算顺序为从左往右,不区分优先级\n");
    if(num[0]==1000)
        break;
    for(i0=0;i0<4;i0++)
    {
        num1[0]=num[i0];
        for(i1=0;i1<4;i1++)
        {
            if(i1!=i0)
            {
                num1[1]=num[i1];
                for(i2=0;i2<4;i2++)
                {
                    if((i2!=i0)&&(i2!=i1))
                    {
                        num1[2]=num[i2];
                        for(i3=0;i3<4;i3++)
                        {
                            if((i3!=i0)&&(i3!=i1)&&(i3!=i2))
                            {
                                num1[3]=num[i3];
                                for(f0=0;f0<4;f0++)
                                {
                                    flag[0]=flg[f0];
                                    for(f1=0;f1<4;f1++)
                                    {
                                        flag[1]=flg[f1];
                                        for(f2=0;f2<4;f2++)
                                        {
                                            flag[2]=flg[f2];
                                            for(f3=0;f3<4;f3++)
                                            {
                                                flag[3]=flg[f3];
                                                for(n=0;n<4;n++)
                                                    res=action(num1[n],res,n,flag[n]);
                                                if(res==24)
                                                    printf("%d %c %d %c %d %c %d =24 \n",num1[0],flag[1],num1[1],flag[2],num1[2],flag[3],num1[3]);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }  
    }
    return;
}
2016-05-05 10:43
快速回复:一个写的24点,可是输出得到的结果不怎么对,有兴趣的大神帮看看一下呗 ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.064968 second(s), 10 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved