[英]reverse print using recursion
我剛開始使用遞歸,似乎無法理解以下將如何發生。 我想打印從i = 2
到i = 6
的值。 目前,它正在以相反的順序打印。 請你幫助我好嗎?
int l[][7] ={{0,0,0,0,0,0,0},{0,0,1,2,1,1,2},{0,0,1,2,1,2,2}};
int lstar = 1;
int recprint(int n, int i){
if (i>n)
{
return lstar;
}
else{
int m = recprint(n, i+1);
printf("i is %d and l[%d][%d] is %d \n", i, m, i , l[m][i]);
return l[m][i];
}
}
recprint(6, n);
output 需要的是
i is 6 and l[1][6] is 2
i is 5 and l[2][5] is 2
i is 4 and l[2][4] is 1
i is 3 and l[1][3] is 2
i is 2 and l[2][2] is 1
相反
void recprint(int i, int j, int val = 1) {
if (i < j) {
return;
}
recprint(i - 1, j, l[val][i]);
printf("i is %d and l[%d][%d] is %d\n", i, val, i, l[val][i]);
}
recprint(6, 2);
Output:
i is 2 and l[2][2] is 1
i is 3 and l[1][3] is 2
i is 4 and l[2][4] is 1
i is 5 and l[2][5] is 2
i is 6 and l[1][6] is 2
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.