一、
max = 0
count = 0
while True:
num = int(input("Enter a number (0: for end of input): "))
if num == 0:
break
if num > max:
max = num
count = 1
elif num == max:
count += 1
print("The largest number is ",max)
print("The occurrence count of the largest number is ",count)
二、
# 方法一:
import random
sum = 0
for i in range (0, 1000001):
x = random.random() * random.choice([-1,1])
y = random.random() * random.choice([-1,1])
if x<= 0 or ( x >= 0 and y >= 0 and (y / (1 - x) >= 1)):
sum += 1
a = sum / 1000000
print("1000000个随机点落在奇数区域的概率是%.12f"%a)
# 方法二:
import random
total = 1000000
count = 0
for i in range(0,total):
x = random.random() * 2 - 1 # random.random() 产生[0,1)之间任意一个小数
y = random.random() * 2 - 1 # random.random() 产生[0,1)之间任意一个小数
if -1 <= x <= 0 and -1 <= y <= 1:
count += 1
elif 0 <= x <= 1 and 0 <= y <= 1 and y / (1-x) <= 1:
count += 1
print("1000000个随机点落在奇数区域的概率是%.12f"%(count / total))
三、
# 方法一:
# 分别为转二进制、八进制、十六进制
# 转换的结果为字符串
# >>> bin(18)
# '0b10010'
# >>> oct(18)
# '0o22'
# >>> hex(18)
# '0x12'
Dec = int(input("请输入一个十进制数:"))
print(hex(Dec))
# 方法二:
a = int(input("请输入一个十进制整数:"))
print("{}对应二进制为{:b},八进制为{:o},"
"十六进制为{:x}".format(a,a,a,a))
# 方法三:
number = int(input("Enter a number:"))
hexStr = ""
while number != 0:
a = number % 16
if a < 10:
hexStr = str(a) + hexStr
else:
hexStr = chr(87 + a) + hexStr
number //= 16
print(hexStr)
四、
def sumDigits(n):
num = 0
for i in range(len(str(n))):
num += n % 10
n = n // 10
return num
print(sumDigits(234))
五、
while True:
tem = input("请输入带符号的温度:")
if tem[-1] == "C" or tem[-1] == "c":
a = float(tem[0:-1])
b = a * 1.8 + 32
c = format(b, ".2f")
result = str(c)
print("转换后的温度是:{}F".format(result))
elif tem[-1] == "F" or tem[-1] == "f":
a = float(tem[0:-1])
b = (a - 32) / 1.8
c = format(b, ".2f")
result = str(c)
print("转换后的温度是:{}C".format(result))
else:
print("格式输入错误")
六、
count = 0
for y in range(2001, 2100 + 1):
if y % 400 == 0 or y % 4 == 0 and y % 100 != 0:
print(y, end=" ")
count += 1
if count % 10 == 0:
print()
七、
def isValid(side1, side2, side3):
return side1 + side2 > side3 and side2 + side3 > side1 and side1 + side3 > side2
def area(side1, side2, side3):
s = (side1 + side2 + side3) / 2
return (s * (s - side1) * (s - side2) * (s - side3)) ** 0.5
s1, s2, s3 = eval(input("请输入三角形三边长:"))
if isValid(s1, s2, s3):
print("area is %.2f" % area(s1, s2, s3))
else:
print("Invalid")
八、
from math import*
def isprime(n):
for j in range(2,int(sqrt(n))+1):
if n%j==0:
return 0
return 1
def ishui(n):
n1=n[::-1]
if n1==n:
return 1
return 0
x=int(input())
i=0
n=2
while(i<x):
if(isprime(n) and ishui(str(n))):
print(n,end=' ')
i+=1
n+=1
九、
# 方法一:
# 分别为转二进制、八进制、十六进制
# 转换的结果为字符串
# >>> bin(18)
# '0b10010'
# >>> oct(18)
# '0o22'
# >>> hex(18)
# '0x12'
Dec = int(input("请输入一个十进制数:"))
print(bin(Dec))
#方法二:
number = int(input("Enter a number:"))
"""
数 / 几进制 = 商 ~ 余数 注意:当商为0时停止,倒着取余数得到二进制数
12 / 2 = 6 ~ 0
6 / 2 = 3 ~ 0
3 / 2 = 1 ~ 1
1 / 2 = 0 ~ 1
1100
"""
binStr = ""
while number != 0:
a = number % 2
binStr = str(a) + binStr
number //= 2
print(binStr)