这个是最令我无奈的DP了,算法什么难度都没有,关键是初始化和数组下标问题,下面代码中我标记一下,希望大家能够引起注意:
#include <iostream>
using namespace std;
static int flag = 1;
int num[32768];
int longer[32768];
int init(int n)
{
int i , longest = 1;
for (i = 0 ; i <=32767 ; i++)
longer[i] = 1;
for (i = 2;i <= n; i++)
for (int j = 1; j < i; j++)
{
if (num[i] <= num[j] && longer[i] < longer[j]+1)
{
longer[i] = longer[j]+1;
}
if (longer[i] > longest)
longest = longer[i];
}
return longest;
}
int main ()
{
int a, i;
while (cin >> a && a!=-1)
{
memset(num,0,sizeof(num));
i = 2;
num[1] = a;
while (scanf("%d",&num[i]) && num[i] != -1) i++;
cout<<"Test #"<<flag<<':'<<endl;
cout<<" maximum possible interceptions: "<<init(i-1)<<endl<<endl; //前导空格有两个,最好直接粘贴过来;
flag++;
}
return 0;
}