Python第四周:函数与递归函数

简介: 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_32502811/article/details/51160127 第一题:题目内容: 一个斐波那契数列的前10项为:1, 2, 3, 5, 8, 13, 21, 34, 55, 89,对于一个最大项的值不超过n的斐波那契数列,求值为偶数的项的和。
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_32502811/article/details/51160127

第一题:

题目内容:
一个斐波那契数列的前10项为:1, 2, 3, 5, 8, 13, 21, 34, 55, 89,对于一个最大项的值不超过n的斐波那契数列,求值为偶数的项的和。

输入格式:
一个正整数n,如100。

输出格式:
值为偶数的项的和,如 2 + 8 + 34 = 44。

输入样例:
100

输出样例:
44

```python

n = int(raw_input())
number = 1
even_sum = 0
def fib(num):
    if num == 1 or num == 2:
        return 1
    else:
        i = 2
        f1 = 1
        f2 = 1
        while i < num:
            f3 = f1 + f2
            f1 = f2
            f2 = f3
            i += 1
        return f3
def is_even(num):
    if num % 2 == 0:
        return True
    else:
        return False
while fib(number) < n:
    if is_even(fib(number)):
        even_sum += fib(number)
    number += 1
print even_sum

第二题:

题目内容:
若已知1800年1月1日为星期3,则对于一个给定的年份和月份,输出这个月的最后一天是星期几。

输入格式:
两行整数,分别代表年份和月份

输出格式:
星期数,0代表星期日

输入样例:
2033
12

输出样例:
6

```python
y=int(raw_input())
m=int(raw_input())
def is_leapyear(year):
    if year%4==0 and year%100!=0 or year%400==0:
        return True
    else:
        return False
def month_days(year,month):
    if month in (1,3,5,7,8,10,12):
        return 31
    elif month in (4,6,9,11):
        return 30
    elif is_leapyear(year):
        return 29
    else:
        return 28
def get_all_days(year,month):
    days=0
    for i in range(1800,year):
        if is_leapyear(i):
            days += 366
        else:
            days+=365
    for j in range(1,month+1):
        days += month_days(year,j)
    return days
def get_endday(year,month):
    return (2+get_all_days(year,month))%7
print get_endday(y,m)

第三题

题目内容:
如在汉诺塔游戏中,我们希望将塔A上的n个盘子,通过塔B移动到塔C,则对于任意输入的n,给出移动的步骤。

输入格式:
一个正整数n

输出格式:
移动的步骤

输入样例:
2

输出样例:
Move 1 from A to B
Move 2 from A to C
Move 1 from B to C

```python
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)
n=int(raw_input())
hanoi(n,'A','B','C')
目录
相关文章
|
13天前
|
Python
python函数的参数学习
学习Python函数参数涉及五个方面:1) 位置参数按顺序传递,如`func(1, 2, 3)`;2) 关键字参数通过名称传值,如`func(a=1, b=2, c=3)`;3) 默认参数设定默认值,如`func(a, b, c=0)`;4) 可变参数用*和**接收任意数量的位置和关键字参数,如`func(1, 2, 3, a=4, b=5, c=6)`;5) 参数组合结合不同类型的参数,如`func(1, 2, 3, a=4, b=5, c=6)`。
14 1
|
28天前
|
Python
Python函数使用(四)
Python函数使用(四)
60 0
|
1天前
|
数据挖掘 数据处理 索引
python常用pandas函数nlargest / nsmallest及其手动实现
python常用pandas函数nlargest / nsmallest及其手动实现
11 0
|
6天前
|
Serverless 开发者 Python
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
《Python 简易速速上手小册》第3章:Python 的函数和模块(2024 最新版)
38 1
|
6天前
|
索引 Python
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
Python高维变量选择:SCAD平滑剪切绝对偏差惩罚、Lasso惩罚函数比较
|
8天前
|
Python
python学习-函数模块,数据结构,字符串和列表(下)
python学习-函数模块,数据结构,字符串和列表
49 0
|
8天前
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
05-python之函数-函数的定义/函数的参数/函数返回值/函数说明文档/函数的嵌套使用/函数变量的作用域
|
9天前
|
Python
python学习10-函数
python学习10-函数
|
9天前
|
Python
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
python学习4-内置函数range()、循环结构、循环控制语句、else语句、嵌套循环
|
12天前
|
测试技术 开发者 Python
Python中的装饰器:优雅而强大的函数修饰工具
在Python编程中,装饰器是一种强大的工具,用于修改函数或方法的行为。本文将深入探讨Python中装饰器的概念、用法和实际应用,以及如何利用装饰器实现代码的优雅和高效。

热门文章

最新文章