赞
踩
提示:最好还是先思考,先编写,再看答案哦 ^_^
11.
- X = [[12,7,3],
- [4,5,6],
- [7,8,9]]
-
- Y = [[5,8,1],
- [6,7,3],
- [4,5,9]]
-
- result = [[0,0,0],
- [0,0,0],
- [0,0,0]]
-
- # 迭代输出行
- for i in range(len(X)):
- # 迭代输出列
- for j in range(len(X[0])):
- result[i][j] = X[i][j] + Y[i][j]
-
- for r in result:
- print(r)
12.
- def inp(numbers):
- for i in range(9):
- numbers.append(int(input('input a number:')))
- numbers.append(int(input('input a number:')))
- 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)
13.
- import random,time
- time1=time.time()
- n=50
- k=20000000
- c=[]
- #c=[1,2,3,4,5]
- for i in range(n):
- c.append(random.randint(1,100))
- cs=[1 if i==0 else 0 for i in range(n+1)]
- ta=[]
- for j in range(n+1):
- for i in range(n-j):
- cs[i+1]+=cs[i]
- ta.append(cs[-1])
- del cs[-1]
- ta[0]+=ta[-1]
- del ta[-1]
- k_n=k//n
- kn=k%n
- for i in range(k_n):
- cc=c.copy()
- for j in range(n):
- xx=0
- for k in range(n):
- xx=xx+(cc[k]*ta[k])%100
- c[j]=xx%100
- cc=cc[1:]+cc[:1]
-
- for i in range(kn):
- c.append(c[0])
- for j in range(n):
- c[j]=(c[j]+c[j+1])%100
- del c[-1]
- print(c,time.time()-time1)
14.
- num = int(input('多少人围成一圈:'))
- list1 = list(range(1, num + 1))
- n = 2 % len(list1)
- while len(list1) > 1:
- list1.pop(n)
- n = (n + 2) % len(list1)
- print('最后留下的是原来第%d号'%list1[0])
15.
- def even_sum(n):
- s1 = 0
- for i in range(2, n+1, 2):
- s1 += 1.0 / i
- return s1
-
- def odd_sum(n):
- s2 = 0
- for i in range(1, n+1, 2):
- s2 += 1.0 / i
- return s2
-
- def my_sum(n):
- if n % 2 == 0:
- s = even_sum(n)
- else :
- s = odd_sum(n)
- return s
- if __name__ == '__main__':
- n = int(input('input a number:\n'))
- sum = my_sum(n)
- print(sum)
16.
- monkey=1#要分桃的猴子数
- peaches=1#桃的总数
- peach=1#每次分桃的总数
-
- while(monkey<=5):
- #如果成功分桃子
- if peach % 5 ==1 and peach // 5 != 0:
- #可分桃的总数为现在的4/5
- peach=(peach // 5)*4
- # 换下一个猴子
- monkey += 1
- # 如果失败,重新分桃这时可分桃数量加1
- else:
- peaches += 1
- peach = peaches
- monkey = 1
- print("桃的最小总数为:%s" %peaches)
17.
- def calc(list1):
- min = abs(list1[0] - list1[1])
- for i in range(len(list1)):
- for j in range(i+1, len(list1)):
- if abs(list1[i] - list1[j]) < min:
- min = abs(list1[i] - list1[j])
- return min
-
- if __name__ == "__main__":
- test = [1, 3, 10, 5, 23, 50]
- print(calc(test))
18.
- import re
- list1 = input('请输入一组数字:')
- list1 = re.findall(r'\d', list1)
-
- for i in range(len(list1)):
- list1[i] = int(list1[i])
-
- list1.reverse()
- for each in list1:
- print(each, end = ' ')
19.
- import os
- while True:
- filename=input('请输入文件名:')
- if os.path.exists(filename):
- print('此文件已存在!')
- break
- else:
- all=[]
- while True:
- content=input('请加入内容:')
- if content=='.':
- print('退出')
- break
- else:
- all.append(content)
- fobj=open(filename,'w')
- str_list=[i+'\n'for i in all]
- fobj.writelines(str_list)
- fobj.close()
- break
20.
创建游戏画布:
- from tkinter import *
- import random
- import time
-
- tk = Tk()
- tk.title("Game")
- tk.resizable(0,0)
- tk.wm_attributes("-topmost",1)
- canvas = Canvas(tk,width=500,height=400,bd=0,highlightthickness=0)
- canvas.pack()
- tk.update()
创建 ball 类:
- class Ball:
- def __init__(self,canvas,color):
- self.canvas = canvas
- self.id = canvas.create_oval(10,10,25,25,fill=color)
- self.canvas.move(self.id,245,100)
- def draw(self):
- pass
创建主循环:
- while 1:
- tk.update_idletasks()
- tk.update()
- time.sleep(0.01)
创建实例化对象:
ball = Ball(canvas,'red')
(未完待续,持续更新)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。