第一位能被1整除,前两位能被2整除,...,前九位能被9整除

#include<iostream>
#include<vector>
using namespace std;

void nineBitInteger(int k, long long a, bool *check, vector<long long> &result)
{   //判断是否可以被位数(k)整除,不可则结束
	if (k&&a%k != 0)return;
	//第9位也符合条件则将数A保存起来
	if (k == 9)
	{
		result.push_back(a);
		return;
	}
	//递归过程
	for (int i = 1; i <= 9; i++)
	{
		if (!check[i])
		{  //1-9这九个数,其中检测过则赋值为1
			check[i] = 1;
			nineBitInteger(k + 1, a * 10 + i, check, result);
			check[i] = 0;
		}
	}
}
void main() { bool check[10]; memset(check, 0, sizeof(check)); vector<long long> result; nineBitInteger(0, 0, check, result); for (unsigned int i = 0; i < result.size(); i++) { cout << result[i] << endl; } cout << endl; }

  

posted on 2018-07-31 17:14  HelloShijam  阅读(783)  评论(0编辑  收藏  举报

导航