dreamangel

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  14 随笔 :: 0 文章 :: 0 评论 :: 0 Trackbacks
已知abcd*e=fghi,abcd、fghi表示一个四位数,e表示一个一位数,abcdefghi表示从1到9的整数,且abcdefghi各不相同,求表达式abcd*e=fghi?
#include <iostream>
using namespace std;

int main()
{
 
int a[9]={1,1,1,1,1},num,s;
 
for (a[4]=1;a[4]<=9;a[4]++)
 
{
  
for (a[3]=1;a[3]<=9;a[3]++)
  
{
   
for (a[2]=1;a[2]<=9;a[2]++)
   
{
    
for (a[1]=1;a[1]<=9;a[1]++)
    
{
     
for (a[0]=1;a[0]<=9;a[0]++)
     
{
      
bool jus=true;            //可能有多个解 
      num=1000*a[0]+100*a[1]+10*a[2]+a[3];                     
      s
=num*a[4];
      
if (s/10000==0)
      
{
       a[
5]=s/1000;
       a[
6]=s/100-10*a[5];
       a[
7]=s/10-100*a[5]-10*a[6];
       a[
8]=s%10;
      }

      
else
          
continue;
      
for (int i=0;i<8 && jus;i++)
      
{
       
for (int j=i+1;j<=8 && jus;j++)
       
{
        
if (a[i]==a[j] || a[j]<=0)    //不能小等于0 
           jus=false;
       }

      }

      
if (jus==true)
         cout 
<<a[0]<<a[1]<<a[2]<<a[3]<<"*"<<a[4]<<"="<<a[5]<<a[6]<<a[7]<<a[8]<<endl;
     }

    }

   }

  }

 }

 getchar();
 
return 0;
}
posted on 2010-04-28 16:05 飞翔天使 阅读(467) 评论(0)  编辑 收藏 引用 所属分类: ACM

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理