将1到9重新排列,使得前n位数字组…

最终结果是:381654729;
首先,偶数位上一定是偶数,因为只有偶数才可以被偶数整除;
编程实现如下:

 

#include <iostream>
using namespace std;
void main()
{
  int a[9];
  int j, num;
  a[4] = 5;
  cout<<"
所求的九位数为:"<<endl;
  for (a[0] = 1; a[0] < 10; a[0] += 2)
  {
    for (a[1] = 2; a[1] < 9; a[1] += 2)
    {
      for (a[2] = 1; a[2] < 10; a[2] += 2)
      {
        if (a[2] == a[0])
          continue;
        for (a[3] = 2; a[3] < 9; a[3] += 2)
        {
          if (a[3] == a[1])
            continue;
          for (a[5] = 2; a[5] < 9; a[5] += 2)
          {
            if (a[5] == a[3] || a[5] == a[1])
              continue;
            for (a[6] = 1; a[6] < 10; a[6] += 2)
            {
              if (a[6] == a[4] || a[6] == a[2] || a[6] == a[0])
                continue;
              for (a[7] = 2; a[7] < 9; a[7] += 2)
              {
                if (a[7] == a[5] || a[7] == a[3] || a[7] == a[1])
                  continue;
                for (a[8] = 1; a[8] < 10; a[8] += 2)
                {
                  if (a[8] == a[6] || a[8] == a[4] || a[8] == a[2] || a[8] == a[0])
                    continue;
                  num = 0;
                  for (j = 0; j < 9; j++)
                  {
                    num = num * 10 + a[j];
                    if (num % (j + 1) != 0)
                      break;
                  }
                  if (j == 9)
                    cout<<num<<endl;
                }
              }
            }
          }
        }
      }
    }
  }
}

 

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值