Problem1:

圆柱体:编写程序,读取圆柱体的半径和高度,并使用以下公式计算圆的面
积和圆柱体的体积:Python圆柱体体积算法 python计算圆柱体体积代码_最大公约数
代码:

1.import math  
2.  
3.radius = float(input("Enter the radius of a cylinder:"))  
4.length = float(input("Enter the length of a cylinder:"))  
5.  
6.area = radius*radius*math.pi  
7.print("The area is", area)  
8.print("The volume is", area*length)

结果:

Python圆柱体体积算法 python计算圆柱体体积代码_Python圆柱体体积算法_02

Problem2:

整数中各位数字:编写程序,输入 0 到 1000 之间的一个整数,并将该整数中的各位数字中的奇数相乘;如该整数不在 0 到 1000 之间,提示重新输入新数字。
代码:

1.   num = input("Enter a number between 0 and 1000:")  
2.while int(num) < 0 or int(num) > 1000:  
3.    print("Please input a correct number")  
4.    num = input("Enter a number between 0 and 1000:")  
5.  
6.ans = 1  
7.for char in num:  
8.    if int(char) % 2 == 1:  
9.        ans *= int(char)  
10.  
11.print("The result is", ans)

结果:

Python圆柱体体积算法 python计算圆柱体体积代码_最大公约数_03

Problem3

数字检查:编写程序,提示用户输入一个整数,并检查该数字(1)是否可以
同时被 5 和 7 整除,(2)是否可以被 5 或 7 整除,(3)是否只可以被其中一个
整除,但不能同时被两者整除。
代码:

1.   num = int(input("Enter an integer:"))  
2.  
3.# 是否可以同时被 5 和 7 整除  
4.if num % 5 == 0 and num % 7 == 0:  
5.    print("Is", num, " divisible by 5 and 7? True")  
6.else:  
7.    print("Is", num, " divisible by 5 and 7? False")  
8.  
9.# 是否可以被 5 或 7 整除  
10.if num % 5 == 0 or num % 7 == 0:  
11.    print("Is", num, " divisible by 5 or 7? True")  
12.else:  
13.    print("Is", num, " divisible by 5 or 7? False")  
14.  
15.# 是否只可以被其中一个整除,但不能同时被两者整除  
16.if num % 5 == 0 and num % 7 != 0 or num % 5 != 0 and num % 7 == 0:  
17.    print("Is", num, " divisible by 5 or 7, but not both? True")  
18.else:  
19.    print("Is", num, " divisible by 5 or 7, but not both? False")

结果:

Python圆柱体体积算法 python计算圆柱体体积代码_最大公约数_04

Problem4:

三位数偶数:编写程序,输出由 1、2、5、8 这四个数字组成的每位数字都不相同的所有三位数偶数。
代码:

1.list = ['1', '2', '5', '8']  
2.For i in range(4):  
3. for j in range(4):  
4.     for k in [1, 3]:  
5.         if i != j and j != k and i != k:  
6.             str = list[i]+list[j]+list[k]  
7.             print(str)

结果:

Python圆柱体体积算法 python计算圆柱体体积代码_最大公约数_05

Problem5:

百钱买汽水问题:假设大瓶汽水 5 元一瓶,中瓶汽水 3 元一瓶,小瓶汽水 1 元 三瓶,现在有 100 块钱,想买 100 瓶汽水,列出所有的买法 (提示:4 种)。
代码:

1.   import math  
2.  
3.small_price, medium_price, big_price = 1/3, 3, 5  
4.total_money=100  
5.cnt=0  
6.  
7.for i in range(math.ceil(total_money/small_price)):  
8.    for j in range(math.ceil(total_money/medium_price)):  
9.        for k in range(math.ceil(total_money/big_price)):  
10.            if(i*small_price+j*medium_price+k*big_price==100 and i+j+k==100):  
11.                print("小瓶汽水数量:", i, "中瓶汽水数量:", j, "大瓶汽水数量:", k)  
12.                cnt+=1  
13.  
14.print("一共有",cnt,"种购买方法")

结果:

Python圆柱体体积算法 python计算圆柱体体积代码_最大公约数_06

Problem6:

乘方:编写程序,输入正整数a,按照以下格式输出乘方结果。
代码:

1.a = int(input("input a:"))  
2.  
3.for i in range(1, a+1):  
4. print(i, i+1, i**(i+1))

结果:

Python圆柱体体积算法 python计算圆柱体体积代码_Python圆柱体体积算法_07

Problem7:

阶乘:编写代码以按相反顺序打印阶乘结果。例如输入 n=10,文字输出为小 于等于n的所有合数的阶乘(注:n的阶乘是123*…*n;不能使用math.factorial()
函数)。
代码:

1.   def isPrime(a):  
2.    for i in range(2, a):  
3.        if a % i == 0:  
4.            return True  
5.    return False  
6.  
7.  
8.def printFactorial(a):  
9.    ans = 1  
10.    for i in range(1,a+1):  
11.        ans *= i  
12.    print(a,"!:",ans)  
13.  
14.  
15.num = int(input("input a num:"))  
16.for i in range(num+1,4,-1):  
17.    if isPrime(i):  
18.        printFactorial(i)

结果:

Python圆柱体体积算法 python计算圆柱体体积代码_Python圆柱体体积算法_08

Problem8:

计算最大公约数:找到两个整数的最大公约数 (the greatest common divisor:
GCD)
代码:

1.   num1 = int(input("Enter the first number:"))  
2.num2 = int(input("Enter the second number:"))  
3.  
4.for i in range(min(num1, num2), 0, -1):  
5.    if num1 % i == 0 and num2 % i == 0:  
6.        print("GCD of", num1, "and", num2, "is", i)  
7.        break  
8.  
9.  
10.def CommDevisor(m, n):  
11.  r = m % n  
12.  while r != 0:  
13.    m = n  
14.    n = r  
15.    r = m % n  
16.  return n  
17.  
18.print("GCD of", num1, "and", num2, "is", CommDevisor(num1,num2))

结果:

Python圆柱体体积算法 python计算圆柱体体积代码_最大公约数_09