【发布时间】:2018-01-23 04:38:53
【问题描述】:
我的初始伪代码如下所示
int ascend(int n) {
Print number
Return ascend
}
我可以从 n 打印到零...但是如何使用递归从零打印到 n?
但是,我设法按升序从零数到 n 个数字。 这是实现
#include<iostream>
using namespace std;
int countUp(int bound,int fixed) /*both parameters are passed by same
value whereas for every function call the 'bound'
variable will change and the 'fixed' variable will remain
as it is */
{
int x = fixed;
int y = bound-x;
if(bound>2*fixed)
{
return 0;
}
else
cout<<y<<endl;
return countUp(bound+1,fixed);
}
int main()
{
int var;
cout<<"Please input the upper bound"<<endl;
cin>>var;
countUp(var,var);
}
【问题讨论】:
-
选择一种语言,编写您的代码。当您遇到特定问题时再回来。
-
在询问之前至少尝试一下。
-
看起来这是你的功课。首先告诉你解决问题的方法
-
我想打印从 1 到 n 的数字的一般方法是先打印从 1 到 n-1 的数字,然后将 n 放在最后。
-
你的伪代码差不多了 - 1. 你需要在调用提升之前获取下一个值。 2.你需要知道什么时候停止 3.print语句的位置会决定你是打印0-n还是n-0。