动态嵌套循环水平 [英] Dynamic nested loops level
本文介绍了动态嵌套循环水平的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图找出一个简单的方法来处理动态嵌套的循环水平。考虑下面的函数,它在2个参数:循环#NUM和最大值
I'm trying to figure out a simple way to handle dynamic nested loops level. Consider the following function that takes in 2 parameters: #num of loops, and max value.
void PrintLoop(int maxloop, int maxvalue)
PrintLoop(1,2);
// output
0
1
PrintLoop(2,2);
// output
0, 0
0, 1
1, 0
1, 1
PrintLoop(3,2);
// output
0, 0, 0
0, 0, 1
0, 1, 0
0, 1, 1
1, 0, 0
1, 0, 1
1, 1, 0
1, 1, 1
等等...
Etc...
有没有一种方法来写,能产生这种动态嵌套循环的行为的函数?
Is there a way to write a function that can generate this "dynamic nested loops" behavior?
感谢您的帮助
推荐答案
是的,这是可能的,实现这一概念的递归的通常用于:
Yes, it is possible, and to implement this a concept of "recursion" is often used:
void PrintLoop(int maxloop, int maxvalue)
{
if (maxloop<=0) return ;
// print something here...
for (int i=0;i<maxmalue;i++){
PrintLoop(maxloop-1, maxvalue);
}
}
这篇关于动态嵌套循环水平的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文