2014-12-05 48 views
-1

我想在不使用循环的情况下编写以下代码的递归版本。没有循环的递归计数器

static void count(){ 
    for (int i =0; i <=10; i++) System.out.println(i); 
} 

我可以做它作为一个静态int,但我不能把它作为一个void。

谢谢!

+0

你有什么错误?你的问题不是很清楚...... – 2014-12-05 08:20:25

回答

0

我相信你想为你的问题递归函数。

#include <iostream> 
using namespace std; 

void count(int x) 
{ 
     if (x == 0) 
     { 
       return; 
     } 
     cout<<x<<endl; 
     count(x-1); 
} 

int main() 
{ 
     count(10); 
} 
0

如果你想从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); 
}