先是c语言的版本:
#include <stdio.h>
main()
{
int a[11], s[11], m[11];
int i, j, max;
for (i = 1; i <= 10; i++) {
printf("input number a[%d]:/n", i);
scanf("%d", &a[i]);
m[i] = 1;
s[i] = -1;
}
m[1] = 1;
for (i = 1; i <= 10; i++)
for (j = 1; j < i; j++)
if (a[j] <= a[i] && m[j] + 1 > m[i] ) {
m[i] = m[j] + 1;
s[i] = j;
}
max = 1;
for (i = 1; i <= 10; i++)
// printf("%d/t", m[i]);
if (m[i] > m[max])
max = i;
i = max;
// printf("%d/n", i);
while (s[i] != -1) {
printf("%d/t", a[i]);
i = s[i];
}
printf("%d", a[i]);
}
然后是python版的:
a = [0]*11
s = [0]*11
m = [0]*11
print a
print s
print m
for i in range(1, 11):
a[i] = int(raw_input("input number a[%d]:/n" %(i)))
m[i] = 1
s[i] = -1
m[1] = 1
print a
print s
print m
for i in range(1,11):
for j in range(1, i):
if a[j] <= a[i] and m[j] + 1 > m[i]:
m[i] = m[j] + 1
s[i] = j
max = 1
for i in range(1, 11):
if m[i] > m[max]:
max = i
i = max
while s[i] != -1:
print a[i]
i = s[i]
print a[i]