当前页面: 开发资料首页 → Eclipse 专题 → 请高手帮我解决一个程序,有18凳台阶,可以上一邓或者两登,打印出每种上法
请高手帮我解决一个程序,有18凳台阶,可以上一邓或者两登,打印出每种上法
摘要: 请高手帮我解决一个程序,有18凳台阶,可以上一邓或者两登,打印出每种上法
请高手帮我解决一个程序,有18凳台阶,可以上一邓或者两登,打印出每种上法
比如:
可以都上一登,打印结果是:111111111111111111
可以有一个上两登:211111111111111111
111111111111111112
等等
谢谢!!!!!!!!!!!!!!!!!!!
package ...
public class Test {
public static void main(String[] args)
{
Test test=new Test();
int[] a=new int[0];
test.climb(18,0,a);//这里的18时你要求的18阶,你可以改成其他的试试
}
public void climb(int altogether,int climbedSteps,int[] climbed)
{
if((altogether-climbedSteps)==1)
{
int[] climbAStep=new int[climbed.length+1];
climbAStep[climbed.length]=1;
for(int i=0;i
{
climbAStep[i]=climbed[i];
}
for(int i=0;i
{
System.out.print(climbAStep[i]);
}
System.out.println("");
}
else if((altogether-climbedSteps)==2)
{
int[] climbAStep=new int[climbed.length+1];
for(int i=0;i
{
climbAStep[i]=climbed[i];
}
climbAStep[climbed.length]=1;
climb(altogether,climbedSteps+1,climbAStep);
climbAStep[climbed.length]=2;
for(int i=0;i
{
System.out.print(climbAStep[i]);
}
System.out.println("");
}
else
{
int[] climbAStep=new int[climbed.length+1];
for(int i=0;i
{
climbAStep[i]=climbed[i];
}
climbAStep[climbed.length]=1;
climb(altogether,climbedSteps+1,climbAStep);
climbAStep[climbed.length]=2;
climb(altogether,climbedSteps+2,climbAStep);
}
}
}
public class Test
{
public static void main(String args[])
{
final int num = 18;
new Test(num);
}
private final short steps[]; //shared
public Test(int maxsteps)
{
this.steps = new short[maxsteps];
this.step(maxsteps,0);
}
private void step(int remain,int numsteps)
{
if(remain==0){
for(int i=0; i
System.out.print(String.valueOf(steps[i]));
}
System.out.println();
}else{
if(remain>=1){
steps[numsteps] = 1;
step(remain-1,numsteps+1);
}
if(remain>=2){
steps[numsteps] = 2;
step(remain-2,numsteps+1);
}
}
}
}
public class Test2 {
public static void main(String[] args)
{
Test2 t=new Test2();
t.list(18);
}
public void list(int totalSteps)
{
int[] step=new int[totalSteps];
int climbedTimes=0;
int climbed=0;
while(true){
for(;climbed
for(int i=0;i
System.out.println("");
int last;
while(true)
{
for(last=2;last==2&&climbedTimes>0;last=step[--climbedTimes],climbed-=last);
if(climbed==0&&last==2)
return;
if(climbed<=(totalSteps-2))
{
step[climbedTimes++]=2;
climbed+=2;
break;
}
last=step[--climbedTimes];
climbed-=last;
if(last==1)
{
step[climbedTimes++]=2;
climbed+=2;
break;
}
}
}
}
}
顶