一、计算max和max值出现的次数count
def max_count(n):
list=[random.randint(0,9)for i in range(n)]
print(list)
count=0
max=0
for i in list:
if i>max:
max=i
count=1
elif i==max:
count+=1
else:
pass
print(max,count)
max_count(12)
二、十进制转16进制
python中可以使用内置函数bin()、oct()、hex()将十进制数转换为二进制、八进制、十六进制数。
#使用hex()10->16
map = {10:'A',11:'B',12:'C',13:'D',14:'E',15:'F'}
n = int(input())
s = hex(n).replace("0x",'').upper()
print(s)
#使用int()16->10
s = input()
print(int(s,16))
$使用oct()16->8
x = int(input())
for i in range(x):
s = input()
print(str(oct(int(s, 16))).replace('0o',''))
#不用内置函数
map = {10:'A',11:'B',12:'C',13:'D',14:'E',15:'F'}
n = int(input())
s=''
while(n>0):
a = int(n%16)
n = int(n/16)
if (a <= 9):
s+=str(a)
else:
s+=map.get(a)
print(s[::-1])
number = int(input())
def Hex10_16(number):
hexStr = ""
while number != 0:
temp = number % 16
if temp < 10:
hexStr += str(temp)
else:
hexStr += chr(55 + temp)
number //= 16
return hexStr
hexStr = Hex10_16(number)
print("十进制数%d,对应的十六进制数是%s"%(number,hexStr))
三、测1百万次落入奇数区域的概率和次数
# 方法一:
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)
print("1000000个随机点落在奇数区域的次数是",sum)
# 方法二:
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))
print("1000000个随机点落在奇数区域的次数是",count)
四、显示2000-2100的闰年,每行10个,每个空格隔开
def run(m,n):
count = 0
for i in range(m, n + 1):
if i % 400 == 0 or i % 4 == 0 and i % 100 != 0:
print(i, end=" ")
count += 1
if count % 10 == 0:
print()
run(2000,2100)
五、计算整数各个位之和
def sumDigits(n):
sum = 0
while n != 0:
sum += n % 10
n = n // 10
print(sum)
sumDigits(11)
六、使用函数 reverse实现 isPalindrome。如果一个数的反向数和它的顺向数一样,那么这个数就被称为回文数。编写一个测试程序,提示用户输入一个整数,然后输出这个整数是不是回文数。
n = int(input("Enter a number:"))
def isPalindrome(n):
Num = reverse(n)
return n == Num
def reverse(n):
Num = 0
while n != 0:
Num = Num * 10 + n % 10
n //= 10
return Num
print(isPalindrome(n))
七、反向显示整数
n = int(input("Enter a number:"))
def reverse(n):
Num = 0
while n!= 0:
Num = Num * 10 + n % 10
n//= 10
return Num
print(reverse(n))
八、体温摄氏度与华氏度转化
def celsiusToFahrenheit(celsius):
Fahrenheit = (9 / 5) * celsius +32
return Fahrenheit
def fahrenheitToCelsius(fahrenheit):
celsius = (5 / 9) * (fahrenheit - 32)
return celsius
print("Celsius\t\tFahrenheit\t\tFahrenheit\tCelsius")
i = 40
for j in range(120, 20, -10):
print(i,end = "\t\t ")
print("%.1f"%(celsiusToFahrenheit(i)),end ="\t\t\t ")
print(j, end="\t\t ")
print("%.2f"%(fahrenheitToCelsius(j)))
i -= 1
if i == 30:
break
九、求和数列并显示结果
# 行数i 行数的值
# 显示行后清空值 sum = 0,以备下一行使用
def m(i):
sum = 0
print(i, end="\t\t\t")
for i in range(1,i+1):
sum += i / (i + 1)
return sum
print("i\t\t\tm(i)")
for i in range(1, 21):
print("%.4f"%m(i))
十、回文素数,既是回文数又是素数1-1000
count=0
for i in range(2, 1001):
for j in range(2, i):
if i % j == 0:
break
else:
n = str(i)
m = n[::-1]
if m == n:
print(i, end=' \t ')
count+=1
if count % 10 == 0:
print()
十一、(反素数)反素数(逆向拼写的素数)是指一个将其逆向拼写后也是一个素数的非回文数。例如:17和71都是素数,所以,17和71都是反素数。编写程序显示前100个反素数。每行显示10个数字,并且准确对齐。
def is_prime(n):
if n <= 1: # 小于2的数字单独处理
return True
for i in range(2, int(n ** (1 / 2) + 1)): # 根据素数定义判定是否是素数,是素数返回1
if n % i == 0:
return False
return True
def isPalindrome(num):
"""接收一个数字为参数,判定其是否为回文数,返回布尔值。"""
return str(num) == str(num)[::-1]
def reverse(num):
"""接收一个整数,返回其逆序字符串对应的整数"""
return int(str(num)[::-1])
def reverse_prime(number):
i = 2
count = 0
while True:
if not isPalindrome(i) and is_prime(i) and is_prime(reverse(i)):
print(i, end=' ') # i为回文素数时输出i,输出后不换行
count = count + 1
if count == number:
break
i = i + 1
if __name__ == "__main__":
n = int(input())
reverse_prime(n)