您的位置:首页 > 其它

Project Euler Problem 24

2011-03-22 11:56 288 查看
A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:

012 021 102 120 201 210

What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?

1 #include <iostream>

2 #include <vector>

3 #include <algorithm>

4 using namespace std;

5

6 int main(){

7 int factorial[9];

8 int sign[9];

9 int mul = 1;

int sum = 999999;

int tmp;

vector<int> out;

for(int i=0; i<10; i++){

out.push_back(i);

}

for(int i=1; i<10; i++){

mul = mul*i;

factorial[9-i] = mul;

}

for(int i=0; i<9; i++){

sign[i] = sum/factorial[i];

sum -= sign[i]*factorial[i];

}

for(int i =0; i<9; i++){

cout << i << '\t' << sign[i] << '\t' << factorial[i] << endl;

}

cout << endl;

for(int i=0; i< 9; i++)

{

tmp = out[i];

out[i] = out[sign[i]+i];

out[sign[i]+i] = tmp;

sort(out.begin()+i+1,out.end());

for(int j=0; j<10; j++){

cout << out[j];

}

cout << endl;

}

cin.get();

return 0;

}

/article/6021065.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: