Project Euler Problem 24 (C++和Python)

本文探讨了求解百万次字典排列的问题,通过C++和Python两种编程语言实现了寻找特定排列的方法。介绍了如何使用next_permutation函数和递归算法生成指定顺序的排列,通过实例验证了算法的正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 24 : Lexicographic permutations

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?

C++ source code

#include <iostream> 
#include <string>
#include <cassert>    // assert()
#include <algorithm>  // next_permutation()

using namespace std;

class PE0024
{
public:
    string findNextPermutation(const string& digitsString, int SeqNo); 
};
    
string PE0024::findNextPermutation(const string& digitsString, int SeqNo)
{
    int num = 0;
    string digits;
    
    digits.assign(digitsString.begin(), digitsString.end());

    while(true == next_permutation(digits.begin(), digits.end()))
    {
        num++;
        if (num == SeqNo-1)
        {
            break;
        }
    }
    return digits; 
}

int main() 
{         
    PE0024 pe0024;

    string digitsString = "012";    
    assert(pe0024.findNextPermutation(digitsString, 3) == "102");

    digitsString = "0123456789";
    int sequenceNo = 1000000; // millionth   

    cout << "The millionth lexicographic permutation of the digits " ;
    cout << "0,1,2,3,4,5,6,7,8 and 9 is " ;
    cout << pe0024.findNextPermutation(digitsString,sequenceNo) << endl;

    return 0; 
}     

Python source code

import math

#--- generate permutations-------------------------------------------------
def next_permutation(s, n):
    """
    Find the nth permutation of the string s, but n starts from 0 
     Example:
    >>>next_permutation('abcde', 30)
    bcade
    """
    if len(s)==1: 
        return s

    q, r = divmod(n, math.factorial(len(s)-1))

    return s[q] + next_permutation(s[:q] + s[q+1:], r)

def next_permutation2(s, n, fn):
    """
    Find the nth permutation of the string s, but n starts from 0 
     Example:
    >>>next_permutation('abcde', 30)
    bcade
    """
    if len(s)==1: 
        return s
     
    q, r = divmod(n, fn)  # fn = factorial(len(s)-1)

    return s[q] + next_permutation(s[:q] + s[q+1:], r)

def findNextPermutation(digitsString, SeqNo):
    """ SeqNo starts from 1 """
    return next_permutation(digitsString, SeqNo-1)

def findNextPermutation2(digitsString, SeqNo):
    """ SeqNo starts from 1 """
    return next_permutation2(digitsString, 
                             SeqNo-1, 
                             math.factorial(len(digitsString)-1))
     
def main():
    assert findNextPermutation('012', 3) == '102'
    
    print("The millionth lexicographic permutation of the digits")
    print("0,1,2,3,4,5,6,7,8 and 9 is", findNextPermutation2('0123456789',1000000))

if  __name__ == '__main__':
    main()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值