在Lisp中最长递减顺序 [英] Longest decreasing sequence in Lisp

查看:144
本文介绍了在Lisp中最长递减顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一些问题,为我即将到来的考试,我需要一些帮助,这Lisp函数。我在工作中CLISP。 我一定要找到最长的递减顺序仅由奇数在列表中。 例如:

I'm working on some problems for my upcoming exam and I need some help with this Lisp function. I'm working in CLISP. I have to find the longest decreasing sequence comprised only of odd numbers in a list. Example:

(longest '(13 9 3 7 4 7 5 3 2 8 15 11 9 7 3))

应该返回:

(15 11 9 7 3)

唯一的硬性要求是,该功能已被递归执行:)

The only mandatory requirement is that that the function has to be implemented recursively :)

推荐答案

通过连续的序列,这很容易。除了我不口齿不清,所以我要解释一下的话。

With contiguous subsequences, it's easy. Except I don't lisp, so I have to explain it in words.

  1. 在调用一个递归帮手,带)的其他参数的)最长的出现c至今二)长度当前子D)多长。最初,这些是()0()0。
  2. 在列表的头部连,复发的尾巴。
  3. 开始电流与第一奇遭遇,再次出现在尾部。
  4. 如果头是偶数,或头部不大于previous元件越小,当前序列停止。比较其长度为previously发现最长的序列。如果当前的较长,则成为新的最长,忘了其它电流。进入2。
  1. Call a recursive helper, with additional arguments a) longest found so far b) length of that c) current subsequence d) length thereof. Initially these are () 0 () 0.
  2. While head of list is even, recur on tail.
  3. Start current with the first odd encountered, recur on tail.
  4. If head is even, or head is not smaller than the previous element, the current sequence stops. Compare its length to the previously found longest sequence. If current is longer, it becomes the new longest, else forget current. Go to 2.

当到达列表的末端,两个想起列表的不再是答案

When the end of the list is reached, the longer of the two remembered lists is the answer.

这篇关于在Lisp中最长递减顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆