最长单调递增子序列

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值