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)
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))
# 方法一:
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)
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)
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))
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()
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)