如何打印A N位数所有可能的组合,而无需使用数组? [英] How to print all the possible combinations of a N digit number without using arrays?

查看:143
本文介绍了如何打印A N位数所有可能的组合,而无需使用数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我强调的点,而无需使用数组。是否有任何的数学推导吗?

I'm stressing on the point of without using an array. Is there any mathematics derivation for this?

推荐答案

这是十进制整数本质上是数字数组。一些简单的数学将允许那些在整数位索引就像任何其他的阵列。

An decimal integer is essentially an array of digits. Some simple math will allow the indexing of those digits in the integer just like any other array.

下面的Python示例code将不正是你想要通过利用这样一个事实:

The following python sample code will do exactly what you wanted by exploiting this fact:

#!/usr/bin/python3

def getNumDigits(number):
    if number == 0:
        digits = 1
    else:
        digits = 0
        while number > 0:
            digits += 1
            number //= 10
    return digits

def getDigitAtPosition(position, number):
    return (number // (10**position)) % 10

def swapDigits(number, position_one, position_two):
    digit_one = getDigitAtPosition(position_one, number)
    digit_two = getDigitAtPosition(position_two, number)
    difference = digit_one - digit_two
    return number + (difference * 10**position_two) - (difference * 10**position_one)

def sortNumber(number, num_digits=None):
    if num_digits is None:
        num_digits = getNumDigits(number)
    for i in range(num_digits-1):
        for j in range(i+1, num_digits):
            if getDigitAtPosition(j, number) < getDigitAtPosition(i, number):
                number = swapDigits(number, i, j)
    return number

def isDigitInNumber(digit, number):
    for i in range(getNumDigits(number)):
        if getDigitAtPosition(i, number) == digit:
            return True
    return False

def printCombinations_sorted(number, prepend, num_digits):
    if num_digits == 1:
        print(number + (prepend * (10**num_digits)))
    else:
        lead_position = num_digits-1
        lead_digit = getDigitAtPosition(lead_position, number)
        digits_swapped = lead_digit
        printCombinations_sorted(number % 10**(lead_position), (prepend*10) + lead_digit, num_digits-1)
        for i in range(num_digits-1):
            sub_number = swapDigits(number, num_digits-1, num_digits-2-i)
            sub_lead_digit = getDigitAtPosition(lead_position, sub_number)
            if not isDigitInNumber(sub_lead_digit, digits_swapped):
                digits_swapped = digits_swapped*10 + sub_lead_digit
                printCombinations_sorted(sub_number % 10**(lead_position), (prepend*10) + sub_lead_digit, num_digits-1)

def printCombinations(number):
    num_digits = getNumDigits(number)
    number = sortNumber(number, num_digits)
    printCombinations_sorted(number, 0, num_digits)

if __name__ == "__main__":
    number = 101010
    printCombinations(number)

既然不指定,如果重复的数字将被允许,我写这个的假设,他们将。它可以简化相当多如果情况并非如此。

Since it was not specified if repeated digits would be allowed, I wrote this with the assumption that they would. It could be simplified quite a bit if that is not the case.

这篇关于如何打印A N位数所有可能的组合,而无需使用数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆