注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

熊猫正正的博客

熊猫正正的天空

 
 
 

日志

 
 

枚举法  

2011-05-03 20:38:06|  分类: C数据结构与算法 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

#include <stdio.h>

int main()
{
 int i1,i2,i3,i4,i5;
 long multi,result;

 for(i1=1 ; i1<=9 ; i1++)
 {
  for(i2=0 ; i2<=9 ; i2++)
  {
   for(i3=0 ; i3<=9 ; i3++)
   {
    for(i4=0 ; i4<=9 ; i4++)
    {
     for(i5=0 ; i5<=9 ; i5++)
     {
      multi = i1*10000+i2*1000+i3*100+i4*10+i5;
      result = i5*100000+i5*10000+i5*1000+i5*100+i5*10+i5;
      if(multi*i1 == result)
      {
       printf("\n%5d%2d%2d%2d%2d\n",i1,i2,i3,i4,i5);
       printf("X%12d\n",i1);
       printf("___________________\n");
       printf("%3d%2d%2d%2d%2d%2d\n",i5,i5,i5,i5,i5,i5);
      }
     }
    }
   }
  }
 }
}

 

#include <stdio.h>
int main()
{
    int j,i[5]; //循环变量 ,数组i用来表示4个运算符
    int sign;//累加运算时的符号  
    int result; //保存运算式的结果值
    int count=0; //计数器,统计符合条件的方案
    int num[6];  //保存操作数
    float left,right; //保存中间结果
    char oper[5]={' ','+','-','*','/'}; //运算符
    printf("请输入5个数:");
    for(j=1;j<=5;j++)
        scanf("%d",&num[j]);
    printf("请输入结果:");
    scanf("%d",&result);
    for(i[1]=1;i[1]<=4;i[1]++)//循环4种运算符,1表示+,2表示-,3表示*,4表示/
    {
        if((i[1]<4) || (num[2]!=0))//运算符若是/,则第二个运算数不能为0
        {
            for(i[2]=1;i[2]<=4;i[2]++)
            {
                if((i[2]<4) || (num[3]!=0))
                {
                    for(i[3]=1;i[3]<=4;i[3]++)
                    {
                        if((i[3]<4) || num[4]!=0)
                        {
                            for(i[4]=1;i[4]<=4;i[4]++)
                            {
                                if((i[4]<4) || (num[5]!=0))
                                {
                                    left=0;
                                    right=num[1];
                                    sign=1;
                                    for(j=1;j<=4;j++)
                                    {
                                        switch(oper[i[j]])
                                        {
                                            case '+':
                                                 left=left+sign*right;
                                                 sign=1;
                                                 right=num[j+1];
                                                 break;
                                            case '-':
                                                 left=left+sign*right;
                                                 sign=-1;
                                                 right=num[j+1];
                                                 break;//通过f=-1实现减法
                                            case '*':
                                                 right=right*num[j+1];
                                                 break;//实现乘法
                                            case '/':
                                                 right=right/num[j+1];//实现除法
                                                 break;
                                        }
                                    }
                                    if(left+sign*right==result)
                                    {
                                        count++;
                                        printf("%3d:",count);
                                        for(j=1;j<=4;j++)
                                            printf("%d%c",num[j],oper[i[j]]);
                                        printf("%d=%d\n",num[5],result);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if(count==0)
        printf("没有符合要求的方法!\n");
    getch();
    return 0;
}

  评论这张
 
阅读(53)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017