擅长:python、mysql、java
<p>让我们一步一步地走完这一步。听起来我们需要一个函数,给定两个初始输入,生成描述项链的列表,包括最后两个输出</p>
<pre class="lang-py prettyprint-override"><code>from typing import List
def make_necklace(a: int, b: int) -> List[int]:
necklace = [a, b]
</code></pre>
<p>很容易得出下一个数字:</p>
<pre class="lang-py prettyprint-override"><code> # we don't really know when we'll leave the loop, so loop forever and handle it later
while True:
# add the last two elements and modulo by 10 to keep only the 1s digit
next_link = (necklace[-1] + necklace[-2]) % 10
necklace.append(next_link)
</code></pre>
<p>但要想知道什么时候该停下来就更难了。停止条件是什么?当最后两个元素与前两个元素相同时?等等:我们可以比较列表元素,对吗?让我们这样做吧</p>
<pre class="lang-py prettyprint-override"><code> if necklace[0:2] == necklace[-2:]:
break # leave the infinite loop
</code></pre>
<p>我们一出去,就把项链还给你</p>
<pre class="lang-py prettyprint-override"><code> return necklace
</code></pre>
<p>现在,调用此函数将为您提供整个项链链,您应该可以轻松地从中显示其余的需求</p>