Python 3.x 通过1或2步到达N级楼梯的所有可能步骤
我正在做一个python程序,我想找到所有可能的方法到达N层Python 3.x 通过1或2步到达N级楼梯的所有可能步骤,python-3.x,Python 3.x,我正在做一个python程序,我想找到所有可能的方法到达N层 Example: 1) s = 3 output should be the possible steps which are {1,1,1}, {2,1}, {1,2}. 2) s = 10, has 89 combinations: 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 2 2 1 1 1 1 1 1 1 1
Example:
1) s = 3 output should be the possible steps which are {1,1,1}, {2,1}, {1,2}.
2) s = 10, has 89 combinations:
1 1 1 1 1 1 1 1 1 1
2 1 1 1 1 1 1 1 1
1 2 1 1 1 1 1 1 1
1 1 2 1 1 1 1 1 1
2 2 1 1 1 1 1 1
1 1 1 2 1 1 1 1 1
2 1 2 1 1 1 1 1
1 2 2 1 1 1 1 1
1 1 1 1 2 1 1 1 1
2 1 1 2 1 1 1 1
1 2 1 2 1 1 1 1
1 1 2 2 1 1 1 1
2 2 2 1 1 1 1
1 1 1 1 1 2 1 1 1
2 1 1 1 2 1 1 1
1 2 1 1 2 1 1 1
1 1 2 1 2 1 1 1
2 2 1 2 1 1 1
1 1 1 2 2 1 1 1
2 1 2 2 1 1 1
1 2 2 2 1 1 1
1 1 1 1 1 1 2 1 1
2 1 1 1 1 2 1 1
1 2 1 1 1 2 1 1
1 1 2 1 1 2 1 1
2 2 1 1 2 1 1
1 1 1 2 1 2 1 1
2 1 2 1 2 1 1
1 2 2 1 2 1 1
1 1 1 1 2 2 1 1
2 1 1 2 2 1 1
1 2 1 2 2 1 1
1 1 2 2 2 1 1
2 2 2 2 1 1
1 1 1 1 1 1 1 2 1
2 1 1 1 1 1 2 1
1 2 1 1 1 1 2 1
1 1 2 1 1 1 2 1
2 2 1 1 1 2 1
1 1 1 2 1 1 2 1
2 1 2 1 1 2 1
1 2 2 1 1 2 1
1 1 1 1 2 1 2 1
2 1 1 2 1 2 1
1 2 1 2 1 2 1
1 1 2 2 1 2 1
2 2 2 1 2 1
1 1 1 1 1 2 2 1
2 1 1 1 2 2 1
1 2 1 1 2 2 1
1 1 2 1 2 2 1
2 2 1 2 2 1
1 1 1 2 2 2 1
2 1 2 2 2 1
1 2 2 2 2 1
1 1 1 1 1 1 1 1 2
2 1 1 1 1 1 1 2
1 2 1 1 1 1 1 2
1 1 2 1 1 1 1 2
2 2 1 1 1 1 2
1 1 1 2 1 1 1 2
2 1 2 1 1 1 2
1 2 2 1 1 1 2
1 1 1 1 2 1 1 2
2 1 1 2 1 1 2
1 2 1 2 1 1 2
1 1 2 2 1 1 2
2 2 2 1 1 2
1 1 1 1 1 2 1 2
2 1 1 1 2 1 2
1 2 1 1 2 1 2
1 1 2 1 2 1 2
2 2 1 2 1 2
1 1 1 2 2 1 2
2 1 2 2 1 2
1 2 2 2 1 2
1 1 1 1 1 1 2 2
2 1 1 1 1 2 2
1 2 1 1 1 2 2
1 1 2 1 1 2 2
2 2 1 1 2 2
1 1 1 2 1 2 2
2 1 2 1 2 2
1 2 2 1 2 2
1 1 1 1 2 2 2
2 1 1 2 2 2
1 2 1 2 2 2
1 1 2 2 2 2
2 2 2 2 2
以下是我的节目选自:
更新:
我在Java中发现了这段工作代码,我不明白如何将其更改为python
public static void main(String args[]) {
int s = 10;
List<Integer> vals = new ArrayList<>();
ClimbWays(s, 0, new int[s], vals);
vals.sort(null);
System.out.println(vals);
}
public static void ClimbWays(int n, int currentIndex, int[] currectClimb, List<Integer> vals) {
if (n < 0)
return;
if (n == 0) {
vals.add(currentIndex);
int last = 0;
for (int i = currentIndex - 1; i >= 0; i--) {
int current = currectClimb[i];
int res = current - last;
last = current;
System.out.print(res + " ");
}
System.out.println();
return;
}
currectClimb[currentIndex] = n;
ClimbWays(n - 1, currentIndex + 1, currectClimb, vals);
ClimbWays(n - 2, currentIndex + 1, currectClimb, vals);
}
publicstaticvoidmain(字符串参数[]){
int s=10;
List vals=new ArrayList();
爬坡道(s、0、新整数、VAL);
vals.sort(空);
系统输出打印项次(VAL);
}
公共静态无效爬升通道(int n,int currentIndex,int[]currentclump,列表VAL){
if(n<0)
返回;
如果(n==0){
VAL.add(当前索引);
int last=0;
对于(int i=currentIndex-1;i>=0;i--){
int电流=电流爬升[i];
int res=当前-最后;
last=当前值;
系统输出打印(res+“”);
}
System.out.println();
返回;
}
电流爬升[电流指数]=n;
爬升通道(n-1,电流指数+1,电流爬升,VAL);
爬升通道(n-2,电流指数+1,电流爬升,VAL);
}
您似乎正在寻找对以下内容的修改:
显示:
[(1, 2), (2, 1), (1, 1, 1)]
请注意,这将返回没有特别排序的顺序
(从中分割算法。)
下面是java代码到python的转换:
def climbWays(n, currentIndex, currentClimb, vals):
if n < 0:
return
if n == 0:
vals.append(currentIndex)
last = 0
for i in range(currentIndex - 1, -1, -1):
current = currentClimb[i]
res = current - last
last = current
print(res, end=" ")
print()
return
currentClimb[currentIndex] = n
climbWays(n - 1, currentIndex + 1, currentClimb, vals)
climbWays(n - 2, currentIndex + 1, currentClimb, vals)
s = 10
vals = []
climbWays(s, 0, [0] * s, vals)
vals.sort()
print(vals)
def爬升通道(n,当前索引,当前爬升,VAL):
如果n<0:
返回
如果n==0:
VAL.append(当前索引)
最后一个=0
对于范围内的i(当前索引-1,-1,-1):
电流=电流爬升[i]
res=当前-最后
最后=当前
打印(res,end=“”)
打印()
返回
当前爬升[当前索引]=n
爬升通道(n-1,电流指数+1,电流爬升,VAL)
爬升通道(n-2,电流指数+1,电流爬升,VAL)
s=10
VAL=[]
爬坡道(s、0、[0]*s、VAL)
vals.sort()
打印(VAL)
我的任务中有一个限制,即我必须使用步骤1或2才能到达终点,因此当我尝试将输入设置为10时,我看到步骤3、4、5不正确。你能帮我解决这个问题吗?你对10个楼梯的预期输出是多少?你想将更高的楼梯表示为楼梯1和2的总和吗?对于input=10,我们将使用1和2值得到89个组合。我将input=10的所有89个组合添加到我的问题中。此外,我发现一个Java代码工作正常,我现在无法理解如何将其更改为python
[(1, 2), (2, 1), (1, 1, 1)]
def climbWays(n, currentIndex, currentClimb, vals):
if n < 0:
return
if n == 0:
vals.append(currentIndex)
last = 0
for i in range(currentIndex - 1, -1, -1):
current = currentClimb[i]
res = current - last
last = current
print(res, end=" ")
print()
return
currentClimb[currentIndex] = n
climbWays(n - 1, currentIndex + 1, currentClimb, vals)
climbWays(n - 2, currentIndex + 1, currentClimb, vals)
s = 10
vals = []
climbWays(s, 0, [0] * s, vals)
vals.sort()
print(vals)