赞
踩
- n = int(input("请输入你想要输入数的个数:"))
- i = 1
- ls = []
- count = 0
- while i <= n:
- ls.append(int(input(f"请输入第{i}个整数:")))
- i += 1
- print(f'您输入的数字有:{ls}')
- print(f'这些数的最大值为:{max(ls)}')
- for i in ls:
- if i == max(ls):
- count += 1
- print(f'{max(ls)}出现的次数为{count}')
- print("求一个十进制数的二进制数")
-
-
- def dec2bin(x):
- x -= int(x)
- bins = []
- while x:
- x *= 2
- bins.append(1 if x >= 1. else 0)
- x -= int(x)
- return bins
-
-
- if __name__ == '__main__':
- a = float(input('请输入一个数:'))
- yuan = a
- a = str(a)
- tmp = a.split('.')
- qian = tmp[0] # 小数点前的数
- hou = tmp[1] # 小数点后的数
-
- qian = int(qian)
- lsz = [] # lsz表示整数部分
- # 整数部分计算:
- while qian != 0:
- z = qian % 2
- lsz.append(int(z))
- qian //= 2
- if qian == 0:
- ls = [0]
- lsz.reverse()
- zheng = str()
- for i in lsz:
- zheng += str(i)
-
- # 小数部分计算:
- b = len(hou)
- hou = int(hou)
- hou = hou / (10 ** b)
- lsx = dec2bin(hou) # lsx表示小数部分
- xiao = str()
- for i in lsx:
- xiao += str(i)
- print(f"{yuan}的二进制数是{zheng}.{xiao}")
- import random
-
-
- a = 0
- for i in range(1000000):
- x = random.uniform(-1, 1)
- y = random.uniform(-1, 1)
- if x < 0 or (x > 0 and y > 0 and x + y <= 1):
- a += 1
- b = a / 1000000
- print("在奇数区域的概率:", b)
- count = 0
- for i in range(2001, 2100):
- if i % 4 == 0:
- print(i, end=' ')
- count += 1
- if count % 10 == 0:
- print()
- def sumdigits(n):
- count = 0
- str_n = str(n)
- for i in str_n:
- count += int(i)
- return count
-
-
- if __name__ == '__main__':
- a = int(input("请输入一个整数:"))
- print(f"{a}的各位数和为{sumdigits(a)}")
- def reverse(num):
- str_num = reversed(str(num))
- reverse_num = str()
- for i in str_num:
- reverse_num += str(i)
- return reverse_num
-
-
- def isPalindrome(num, reverse_num):
- str_num = str(num)
- str_reverse_num = str(reverse_num)
- kun = str()
- for j in str_reverse_num:
- kun += str(j)
- if kun == str_num:
- return True
- return False
-
-
- if __name__ == '__main__':
- a = int(input("请输入一个整数:"))
- b = reverse(a)
- print(f"{a}的反向数{b}")
- print(f"{isPalindrome(a, b)}")
- def huatoshe(fahrenheit):
- celsius = (5 / 9) * (fahrenheit - 32)
- return round(celsius, 2)
-
-
- def shetohua(celsius):
- fahrenheit = (9 / 5) * celsius + 32
- return round(fahrenheit, 2)
-
-
- print("------------------")
- print("摄氏度\t\t华氏度")
- print("------------------")
- for c in range(31, 41):
- f = shetohua(c)
- print(f"{c}\t\t\t{f}")
- print("------------------")
- print("华氏度\t\t摄氏度")
- print("------------------")
-
- for f in range(30, 120, 10):
- c = huatoshe(f)
- print(f"{f}\t\t\t{c}")
- def series_sum(n):
- x = 0
- for i in range(1, n + 1):
- x += i / (i + 1)
- return x
-
-
- if __name__ == '__main__':
- for j in range(1, 21):
- s = series_sum(j)
- print("%d\t%.4f" % (j, s))
- import math
-
-
- def isvalid(s1, s2, s3):
- if s1 + s2 > s3 and s1 + s3 > s2 and s2 + s3 > s1:
- return True
- else:
- return False
-
-
- def area(s1, s2, s3):
- d = cal(s1, s2, s3) / 2
- return math.sqrt((d * (d - s1) * (d - s2) * (d - s3)))
-
-
- def cal(s1, s2, s3):
- return s1 + s2 + s3
-
-
- if __name__ == '__main__':
- while True:
- a = float(input("请输入第一条边长度:"))
- b = float(input("请输入第二条边长度:"))
- c = float(input("请输入第三条边长度:"))
- if isvalid(a, b, c):
- print(f"面积{area(a, b, c)}")
- print(f"周长{cal(a, b, c)}")
- break
- else:
- print("输入的三边不能构成三角形!!")
- def reverse(num):
- str_num = reversed(str(num))
- reverse_num = str()
- for i in str_num:
- reverse_num += str(i)
- return reverse_num
-
-
- def isPalindrome(num, reverse_num):
- str_num = str(num)
- str_reverse_num = str(reverse_num)
- kun = str()
- for j in str_reverse_num:
- kun += str(j)
- if kun == str_num:
- return True
- return False
-
- def isPrime(num):
- for i in range(2, num // 2+1):
- if num % i == 0:
- return False
- return True
-
-
- if __name__ == '__main__':
- count = 0
- num = 1
- while count < 100:
- num += 1
- b = reverse(num)
- if isPrime(num) and isPalindrome(num, b):
- count += 1
- print("%5d" % num, end=" " if count % 10 else '\n')
- def num_isPrime(num):
- for i in range(2, num):
- if num % i == 0:
- return False
- return True
-
-
- def isPalindrome(num):
- t = str(num)
- if t == t[::-1]:
- return False
- return True
-
-
- def reverse_num_isPrime(num):
- str_num = reversed(str(num))
- reverse_num = str()
- for i in str_num:
- reverse_num += str(i)
- reverse_num = int(reverse_num)
- for i in range(2, reverse_num):
- if reverse_num % i == 0:
- return False
- return True
-
-
- if __name__ == '__main__':
- num, count = 1, 0
- while count < 100:
- num += 1
- if num_isPrime(num) and reverse_num_isPrime(num) and isPalindrome(num):
- count += 1
- print("%4d" % num, end=" " if count % 10 else '\n')
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。