偶是一只想要飞却忘了咋飞的菜鸟
多谢大家了,快点告诉我
大家可以想想这个问题的时间复杂度:
只说i,i从0循环到999999999要循环1亿次!!!!!可读性不好,时间复杂度高~~~这是个好问题~~~,大家可以想想其他的思路~~~~
#include <iostream> #include <cmath> #include <cstdlib> #include <algorithm> using namespace std;
int main() { int code[9] = {1,2,3,4,5,6,7,8,9}; int num[9] = {0};
sort(code, code+9); do { bool success = false; for(int i = 1; i<9; i++) { num[i] = 0; for(int k = 0; k<=i; k++) { num[i] += code[k]*pow(10, (i-k)); } if(num[i]%(i+1) == 0) success = true; else { success = false; break; } } if(success) { for(int r = 0; r<9; r++) cout<<code[r]<<" "; cout<<endl; } }while (next_permutation(code, code+9));
system("pause"); return 0; }