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
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
相关文章推荐
- project euler problem 24
- Project Euler problem 24
- Project Euler Problem 24 Lexicographic permutations
- project euler problem 22对文件的处理
- Project Euler problem 68
- Project Euler problem 21
- Project Euler problem 41
- Project Euler Problem 2 - Even Fibonacci numbers
- Project Euler Problem 12
- project euler Problem 39
- project euler Problem 13
- Project Euler Problem 1: Multiples of 3 and 5
- Project Euler Problem 7: 10001st prime
- Project Euler Problem 9: Special Pythagorean triplet
- Project Euler Problem 27 Quadratic primes
- http://projecteuler.net/problem=27 [Answer:-59231]
- Project Euler Problem 9
- project euler Problem 3
- Projecteuler Problem 1-10
- project euler problem 29数的多次幂的重复个数