15.1-1习题答案

15.1-1习题答案

include 
using std::cin; using std::cout;
unsigned int l1[] = {0,0,1,2,1,1,2};
unsigned int l2[] = {0,0,1,2,1,2,2};
//打印前一个station的line
void print(unsigned station, unsigned line){
    if (station == 2){
        if (line == 1){
            cout << "line 1,station 1" << std::endl;
        }
        else{
            cout << "line 2,station 1" << std::endl;
        }
        return;
    }
    if (line == 1){
        print(station - 1, l1[station - 1]);
    }
    else{
        print(station - 1, l2[station - 1]);
    }
    cout << "line " << line << ",station " << station - 1 << std::endl;//打印上一次的结果
}
int main()
{
    print(7, 1);
    return 0;
}

每次打印的都是上一个station的line。

你可能感兴趣的:(算法导论习题答案)