task1 赶鸭子 递归算法
task2 从终端输入一个数,用递归按顺序打印每一位数字,此递归不能有返回值(void function)
#include <stdio.h>
#include <math.h>
int peach(int a,int number){
while(a > 0){
printf("%dday %d peaches\n",a,number);
return peach(--a,(++number)*2);
}
}
int duck(int a, int number){
while(a > 0){
printf("%dday %d ducks\n",a,number);
return duck(--a,(++number)*2);
}
}
void fun(int a,int n){
while(a / 10 >= 0){
if(pow(10,n) >= 1){
int temp = a / (int)pow(10,n);
printf("%d ", temp);
fun(a -= temp * pow(10,n + 1), --n );
}
break;
}
}
int main(int argc, const char *argv[])
{
int a;
int n = 0;
peach(10,1);
duck(8,2);
printf("please enter an integer number:");
scanf("%d",&a);
int temp = a;
while(temp / 10 > 0){
temp = temp / 10;
n++;
}
fun(a,n);
printf("\n");
return 0;
}