PythonNOJ习题前二十道(西北工业大学cpSkill实验平台)

it2023-08-02  84

第一题 输出Hello World

print("Hello World")

第二题 输入两个整数A、B,输出它们的和

a = int(input()) b = int(input()) c = a+b print(c)

第三题 输入一个浮点数和一个整数,计算保留n位小数的四舍五入的值

= float(input()) b = int(input()) print(round(a,b))

第四题 输入两个正整数,输出他们各自的二进制和逻辑与运算

a = int(input()) b = int(input()) c= a & b print(bin(a),bin(a),c,end = '')

第五题 输入一个整数n和一个字符a,输出n对应的ASCll 码值和a的ASCll 码值。

a = int(input()) b = input() print(chr(a),ord(b))

第六题 输入一个整数,输出它的十六进制,八进制,二进制

a = int(input()) print(oct(a),hex(a),bin(a),sep = ',')

第七题 输入一个负整数,输出4行格式输出结果

a = int(input()) b = abs(a); print('{:<10}'.format(a)) print('{:>10}'.format(a)) print('{:<10}'.format('+%d'%b)) print('{:>10}'.format('+%d'%b))

第八题 输入一个浮点数,输出它的4个格式结果

a = float(input()) print(round(a,6),round(a,2),round(a,8),'{:.6e}'.format(a),'{:,}'.format(a),sep='/')

第九题 输入一个整数,输出它的各种进制表示

a = int(input()) print('{:b}'.format(a),'{:o}'.format(a),'{:#o}'.format(a),'{:x}'.format(a),'{:#x}'.format(a),'{:#X}'.format(a),sep = ',')

第十题 输入整数m和n,输出n位长的m的二进制,若n超出实际长度,则左边填充0

a = int(input()) b = int(input()) c = int('{:b}'.format(a)) d = c count = 0 while d > 0: count = count+1 d = d//10 if count < b: for i in range(b-count): print('0',end= '') print(c) else : print(c)

第十一题 将一个直角坐标转换为极坐标

import cmath a = int(input()) b = int(input()) cn = complex(a,b) ret = cmath.polar(cn) print('{:.4f}'.format(ret[0]),'{:.4f}'.format(ret[1]),sep = ',')

第十二题 将一个浮点数转为比率

a = float(input()) b = int(a*10) c = 10 d = 1 if b%2==0 and c%2==0: b = b//2 c = c//2 print(b,c,sep='/') elif b%5==0 and c%5==0: b = b//5 c = c//5 print(b,c,sep='/') else: print(b,d,sep='/')

第十三题 勾股定理

import math a = int(input()) b = int(input()) max = a if a > b else b min = a if a < b else b def isTriangle(a, b, c): # 判断是否三角形 if a + b > c and a + c > b and b + c > a and abs(a - b) < c and abs(a - c) < b and abs(c - b) < a: return True else: return False condition1 = int(math.sqrt(math.pow(max, 2) - math.pow(min, 2))) condition2 = int(math.sqrt(math.pow(max, 2) + math.pow(min, 2))) if isTriangle(min, max, condition2) and math.pow(min, 2) + math.pow(max, 2) == math.pow(condition2,2) and condition2 > max: print("c") elif isTriangle(min, max, condition1) and math.pow(min, 2) + math.pow(condition1, 2) == math.pow(max, 2): if condition1 < min: print("a") if max > condition1 > min: print("b") else: print("non")

第十四题 对一个浮点数f指定n位小数输出且四舍五入

a = float(input()) n = int(input()) print(round(a,n))

第十五题 输入经纬度计算距离

import math lat1 = float(input()) #lat 纬度 lon 经度 lon1 = float(input()) lat2 = float(input()) lon2 = float(input()) result = (1-math.cos(math.radians(lat2-lat1)))/2 + math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * ((1-math.cos(math.radians(lon2-lon1)))/2) d = 2 * math.asin(math.sqrt(result)) * 6371 print("%.4fkm" % d)

第十六题 风寒指数

import math v = float(input()) T = float(input()) W = 13.12 + 0.6215 * T - 11.35 * math.pow(v, 0.16) + 0.3965 * T * math.pow(v, 0.16) print(round(W))

第十七题 输入两个分数,计算加减乘除

import fractions a = int(input()) b = int(input()) c = int(input()) d = int(input()) num1 = fractions.Fraction(a, b) num2 = fractions.Fraction(c, d) temp = num1 + num2 print("(", num1, ")+(", num2, ")=", temp, sep='') temp = num1 - num2 print("(", num1, ")-(", num2, ")=", temp, sep='') temp = num1 * num2 print("(", num1, ")*(", num2, ")=", temp, sep='') temp = num1 / num2 print("(", num1, ")/(", num2, ")=", temp, sep='')

第十八题 计算圆柱体积和面积

import math h = float(input()) r = float(input()) v = math.pi * math.pow(r,2) * h s = 2 * math.pi * math.pow(r,2) + 2 * math.pi * r * h print("%.4f"% v) print("%.4f"% s)

第十九题 计算狗的年龄

def getYear(year): a = 10.5 if year == 0: return 0 elif year == 1: return a elif year == 2: return 2 * a else: ret = (year - 2) * 4 + 2 * a return ret n = int(input()) y = getYear(n) print(int(y))

第二十题 RGB转换HSV

def rgb2hsv(R, G, B): R, G, B = R / 255.0, G / 255.0, B / 255.0 # R,G,B在 0 到 1 之间 Max = max(R, G, B) # 最大值 Min = min(R, G, B) # 最小值 m = Max - Min V = Max if V == 0: S = 0 else: S = m / Max if Max == Min: H = 0 elif Max == R: H = ((G - B) / m) * 60 elif Max == G: H = ((B - R) / m) * 60 + 120 elif Max == B: H = ((R - G) / m) * 60 + 240 if H < 0: H = H + 360 return H, S, V r = int(input()) g = int(input()) b = int(input()) h, s, v = rgb2hsv(r, g, b) print("%.4f" % h, "%.4f%%" % (s * 100), "%.4f%%" % (v * 100), sep=",")
最新回复(0)