-4
有人可以解释这行代码何时结束吗? :解释这行代码
void constituteSubsequence(int i){
if(Pred[i] + 1) constituteSubsequence(Pred[i]);
cout << a[i] << " ";
}
在这个程序计算最长递增子:
#include <iostream>
using namespace std;
int Pred[1000]; //Pred is previous.
int a[1000], v[1000], n, imax;
void read() {
cout << " n = ";
cin >> n;
cout << " Sequence: ";
for (int i = 0; i < n; i++) {
cin >> a[i];
}
}
void constituteSubsequence(int i) {
if (Pred[i] + 1) constituteSubsequence(Pred[i]);
cout << a[i] << " ";
}
void calculate() {
int i, j;
v[0] = 1;
imax = 0;
Pred[0] = -1;
for (int i = 1; i < n; i++) {
v[i] = 1;
Pred[i] = -1;
for (int j = 0; j < i; j++) {
if (a[j] < a[i] && v[j] + 1 > v[i]) {
v[i] = v[j] + 1;
Pred[i] = j;
}
if (v[i] > v[imax]) {
imax = i;
}
}
}
}
void write() {
cout << " Longest Increasing Subsequence : ";
constituteSubsequence(imax);
cout << endl << " Length: " << v[imax];
}
int main() {
read();
calculate();
write();
return 0;
}
如果我运行这段代码,它编译和按预期工作,但如何这种状态何其它找到了一个0后价值(假),它打印cout < < a [i]? 。它何时停止?
的可能的复制[解释这行代码做(http://stackoverflow.com/questions/35657778/explain-what-line-of-code-does) – Zulan