最长单调递增子序列

先是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] 

阅读更多
文章标签: input python 语言 c
个人分类: 算法的一些代码
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

最长单调递增子序列

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭