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 . . .

猜你喜欢

转载自blog.csdn.net/qq_41905045/article/details/81184685
今日推荐