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):
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):
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]
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):
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:
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')
(未完待續,持續更新) 程式設計題2 偶的答案
2.用標準的dom方法動態建立乙個包含文字 hello ajax 的div,用靜態css定義或者用指令碼設定style屬性,使得該div寬高都為500px,文字顏色為黃色,背景色為紅色,同時該div的文字垂直和水平方向都居中。咱最早的答案只是乙個帶style的div標籤。花了些時間來讓文字上下居中。...
程式設計珠璣課後題答案3 4
問題描述 給出兩個日期計算兩者的天數距離 給出乙個日期確定之周幾 列印出給出年月的日曆 參考 include include using namespace std 有效月份1 12 const int days everymonth 13 typedef struct date int which...
2023年試題程式設計題答案
1 從鍵盤輸入乙個可帶空格的字元產,判斷該字串是否是回文 回文指正向讀和逆向讀相同的字串,如abcddcba等 如果是回文,輸出 yes 否則輸入 no define crt secure no warnings include include void panduanhuiwen char a 書...