由0到4五个数字,组成五位数,每一位都不重复,但十位和百位不能为3(当然万位不能为0),输出所有可能的五位数。

由0到4五个数字,组成五位数,每一位都不重复,但十位和百位不能为3(当然万位不能为0),输出所有可能的五位数。

#include<iostream>
using namespace std;
void Combination()
{
 //a,b,c,d,e分别是个位 十位 百位 千位 万位
 //e = 1 2 3 4
 //d = 0 1 2 3 4
 //c = 0 1 2 4
 //b = 0 1 2 4
 //a = 0 1 2 3 4 
 int num = 0;
 int a[5] = {0,1,2,3,4};//个位
 int b[4] = {0,1,2,4};//十位
 int c[4] = {0,1,2,4};//百位
 int d[5] = {0,1,2,3,4};//千位
 int e[4] = {1,2,3,4};//万位
 for(int e1 = 0;e1 < 4;e1++)
 {
  for(int d1 = 0;d1< 5;d1++)
  {
   for(int c1 = 0;c1 < 4;c1++)
   {
    for(int b1 = 0;b1 < 4;b1++)
    {
     for(int a1 = 0;a1 < 5;a1++)
     {
      if(a[a1]!=b[b1] && a[a1]!=c[c1] && a[a1]!=d[d1] && a[a1]!=e[e1] && b[b1]!=c[c1] && b[b1]!=d[d1] && b[b1]!=e[e1] && c[c1]!=d[d1] && c[c1]!=e[e1] && d[d1]!=e[e1])//每一位不能重复
      {
          num = e[e1]*10000+d[d1]*1000+c[c1]*100+b[b1]*10+a[a1];
          cout<<num<<" ";
      }
     }
    }
   }
  }
 }
}
int main()
{
 Combination();
 return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_43579888/article/details/88938316
今日推荐