用1-9组成三个三位数abc,def,ghi,每个数字只能使用一次,并且要求abc:def:ghi=1:2:3

//判断有没有重复的数字
int fun(int a[],int b[],int c[])
{
   for(int i=0;i<3;i++)
   {
      for(int j=0;j<3;j++)
      {
          for(int k=0;k<3;k++)
          {
              if(a[i]==b[j]||a[i]==c[k]||b[j]==c[k])
                  return 0;
          }
      }
   }
   return 1;
}


int main()
{
   int one,two,three;
   int a[3],b[3],c[3];
   int i,j,k;
   for(i=123;i<350;i++)
   {
      one=i;
      a[0]=one/100;                  
      a[1]=(one%100)/10;
      a[2]=one%10;
      if(a[0]==a[1]||a[1]==a[2]||a[0]==a[2])     //不能有重复的数字
          continue;
      for(j=246;j<700;j++)
      {
          two=j;
          b[0]=two/100;
          b[1]=two%100/10;
          b[2]=two%10;
          if(b[0]==a[1]||b[1]==b[2]||b[0]==b[2])
              continue;
          for(k=369;k<1000;k++)
          {
              three=k;
              c[0]=three/100;
              c[1]=three%100/10;
              c[2]=three%10;
              if(c[0]==c[1]||c[1]==c[2]||c[0]==c[2])
                  continue;
              if(fun(a,b,c))
                  if(one*3==three&&one*2==two)
                      printf("%d %d %d\n",one,two,three);
          }
      }
   }
}

 

posted on 2013-03-20 13:11  mrheyao  阅读(1366)  评论(1编辑  收藏  举报