我想写一个c函数,它接受一个整数作为输入,并使用那么多数字给我所有可能的组合 例如
cases(3);
输出:
123 132 213 231 312 321
它使用前三位数字来创建一个三位数的数字,请注意我需要它才能处理任意数量的数字n。
注意案例(3)有3个! = 6结果 所以案例(4)有4个! = 24结果等等......
我实际上甚至不知道如何处理这个问题,所以任何帮助都会受到赞赏。
答案 0 :(得分:1)
胜利的递归: - )
1
N
个数字的组合是N - 1
个数字的递归组合,每个可能的地方都添加了N
答案 1 :(得分:0)
在实际尝试编写代码之前尝试考虑算法。 想一想,当你写下你想要的输出时,你是如何解决问题的。只是找到一种系统的方法来做到这一点:例如,你从最低的数字开始,然后检查其他数字......
答案 2 :(得分:0)
我已经用python编写了逻辑和代码
#n digit number as input converted into list
m=int(input("enter number of digits:"))
f=[]
for i in range(1,m+1):
f.append(str(i))
#dynamic array for dynamic for loop inside recursion
a=[0 for k in range(len(f))]
c=[]#list which is to be used for append for digits
ans=[]# result in which the
# recursion for if loop inside for loop
#1st argument is fixed inside the loop
#2nd argument will be decreasing
def conditn(k,m):
if(m==0):
return 1
if(m==1):
if(a[k]!=a[0]):
return 1
if(a[k]!=a[m-1] and conditn(k,m-1)):
return 1
#recursion for for loop
#1st argument y is the length of the number
#2nd argument is for initialization for the varible to be used in for loop
#3rd argument is passing the list c
def loop(y, n,c):
if n<y-1:
#recursion until just befor the last for loop
for a[n] in range(y):
if(conditn(n,n)):
loop(y, n + 1,c)
else:
# last for loop
if(n==y-1):
for a[n] in range(y):
#last recursion of condition
if(conditn(n,n)):
#concatinating the individual number
concat=""
for i in range(y):
concat+=f[a[i]]+""
c.append(concat)
#returning the list of result for n digit number
return c
#printing the list of numbers after method call which has recursion within
#set is used used to convert any of the iterable to the
#distinct element and sorted sequence of iterable elements,
for j in (loop(len(f),0,c)):
print(j)