linux爱好者
分类: Python/Ruby
2017-01-09 21:18:19
题目:打印出杨辉三角形(要求打印出10行如下图)。
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': a = [] for i in range(10): a.append([]) for j in range(10): a[i].append(0) for i in range(10): a[i][0] = 1 a[i][i] = 1 for i in range(2,10): for j in range(1,i): a[i][j] = a[i - 1][j-1] + a[i - 1][j] from sys import stdout for i in range(10): for j in range(i + 1): stdout.write(str(a[i][j])) stdout.write(' ') print
以上实例输出结果为:
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 5 10 10 5 1 1 6 15 20 15 6 1 1 7 21 35 35 21 7 1 1 8 28 56 70 56 28 8 1 1 9 36 84 126 126 84 36 9 1
题目:查找字符串。
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- sStr1 = 'abcdefg' sStr2 = 'cde' print sStr1.find(sStr2)
以上实例输出结果为:
2
题目:画椭圆ellipse。
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': from Tkinter import * x = 360 y = 160 top = y - 30 bottom = y - 30 canvas = Canvas(width = 400,height = 600,bg = 'white') for i in range(20): canvas.create_oval(250 - top,250 - bottom,250 + top,250 + bottom) top -= 5 bottom += 5 canvas.pack() mainloop()
以上实例输出结果为:
题目:利用ellipse 和 rectangle 画图。。
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': from Tkinter import * canvas = Canvas(width = 400,height = 600,bg = 'white') left = 20 right = 50 top = 50 num = 15 for i in range(num): canvas.create_oval(250 - right,250 - left,250 + right,250 + left) canvas.create_oval(250 - 20,250 - top,250 + 20,250 + top) canvas.create_rectangle(20 - 2 * i,20 - 2 * i,10 * (i + 2),10 * ( i + 2)) right += 5 left += 5 top += 10 canvas.pack() mainloop()
以上实例输出结果为:
题目:一个最优美的图案。
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- import math class PTS: def __init__(self): self.x = 0 self.y = 0 points = [] def LineToDemo(): from Tkinter import * screenx = 400 screeny = 400 canvas = Canvas(width = screenx,height = screeny,bg = 'white') AspectRatio = 0.85 MAXPTS = 15 h = screeny w = screenx xcenter = w / 2 ycenter = h / 2 radius = (h - 30) / (AspectRatio * 2) - 20 step = 360 / MAXPTS angle = 0.0 for i in range(MAXPTS): rads = angle * math.pi / 180.0 p = PTS() p.x = xcenter + int(math.cos(rads) * radius) p.y = ycenter - int(math.sin(rads) * radius * AspectRatio) angle += step points.append(p) canvas.create_oval(xcenter - radius,ycenter - radius, xcenter + radius,ycenter + radius) for i in range(MAXPTS): for j in range(i,MAXPTS): canvas.create_line(points[i].x,points[i].y,points[j].x,points[j].y) canvas.pack() mainloop() if __name__ == '__main__': LineToDemo()
以上实例输出结果为:
题目:输入3个数a,b,c,按大小顺序输出。
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': n1 = int(raw_input('n1 = :\n')) n2 = int(raw_input('n2 = :\n')) n3 = int(raw_input('n3 = :\n')) def swap(p1,p2): return p2,p1 if n1 > n2 : n1,n2 = swap(n1,n2) if n1 > n3 : n1,n3 = swap(n1,n3) if n2 > n3 : n2,n3 = swap(n2,n3) print n1,n2,n3
以上实例输出结果为:
n1 = : 123 n2 = : 456 n3 = : 789 123 456 789
题目:输入数组,最大的与第一个元素交换,最小的与最后一个元素交换,输出数组。
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- def inp(numbers): for i in range(9): numbers.append(int(raw_input('input a number:\n'))) numbers.append(int(raw_input('input a number:\n'))) p = 0 def max_min(array): max = min = 0 for i in range(1,len(array) - 1): p = i if array[p] > array[max] : max = p elif array[p] < array[min] : min = p k = max l = min array[0],array[l] = array[l],array[0] array[9],array[k] = array[k],array[9] def outp(numbers): for i in range(len(numbers)): print numbers[i] if __name__ == '__main__': array = [] inp(array) max_min(array) outp(array)
以上实例输出结果为:
input a number: 6 input a number: 8 input a number: 3 input a number: 6 input a number: 4 input a number: 9 input a number: 334 input a number: 34 input a number: 23 input a number: 12 3 8 6 6 4 9 12 34 23 334
题目:有n个整数,使其前面各数顺序向后移m个位置,最后m个数变成最前面的m个数
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': n = int(raw_input('the total number is:\n')) m = int(raw_input('back m:\n')) def move(array,n,m): array_end = array[n - 1] for i in range(n - 1,-1,- 1): array[i] = array[i - 1] array[0] = array_end m -= 1 if m > 0:move(array,n,m) number = [] for i in range(n): number.append(int(raw_input('input a number:\n'))) print 'orignal number:',number move(number,n,m) print 'after moved:',number
以上实例输出结果为:
the total number is: 8 back m: 5 input a number: 2 input a number: 8 input a number: 6 input a number: 1 input a number: 78 input a number: 45 input a number: 34 input a number: 2 orignal number: [2, 8, 6, 1, 78, 45, 34, 2] after moved: [1, 78, 45, 34, 2, 2, 8, 6]
题目:有n个人围成一圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是原来第几号的那位。
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': nmax = 50 n = int(raw_input('请输入总人数:')) num = [] for i in range(n): num.append(i + 1) i = 0 k = 0 m = 0 while m < n - 1: if num[i] != 0 : k += 1 if k == 3: num[i] = 0 k = 0 m += 1 i += 1 if i == n : i = 0 i = 0 while num[i] == 0: i += 1 print num[i]
执行以上代码,输出结果:
$ python test.py 请输入总人数:34 10
题目:写一个函数,求一个字符串的长度,在main函数中输入字符串,并输出其长度。
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': s = raw_input('please input a string:\n') print 'the string has %d characters.' % len(s)
以上实例输出结果为:
please input a string: abc the string has 3 characters.
题目:编写input()和output()函数输入,输出5个学生的数据记录。
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- N = 3 #stu # num : string # name : string # score[4]: list student = [] for i in range(5): student.append(['','',[]]) def input_stu(stu): for i in range(N): stu[i][0] = raw_input('input student num:\n') stu[i][1] = raw_input('input student name:\n') for j in range(3): stu[i][2].append(int(raw_input('score:\n'))) def output_stu(stu): for i in range(N): print '%-6s%-10s' % ( stu[i][0],stu[i][1] ) for j in range(3): print '%-8d' % stu[i][2][j] if __name__ == '__main__': input_stu(student) print student output_stu(student)
以上实例输出结果为:
input student num: 2 input student name: aaa score: 89 score: 98 score: 67 input student num: bbb input student name: ccc score: 87 score: 45 score: 68 input student num: ddd input student name: eee score: 56 score: 78 score: 56 [['2', 'aaa', [89, 98, 67]], ['bbb', 'ccc', [87, 45, 68]], ['ddd', 'eee', [56, 78, 56]], ['', '', []], ['', '', []]] 2 aaa 89 98 67 bbb ccc 87 45 68 ddd eee 56 78 56
题目:创建一个链表。
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': ptr = [] for i in range(5): num = int(raw_input('please input a number:\n')) ptr.append(num) print ptr
以上实例输出结果为:
please input a number: 3 please input a number: 5 please input a number: 7 please input a number: 8 please input a number: 2 [3, 5, 7, 8, 2]
题目:反向输出一个链表。
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': ptr = [] for i in range(5): num = int(raw_input('please input a number:\n')) ptr.append(num) print ptr ptr.reverse() print ptr
以上实例输出结果为:
please input a number: 6 please input a number: 5 please input a number: 3 please input a number: 4 please input a number: 8 [6, 5, 3, 4, 8] [8, 4, 3, 5, 6]
题目:连接两个链表。
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': arr1 = (3,12,8,9,11) ptr = list(arr1) print ptr ptr.sort() print ptr
[3, 12, 8, 9, 11] [3, 8, 9, 11, 12]
题目:放松一下,算一道简单的题目。
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': for i in range(5): n = 0 if i != 1: n += 1 if i == 3: n += 1 if i == 4: n += 1 if i != 4: n += 1 if n == 3: print 64 + i
以上实例输出结果为:
67
题目:编写一个函数,输入n为偶数时,调用函数求1/2+1/4+...+1/n,当输入n为奇数时,调用函数1/1+1/3+...+1/n(利用指针函数)
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- def peven(n): i = 0 s = 0.0 for i in range(2,n + 1,2): s += 1.0 / i return s def podd(n): s = 0.0 for i in range(1, n + 1,2): s += 1 / i return s def dcall(fp,n): s = fp(n) return s if __name__ == '__main__': n = int(raw_input('input a number:\n')) if n % 2 == 0: sum = dcall(peven,n) else: sum = dcall(podd,n) print sum
以上实例输出结果为:
input a number: 6 0.916666666667
题目:循环输出列表
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': s = ["man","woman","girl","boy","sister"] for i in range(len(s)): print s[i]
以上实例输出结果为:
man
woman
girl
boy
sister
题目:找到年龄最大的人,并输出。请找出程序中有什么问题。
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': person = {"li":18,"wang":50,"zhang":20,"sun":22} m = 'li' for key in person.keys(): if person[m] < person[key]: m = key print '%s,%d' % (m,person[m])
以上实例输出结果为:
wang,50
题目:字符串排序。
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': str1 = raw_input('input string:\n') str2 = raw_input('input string:\n') str3 = raw_input('input string:\n') print str1,str2,str3 if str1 > str2 : str1,str2 = str2,str1 if str1 > str3 : str1,str3 = str3,str1 if str2 > str3 : str2,str3 = str3,str2 print 'after being sorted.' print str1,str2,str3
以上实例输出结果为:
input string: abcde input string: efdis input string: adk abcde efdis adk after being sorted. abcde adk efdis
题目:海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?
程序分析:无。
程序源代码:
#!/usr/bin/python # -*- coding: UTF-8 -*- if __name__ == '__main__': i = 0 j = 1 x = 0 while (i < 5) : x = 4 * j for i in range(0,5) : if(x%4 != 0) : break else : i += 1 x = (x/4) * 5 +1 j += 1 print x
以上实例输出结果为:
3121