笔试题 1.6 枚举+递归
1, 这个题是个老题目啦:
1~9个数字,每个数字只能出现一次,要求这样一个9位的数字:其第一位能被1整除,前两位能被2整除,前三位能被3整除,。。前9位能被
9整除。
#include<iostream> #include<vecor> using namespace std; vector<long long >acc; bool used[10]; void dfs(int k, long long a){ if(k>0 && a%k!=0){ return; } if( k == 9 ){ acc.push_back(a); return; } for(int i=1; i<=9; i++){ if(used[i] == false){ used[i] = true; dfs(k+1, a*10+i); used[i] = false; } } }
算是学习了。