不能用循环,对一个整形n,实现输出n,2n,4n,...当大于max(比如5000)时再逆向输出...4n,2n,n。
#include <stdio.h>
void show(int n, int max)
{
if (n > max) {
return;
}
printf("%d ", n);
show(2*n, max);
printf("%d ", n);
}
int main()
{
show(1, 400);
return 0;
}
本文介绍了一种使用递归而非循环的方法来实现输出一个整数及其倍数序列,并在达到最大值后逆向输出。通过自定义函数show,实现了从n开始,依次输出n, 2n, 4n等直至超过最大值max时停止,之后从最大值逆向输出回n。

被折叠的 条评论
为什么被折叠?



