Python 入门编程题(一)答案

Python 入门编程题:1~10(答案)

提示:最好还是先思考,先编写,再看答案哦 ^_^

1.

for i in range(1, 5):
	for j in range(1,5):
		for k in range(1, 5):
			if i != j and j!= k and i != k:
				print(i,j,k)

2.

i = int(input('净利润:'))
arr = [1000000,600000,400000,200000,100000,0]
rat = [0.01,0.015,0.03,0.05,0.075,0.1]
r = 0
for idx in range(0,6):
    if i > arr[idx]:
        r += (i - arr[idx]) * rat[idx]
        print((i-arr[idx]) * rat[idx])
        i = arr[idx]
print(r)

3.

import math
for i in range(10001):
	if math.sqrt(i + 100) == int(math.sqrt(i + 100)) and math.sqrt(i + 268) == int(math.sqrt(i + 268)):
		print(i)

4.

def leapyear(n):
    return True if (n % 4 == 0 and n % 100 != 0) or n % 400 == 0 else False

days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30 ]
year, month, day = [int(x) for x in input('input year/month/day: ').split('/')]
day2 = sum(days[:month - 1]) + day
if leapyear(year) and month > 2:
    day2 += 1
print(day2)

5.

L = []
a = int(input('请输入第一个整数:'))
b = int(input('请输入第二个整数:'))
c = int(input('请输入第三个整数:'))
L.append(a)
L.append(b)
L.append(c)
L.sort()
print(L)

6.

#斐波那契数列。
def fib(n):
        a,b = 1,1
        for i in range(n-1):
                a,b = b,a+b
        return a

7.

#输出 9*9 乘法口诀表
for i in range(1, 10):
	for j in range(1, i+1):
		print('%s * %s = %s' %(i, j, i * j), end = '    ')
	print()

8.

import math
leap = 1
h = 0
for m in range(101, 201):
	
	k = int(math.sqrt(m + 1))
	for i in range(2, k+1):
		if  m % i == 0:
			leap = 0
			break
	if leap == 1:
		print('%-4d' %m, end = '')
		h += 1
		if h % 10 == 0:
			print()
	leap = 1
print('The total is %d' % h)

9.

from math import sqrt
#判断n是否为素数
def isprime(n):
    if n <= 1:
        return 0
    m = int(sqrt(n))+1
    for x in range(2,m):
        if n%x == 0:
            return 0
    return 1
#利用递归分解n并打印质因数
def bprime(n):
    if isprime(n):
        print(n)
    else:
        x = 2
        while x <= int(n/2):
            if n%x == 0:
                print(x)
                return bprime(n/x)
            x = x + 1

10.

x2 = 1
for day in range(9,0,-1):
    x1 = (x2 + 1) * 2
    x2 = x1
print(x1)

(未完待续,持续更新)

    原文作者:来自江南的你
    原文地址: https://blog.csdn.net/qq_41556318/article/details/85043541
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞