理解递归

#include <iostream>
#include <string>
using namespace std;
int box(int j)
{
    cout<<"你在第几排"<<endl;
    if(j==2) cout<<"我在第一排"<<endl;
    else
    {
        
        box(j-1);
    }
    cout<<"我在"<<j<<"排"<<endl;
}
int main()
{
    int j;
    scanf("%d",&j);
    box(j);
}

看电影

比如:第三排的人问第二排:我在第几排?

第二排的人问第一排:我在第几排?

第一排的人回答:我在第一排

然后

第二排通过第一排的回答知道了自己在第二排

第三排通过第二排的回答知道了自己在第三排

 

posted @ 2018-12-03 21:54  vincand  阅读(112)  评论(0)    收藏  举报