登录
  • #刷题

Recursion题目求解

kakachelsea
1490
5
最近在自学CS61A(python),做homework3的时候遇到一道题,想了半天也解决不了,希望大家帮忙想个算法。。。谢谢!

题目的关键是不能有任何assignment语句,也就是要用recursion,可我想不出做法。

题目如下:

Write a function that takes a positive integer n and returns the number of ten-pairs it contains. A ten-pair is a pairs of digits within n that sum to 10. Do not use any assignment statements.The number 7,823,952 has 3 ten-pairs. The first and fourth digits sum to 7+3=10, the second and third digits sum to 8+2=10, and the second and last digit sum to 8+2=10:

def ten_pairs(n): """Return the number of ten-pairs within positive integer n.

>>> ten_pairs(7823952)

3

>>> ten_pairs(55055)

6

>>> ten_pairs(9641469)

6

"""
5条回复
热度排序

发表回复