Python Learning Week Fourth: functions and recursive functions • Jobs 20141224

Source: Internet
Author: User

1 Write the program, complete the following topics:(1 points)

Topic content:

The first 10 items of a Fibonacci sequence are: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, and for an even number of Fibonacci numbers that have a maximum value of not more than N.


Input format:

A positive integer n, such as 100.


Output format:

The value of an even-numbered item, such as 2 + 8 + 34 = 44.


Input Sample:

100


Sample output:

44

time limit: 500ms memory limit: 32000kb


Note: Because the Fibonacci sequence function belongs to recursive invocation, there are a lot of repetitive computations in the inside, the calculation is used more time, the test platform on the test time-out, so you rewrite the next, through the loop to calculate.


Fibonacci Sequence Functions:

def fib (n): if n = = 0 or n = = 1:return 1else:return fib (n-1) + fib (n-2) sum = 0i = 0num = Int (raw_input ()) while FIB (i) < num:if fib (i)% 2 = = 0:sum = fib (i) i + = 1print sum



Custom functions:

Def fib_qiujie (n):     result = 0    sum = 0     i = 0    tmp1 = 0    tmp2  = 0    while result < n:         if i ==0 or i == 1:             tmp1 = 1             tmp2 = 1            result =  1        elif i > 1:             if result % 2 == 0:                 sum += result             result = tmp2 +tmp1             tmp1 = tmp2             tmp2 = result        i +=  1    print sumnum = int (Raw_input ()) Fib_qiujie (num)


2

Write the program to complete the following topics:(2 points)

Topic content:

If the January 1, 1800 is known as Week 3, then for a given year and month, the last day of the month is the date of the week.


Input format:

Two-line integer representing the year and month, respectively


Output format:

Days of the week, 0 for Sunday


Input Sample:

2033

12


Sample output:

6

time limit: 500ms memory limit: 32000kb

Def is_leap_year (year):      if year % 4 == 0 and  Year % 100 != 0 or Year % 400 == 0:          return True     else:          return falsedef  year_month_days (Year, Month):     if Month in  (1, 3, 5, 7,  8, 10, 12):         return 31    elif month in   (4,&NBSP;6,&NBSP;9,&NBSP;11):        return 30     elif is_leap_year (year):        return 29     else:        return 28def  year_days ( Year):    &nbsP;if is_leap_year (year):        return 366     else:        return 365         def  weekday_year_month (year, month):     weekday = 2                    For year in range (1800, year):         weekday  =  (Weekday + year_days (year)  )  % 7             for month in range (1, month + 1):         Weekday =  (Weekday + year_month_days (year,  Month))  % 7        print weekdayy = int (raw_ Input ()) M = int (RAW_input ())    weekday_year_month (y, m) 



3

Write the program to complete the following topics:(2 points)

Topic content:

As in the game of Nottingham, we want to move n plates on tower A to tower C through Tower B, then for any input n, give the steps to move.


Input format:

A positive integer n


Output format:

Steps to move


Input Sample:

2


Sample output:

Move 1 from A to B

Move 2 from A to C

Move 1 from B to C


time limit: 500ms memory limit: 32000kb


def Hanoi (N, A, B, c): if n = = 1:print ' Move ', N, ' from ', A, ' to ', C Else:hanoi (N-1, A, C, B) print ' Move ', N, ' from ', A, ' to ', C Hanoi (N-1, B, A, c) num = Int (raw_input ()) Hanoi (Num, ' A ', ' B ', ' C ')


This article from "Ops said: from rookie to veteran" blog, please be sure to keep this source http://liuqunying.blog.51cto.com/3984207/1594167

Python Learning Week Fourth: functions and recursive functions • Jobs 20141224

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.