
#include<stdioh>
int total=20;
int result[21]={0};
FILE fp;
int sum=0;
test(int n){
int i;
if(total==0){
printresult(n-1);
sum++;
return;
}
for(i=1;i<=2;i++){
result[n]=i;
total-=i;
if(total>=0)
test(n+1);
total+=i;
}
}
main(){
if((fp=fopen("resulttxt","w"))==NULL)
{ /以文本只写方式打开文件/
printf("cannot open file");
exit(0);
}
test(1);
printf("There are %d kind of ways to do this",sum);
}
printresult(int n){
int j;
for(j=1;j<=n;j++){
printf("%d ",result[j]);
fprintf(fp,"%d ",result[j]);
}
printf("\n");
fprintf(fp,"\n");
}
结果保存在同目录下的resulttxt中 共10946种结果 耐心点 运行需要一定时间
total剩余阶梯数 sum方法种数 result结果数组
int recursive(int n)
{
if (n <= 2)
return n;
return recursive(n - 1) + 2 recursive(n - 2);
}
int iterative(int n)
{
int f1 = 1, f2 = 2, f;
for (int i = 3; i <= n; ++i)
{
f = f2 + 2 f1;
f1 = f2;
f2 = f;
}
return f;
}
这一句不会影响到你printf的输出结果。
return只是对上层调用者返回一个参数,如果你不写,则函数运行结束的时候,会返回一个随机的数值。它只是让调用者知道函数需要返回的值(如果调用者需要的话)。
本程序的调用者是 *** 作系统。
良好的编译风格,返回值要与函数的声明类型一致。
如果不需要返回值,则也需要写成:
void main()
{
//
return ;
}
program climbstone;
var total,step;
procedure tryit(n:integer;var step,
total
:integer );
begin
if n=0 then begin writeln ;writeln ('step=',step); writeln("----"); step=0; inc(total); exit;end;
inc(step);
wirite('1,');
tryit(n-1);
if n>1 then do begin write('2,'); tryit(n-2) ;end;
end;
begin
readln(n);
total:=0;
step:=0;
tryit(n,step,total);
writeln('total ',total ,'mode');
end
X轴和Z轴怎么能同时车呢?要不就是X轴进给,车外圆;要不就是Z轴进给,车平面。不可能同时走。
一般来说造成有台阶的问题主要是由于托板有间隙造成的,X轴是转塔大托板、Z轴是转塔上的小托板,消除台阶需要调整托板的衔铁间隙。
如果每次都可以走i个台阶(i为1或2或3)
步骤一:只要N大于i,N个台阶的走法总是可以转化成先走i个台阶,再走(N-i)个台阶。
步骤二:然后用N代替N-i,重复步骤一。
以上就是关于编译程序:有20级台阶的楼梯,一次可以迈一级或两级台阶,那么爬完此楼梯有几种方法全部的内容,包括:编译程序:有20级台阶的楼梯,一次可以迈一级或两级台阶,那么爬完此楼梯有几种方法、楼梯有n阶台阶,上楼可以一步上1阶,也可以一步上2阶,用C++或lua语言编一程序计算共有多少种不同的走法、C程序:爱因斯坦的阶梯问题等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)