1. 程式人生 > >python3 100例 一碼人學習筆記(81-90)

python3 100例 一碼人學習筆記(81-90)

題目81:809*??=800*??+9*?? 其中??代表的兩位數, 809*??為四位數,8*??的結果為兩位數,9*??的結果為3位數。求??代表的兩位數,及809*?? 

a = 809
for i in range(10,100):
    b = i * a
    if b >= 1000 and b <= 10000 and 8 * i < 100 and 9 * i >= 100:
        print(b,' = 800 * ', i, ' + 9 * ', i)
9708  = 800 *  12  + 9 *  12
Press any key to continue . . .

 題目82:

八進位制轉換為十進位制

 
if __name__ == '__main__':
    n = 0
    p = input('input a octal number:\n')
    for i in range(len(p)):
        n = n * 8 + ord(p[i]) - ord('0')
    print(n)
input a octal number:
18
16
Press any key to continue . . .

題目83:求0—7所能組成的奇數個數。

程式分析:

組成1位數是4個。

組成2位數是7*4個。

組成3位數是7*8*4個。

組成4位數是7*8*8*4個。

def f(n):
    if n == 0:
        return 1
    elif n == 1:
        return 7
    else:
        return f(n-1)*8
l = []
#算出每位數有多少奇數
for i in range(1,9):
    l.append(f(i-1)*4)
print(l)
#輸出一共有多少個奇數
print(sum(l))
[4, 28, 224, 1792, 14336, 114688, 917504, 7340032]
8388608
Press any key to continue . . .

題目84:

連線字串

delimiter = ','
mylist = ['Brazil', 'Russia', 'India', 'China']
print(delimiter.join(mylist))
Brazil,Russia,India,China
Press any key to continue . . .

題目85:輸入一個奇數,然後判斷最少幾個 9 除於該數的結果為整數。

def monkey():
    a=int(input('輸入一個數字:\n'))
    b=9
    i=1
    while True:
        if b > a and b%a==0:
            print('需要{}個9'.format(i))
            break
        else:
            b = b*10+9
            i+=1
if __name__ == '__main__':
    monkey()
輸入一個數字:
19
需要18個9
Press any key to continue . .

題目86:回答結果(結構體變數傳遞)

if __name__ == '__main__':
    class student:
        x = 0
        c = 0
    def f(stu):
        stu.x = 20
        stu.c = 'c'
    a= student()
    a.x = 3
    a.c = 'a'
    f(a)
    print(a.x,a.c)
20 c
Press any key to continue . . .

題目87:讀取7個數(1—50)的整數值,每讀取一個值,程式打印出該值個數的*。

import random

# 隨機輸出 3 個 1~10 的星個數
num=[]
s=['*']
for i in range(0,3):
    num.append(random.randint(1,10)) 
    print(" * 個數為:",num[i])
    print('*'*num[i])
 * 個數為: 1
*
 * 個數為: 4
****
 * 個數為: 5
*****
Press any key to continue . . .

題目88:某個公司採用公用電話傳遞資料,資料是四位的整數,在傳遞過程中是加密的,加密規則如下:每位數字都加上5,然後用和除以10的餘數代替該數字,再將第一位和第四位交換,第二位和第三位交換。

def func():
    n = str(input("請輸入四位整數"))
    a = str((int(n[:1])+5)%10)
    b = str((int(n[1:2])+5)%10)
    c = str((int(n[2:3])+5)%10)
    d = str((int(n[3:4])+5)%10)
    number = d+c+b+a
    print(number)
func()
請輸入四位整數1234
9876
Press any key to continue . . .

題目89:列表使用例項。

testList=[10086,'中國移動',[1,2,4,5]]  
  
#訪問列表長度  
print(len(testList)) 
#到列表結尾  
print(testList[1:] )
#向列表新增元素  
testList.append('i\'m new here!')  
  
print(len(testList)) 
print(testList[-1] )
#彈出列表的最後一個元素  
print(testList.pop(1) )
print(len(testList)  )
print(testList )
#list comprehension  
#後面有介紹,暫時掠過  
matrix = [[1, 2, 3],  
[4, 5, 6],  
[7, 8, 9]]  
print(matrix) 
print(matrix[1]) 
col2 = [row[1] for row in matrix]#get a  column from a matrix  
print(col2  )
col2even = [row[1] for row in matrix if  row[1] % 2 == 0]#filter odd item  
print(col2even)
3
['中國移動', [1, 2, 4, 5]]
4
i'm new here!
中國移動
3
[10086, [1, 2, 4, 5], "i'm new here!"]
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
[4, 5, 6]
[2, 5, 8]
[2, 8]
Press any key to continue . . .