'''
【程序21】
题目:猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个
第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。
1.程序分析:采取逆向思维的方法,从后往前推断。
2.程序源代码:
'''
x2 = 1
for day in range(9,0,-1):
x1 = (x2 + 1) * 2
x2 = x1
print x1
'''
【程序22】
题目:两个乒乓球队进行比赛,各出三人。甲队为a,b,c三人,乙队为x,y,z三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a说他不和x比,c说他不和x,z比,请编程序找出三队赛手的名单。
1.程序分析:判断素数的方法:用一个数分别去除2到sqrt(这个数),如果能被整除,
则表明此数不是素数,反之是素数。
2.程序源代码:
'''
for i in range(ord('x'),ord('z') + 1):
for j in range(ord('x'),ord('z') + 1):
if i != j:
for k in range(ord('x'),ord('z') + 1):
if (i != k) and (j != k):
if (i != ord('x')) and (k != ord('x')) and (k != ord('z')):
print 'order is a -- %s\t b -- %s\tc--%s' % (chr(i),chr(j),chr(k))
'''
【程序24】
题目:有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13...求出这个数列的前20项之和。
1.程序分析:请抓住分子与分母的变化规律。
2.程序源代码:
'''
#方法一
a = 2.0
b = 1.0
s = 0
for n in range(1,21):
s += a / b
t = a
a = a + b
b = t
print s
#方法二
s = 0.0
for n in range(1,21):
s += a / b
b,a = a , a + b
print s
s = 0.0
for n in range(1,21):
s += a / b
b,a = a , a + b
print s
#方法三
l = []
for n in range(1,21):
b,a = a,a + b
l.append(a / b)
print reduce(lambda x,y: x + y,l)
'''
【程序25】
题目:求1+2!+3!+...+20!的和
1.程序分析:此程序只是把累加变成了累乘。
2.程序源代码:
'''
#方法一
n = 0
s = 0
t = 1
for n in range(1,21):
t *= n
s += t
print '1! + 2! + 3! + ... + 20! = %d' % s
#方法二
s = 0
l = range(1,21)
def op(x):
r = 1
for i in range(1,x + 1):
r *= i
return r
s = sum(map(op,l))
print '1! + 2! + 3! + ... + 20! = %d' % s
'''
【程序26】
题目:利用递归方法求5!。
1.程序分析:递归公式:fn=fn_1*4!
2.程序源代码:
'''
def fact(j):
sum = 0
if j == 0:
sum = 1
else:
sum = j * fact(j - 1)
return sum
for i in range(5):
print '%d! = %d' % (i,fact(i))
''
【程序27】
题目:利用递归函数调用方式,将所输入的5个字符,以相反顺序打印出来。
1.程序分析:
2.程序源代码:
'''
def palin(n):
next = 0
if n <= 1:
next = input()
print
print next
else:
next = input()
palin(n - 1)
print next
i = 5
palin(i)
print
'''
【程序28】
题目:有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后 问第一个人,他说是10岁。请问第五个人多大?
1.程序分析:利用递归的方法,递归分为回推和递推两个阶段。要想知道第五个人岁数,需知道第四人的岁数,依次类推,推到第一人(10岁),再往回推。
'''
def age(n):
if n == 1: c = 10
else: c = age(n - 1) + 2
return c
print age(5)
'''
【程序29】
题目:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
1. 程序分析:学会分解出每一位数,如下解释:(这里是一种简单的算法,师专数002班赵鑫提供)
2.程序源代码:
'''
x = int(raw_input("input a number:\n"))
a = x / 10000
b = x % 10000 / 1000
c = x % 1000 / 100
d = x % 100 / 10
e = x % 10
if a != 0:
print "there are 5 ",e,d,c,b,a
elif b != 0:
print "there are 4 ",d,c,b,a
elif c != 0:
print "there are 3 ",e,d,c
elif d != 0:
print "there are 2 ",e,d
else:
print "there are 1",e
'''
题目:一个5位数,判断它是不是回文数。即12321是回文数,个位与万位相同,十位与千位相同。
1.程序分析:同29例
2.程序源代码:
'''
x = int(raw_input("input a number:\n"))
x = str(x)
for i in range(len(x)/2):
if x[i] != x[-i - 1]:
print 'this number is not a huiwen'
break
print 'this number is a huiwen'
'''
程序31】
题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续
判断第二个字母。
1.程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母。
2.程序源代码:
'''
from sys import stdin
letter = stdin.read(1)
stdin.flush()
while letter != 'Y':
if letter == 'S':
print 'please input second letter'
letter = stdin.read(1)
stdin.flush()
if letter == 'a':
print 'Saturday'
elif letter == 'u':
print 'Sunday'
else:
print 'data error'
break
elif letter == 'F':
print 'Friday'
break
elif letter == 'M':
print 'Monday'
#break
elif letter == 'T':
print 'please input second letter'
letter = stdin.read(1)
stdin.flush()
if letter == 'u':
print 'Tuesday'
elif letter == 'h':
print 'Thursday'
else:
print 'data error'
break
elif letter == 'W':
print 'Wednesday'
else:
print 'data error'
letter = stdin.read(1)
stdin.flush()
'''
【程序32】
题目:按相反的顺序输出列表的值。
1.程序分析:
2.程序源代码:
a = ['one', 'two', 'three']
for i in a[::-1]:
print i
list_ = ['a', 'b', 'c', 'd']
list_.reverse()
print list_
'''
【程序33】
题目:按逗号分隔列表。
1.程序分析:
2.程序源代码:
L = [1,2,3,4,5]
s1 = ','.join(str(n) for n in L)
print s1
L = [1, 2, 3, 4, 5]
L = repr(L)[1:-1]
print L
'''
'''
【程序34】
题目:练习函数调用
1. 程序分析:
2.程序源代码:
'''
def hello_world():
print 'hello world'
def three_hellos():
for i in range(3):
hello_world()
if __name__ == '__main__':
three_hellos()
'''
【程序35】
题目:文本颜色设置
1.程序分析:
2.程序源代码:
#include <conio.h>
void main(void)
{
int color;
for (color = 1; color < 16; color++)
{
textcolor(color);/*设置文本颜色*/
cprintf("This is color %d\r\n", color);
}
textcolor(128 + 15);
cprintf("This is blinking\r\n");
}
'''
'''
【程序36】
题目:求100之内的素数
1.程序分析:
2.程序源代码:
'''
# 输出指定范围内的素数
# 用户输入数据
lower = int(input("输入区间最小值: "))
upper = int(input("输入区间最大值: "))
for num in range(lower,upper + 1):
# 素数大于 1
if num > 1:
for i in range(2,num):
if (num % i) == 0:
break
else:
print(num)
for i in range(2,100):
if 0 not in [i%n for n in range(2,i)]:
print (i)
'''
【程序37】
题目:对10个数进行排序
1.程序分析:可以利用选择法,即从后9个比较过程中,选择一个最小的与第一个元素交换,
下次类推,即用第二个元素与后8个进行比较,并进行交换。
2.程序源代码:
'''
if __name__ == "__main__":
N = 10
# input data
print 'please input ten num:\n'
l = []
for i in range(N):
l.append(int(raw_input('input a number:\n')))
print
for i in range(N):
print l[i]
print
# sort ten num
for i in range(N - 1):
min = i
for j in range(i + 1,N):
if l[min] > l[j]:min = j
l[i],l[min] = l[min],l[i]
print 'after sorted'
for i in range(N):
print l[i]
第二种方法:
a = []
for i in range(10):
a.append(input("entert the num:"))
print a
for i in range(9):
for j in range(i+1,10):
if a[i] > a[j]:
a[i],a[j] = a[j],a[i]
print a
'''
【程序38】
题目:求一个3*3矩阵对角线元素之和
1.程序分析:利用双重for循环控制输入二维数组,再将a[i][i]累加后输出。
2.程序源代码:
'''
if __name__ == '__main__':
a = []
sum = 0.0
for i in range(3):
a.append([])
for j in range(3):
a[i].append(float(raw_input("input num:\n")))
for i in range(3):
sum += a[i][i]
print sum
'''
【程序39】
题目:有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
1. 程序分析:首先判断此数是否大于最后一个数,然后再考虑插入中间的数的情况,插入后
此元素之后的数,依次后移一个位置。
2.程序源代码:
'''
if __name__ == '__main__':
# 方法一
a = [1,4,6,9,13,16,19,28,40,100,0]
print 'original list is:'
for i in range(len(a)):
print a[i]
number = int(raw_input("insert a new number:\n"))
end = a[9]
if number > end:
a[10] = number
else:
for i in range(10):
if a[i] > number:
temp1 = a[i]
a[i] = number
for j in range(i + 1,11):
temp2 = a[j]
a[j] = temp1
temp1 = temp2
break
for i in range(11):
print a[i]
# 方法二
# insrt another number
number = int(raw_input('input a number:\n'))
if number > a[len(a) - 1]:
a.append(number)
else:
for i in range(len(a)):
if a[i] > number:
a.insert(i,number)
print a
'''
【程序40】
题目:将一个数组逆序输出。
1.程序分析:用第一个与最后一个交换。
2.程序源代码:
'''
if __name__ == '__main__':
a = [9,6,5,4,1]
N = len(a)
print a
for i in range(len(a) / 2):
a[i],a[N - i - 1] = a[N - i - 1],a[i]
print a