没有循环的递归计数器
Recursive counter without a loop
我正在尝试在不使用循环的情况下编写以下代码的递归版本。
static void count (){
for ( int i =0; i <=10; i++) System.out.println(i);
}
我可以将其作为静态 int 来做,但我不能把它作为一个空。
谢谢!
我相信你想要一个递归函数来解决你的问题。
#include <iostream>
using namespace std;
void count(int x)
{
if (x == 0)
{
return;
}
cout<<x<<endl;
count(x-1);
}
int main()
{
count(10);
}
如果你想从 0 到 10 计数,你可以用 2 个参数来完成:
#include <iostream>
using namespace std;
void count(int start, int end)
{
if (start == end)
{
return;
}
cout << start << endl;
count(++start, end);
}
int main()
{
count(0, 10);
}
相关文章:
- 通过递归进行因子分解
- 递归函数计算序列中的平方和(并输出过程)
- 使用递归的数组的最小值.这是怎么回事
- 递归列出所有目录中的C++与Python与Ruby的性能
- 递归计数给定目录的文件和所有目录
- 如何在BST的这个简单递归实现中消除警告
- C++:正在检查LinkedList中的回文-递归方法-错误
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 递归无序映射
- TSP递归解的迭代形式
- 如何在Elixir中调用递归函数并行
- 返回递归调用和仅递归调用的区别
- 数组元素打印的递归方法
- 使用递归时获取变量的奇怪值
- 如何在C++中递归地按相反顺序打印集合
- 到连接组件算法的问题(递归)
- 如何使用递归打印修改后的星号三角形图案
- C - 递归计数器
- 没有循环的递归计数器
- C++函数内部的递归计数器