Ray's playground

 

Project Euler Problem 24

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;
10     int sum = 999999;
11     int tmp;
12     vector<int> out;
13     
14     for(int i=0; i<10; i++){
15         out.push_back(i);
16     }
17     
18     for(int i=1; i<10; i++){
19         mul = mul*i;
20         factorial[9-i] = mul;
21     }
22     
23     for(int i=0; i<9; i++){
24         sign[i] = sum/factorial[i];
25         sum -= sign[i]*factorial[i];
26     }
27     
28     for(int i =0; i<9; i++){
29         cout << i << '\t' << sign[i] << '\t' << factorial[i] << endl;
30     }
31     cout << endl;
32     
33     for(int i=0; i< 9; i++)
34     {
35         tmp = out[i];
36         out[i] = out[sign[i]+i];
37         out[sign[i]+i] = tmp;
38         sort(out.begin()+i+1,out.end());
39         for(int j=0; j<10; j++){
40             cout << out[j];
41         }
42         cout << endl;
43     }
44     
45     cin.get();
46     return 0;
47 }

 

http://www.cnblogs.com/xianglan/archive/2011/03/07/1976431.html 

posted on 2011-03-22 11:56  Ray Z  阅读(208)  评论(0编辑  收藏  举报

导航