Demo50
**
**
Enter a number (0: for end of input): 3
Enter a number C0: for end of input): 5
Enter a number C0: for end of input): 2
Enter a number C0: for end of input): 5
Enter a number (0: for end of input): 5
Enter a number C0: for end of input): 5
Enter a number (0: for end of input): 0
The largest number is 5
The occurrence count of the largest number is 4
程序编辑:
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)
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/50.py
Enter a number (0: for end of input): 3
Enter a number (0: for end of input): 5
Enter a number (0: for end of input): 2
Enter a number (0: for end of input): 5
Enter a number (0: for end of input): 5
Enter a number (0: for end of input): 5
Enter a number (0: for end of input): 0
The largest number is 5
The occurrence count of the largest number is 4
Process finished with exit code 0
Demo51
**
**
程序编辑:
# 方法一:
# 分别为转二进制、八进制、十六进制
# 转换的结果为字符串
# >>> bin(18)
# '0b10010'
# >>> oct(18)
# '0o22'
# >>> hex(18)
# '0x12'
Dec = int(input("请输入一个十进制数:"))
print(bin(Dec))
# 方法二:
a = int(input("请输入一个十进制整数:"))
print("{}对应二进制为{:b},八进制为{:o},"
"十六进制为{:x}".format(a,a,a,a))
#方法三:
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)
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/51.py
请输入一个十进制数:12
0b1100
请输入一个十进制整数:12
12对应二进制为1100,八进制为14,十六进制为c
Enter a number:12
1100
Process finished with exit code 0
Demo52
**
**
程序编辑:
# 方法一:
# 分别为转二进制、八进制、十六进制
# 转换的结果为字符串
# >>> 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)
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/52.py
请输入一个十进制数:13
0xd
请输入一个十进制整数:13
13对应二进制为1101,八进制为15,十六进制为d
Enter a number:13
d
Process finished with exit code 0
Demo53
**
程序编辑:
# 方法一:
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))
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/53.py
1000000个随机点落在奇数区域的概率是0.625219000000
1000000个随机点落在奇数区域的概率是0.625204000000
Process finished with exit code 0
Demo54
**
**
程序编辑:
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()
测试:
J:\python\python.exe K:/python2020.8.4练习/PythonCode/day1/day03/54.py
2004 2008 2012 2016 2020 2024 2028 2032 2036 2040
2044 2048 2052 2056 2060 2064 2068 2072 2076 2080
2084 2088 2092 2096
Process finished with exit code 0