以下作业题仅为参考答案,为了锻炼思维的目的,所有的底层操作都是独立实现,尽量少的引包,大家对题有更好的思路和更方便的包欢迎大家多多留言,祝愿大家共同进步
1 hello word
略…
2A+B
A = int(input())
B = int(input())
print(A+B)
3N位小数
f = float(input())
n = int(input())
print(round(f,n))
4二进制(题目错误,只要第二个输出的也是a的二进制题目就能通过)
调用函数
a = int(input())
b = int(input())
print(bin(a),bin(b),a & b)
全部自己实现
a = int(input())
b = int(input())
def toBin(num):
s = ""
while num > 0:
s += str(num&1)
num >>= 1
l = list(s)
l.reverse()
return "".join(l)
print("0b"+toBin(a))
print("0b"+toBin(b))
5ASCII
n = int(input())
a = input()
print(chr(n),ord(a))
6进制转换
n = int(input())
print(oct(n), hex(n),bin(n), sep = ",")
7整数格式输出
a = int(input())
print("{:<10d}".format(a))
print("{:>10d}".format(a))
print("{:<+10d}".format(abs(a)))
print("{:>+10d}".format(abs(a)))
8浮点数输出
a = float(input())
print("{:<.6f}".format(a),"{:<.2f}".format(a),"{:<.8f}".format(a), "{:.6e}".format(a), "{:,}".format(a), sep = "/")
9各种表示
a = int(input())
print('{:b}'.format(a), '{:o}'.format(a), oct(a), '{:x}'.format(a), hex(a), '{:#X}'.format(a), sep = ",")
10动态宽度
m = int(input())
n = int(input())
def toBin(num):
s = ""
for i in range(0, n):
s += str(num&1)
num >>= 1
l = list(s)
l.reverse()
return "".join(l)
print(toBin(m))
11两个分数加减乘除
a = int(input())
b = int(input())
c = int(input())
d = int(input())
def simple(d):
a = d['molecule']
b = d['denominator']
while a%b != 0:
tmp=a%b
a = b
b = tmp
d['molecule'] /= b
d['denominator'] /= b
dict = {'molecule':a*d+b*c, 'denominator':b*d}
simple(dict)
print("("+str(a)+"/"+str(b)+")"+"+("+str(c)+"/"+str(d)+")"+"="+str(int(dict['molecule']))+"/" + str(int(dict['denominator'])))
dict['molecule'] = a*d-b*c
simple(dict)
print("("+str(a)+"/"+str(b)+")"+"-("+str(c)+"/"+str(d)+")"+"="+str(int(dict['molecule']))+"/" + str(int(dict['denominator'])))
dict['molecule'] = a*c
simple(dict)
print("("+str(a)+"/"+str(b)+")"+"*("+str(c)+"/"+str(d)+")"+"="+str(int(dict['molecule']))+"/" + str(int(dict['denominator'])))
dict['molecule'] = a*d
dict['denominator'] = b*c
simple(dict)
print("("+str(a)+"/"+str(b)+")"+"/("+str(c)+"/"+str(d)+")"+"="+str(int(dict['molecule']))+"/" + str(int(dict['denominator'])))
12计算狗的年龄
a = int(input())
if a<=2:
print(a*10.5)
else:
print(int(2*10.5+(a-2)*4))
13风寒指数
import math
v = float(input())
t = float(input())
chill = 13.12+0.6215*t-11.37*math.pow(v, 0.16)+0.3965*t*math.pow(v, 0.16)
print(round(chill))
14圆柱
import math
h = float(input())
r = float(input())
pai = 3.14159265
print("{:.4f}".format(pai*pow(r, 2)*h))
print("{:.4f}".format(pai*pow(r, 2)*2+2*pai*r*h))
15直角坐标转换为极坐标
import math
x = float(input())
y = float(input())
print("{:.4f}".format(math.sqrt(pow(x, 2)+pow(y, 2))), "{:.4f}".format(math.atan2(y, x)), sep = ",")
16给定经纬度计算地球上两点之间的距离
import math
h = float(input())
r = float(input())
pai = 3.14159265
print("{:.4f}".format(pai*pow(r, 2)*h))
print("{:.4f}".format(pai*pow(r, 2)*2+2*pai*r*h))
17勾股定理
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 condition1<max and condition1>min:
print("b")
else:
print("non")
18RGB转换HSV
R = int(input())/255
G = int(input())/255
B = int(input())/255
def max(a, b, c):
m = a if a > b else b
m = m if m > c else c
return m
def min(a, b, c):
m = a if a < b else b
m = m if m < c else c
return m
V = maximum = max(R, G, B)
minimum = min(R, G, B)
S = (maximum - minimum)/maximum
if maximum == R:
H = (G-B)/(V-minimum)
if maximum == G:
H = 2+(B-R)/(maximum-minimum)
if maximum == B:
H = 4+(R-G)/(maximum-minimum)
H *= 60
if H<0:
H += 360
print("{:.4f}".format(H), "{:.4%}".format(S), "{:.4%}".format(V), sep = ",")
19比率
f = float(input())
# 分数通分
def simple(d):
a = d['molecule']
b = d['denominator']
while a%b != 0:
tmp=a%b
a = b
b = tmp
d['molecule'] /= b
d['denominator'] /= b
# 判断小数位有几位
len = len(str(f))-len(str(int(f)))-1
dict = {'molecule':f*pow(10, len), 'denominator':pow(10, len)}
simple(dict)
print(str(int(dict['molecule']))+"/" + str(int(dict['denominator'])))
20.指定精度输入
f = float(input())
n = int(input())
s = str(f)[0:len(str(int(f)))+n+2]
if(int(s[len(s)-1])>4):
s2 = str(int(s[len(s)-2])+1)
else:
s2 = str(int(s[len(s)-2]))
s = s[0:len(str(int(f)))+n]+s2
print(s)
21.整数组合
import math
n = int(input())
m = int(input())
sum = 0
for i in range(m):
sum += n*int(math.pow(10, i))*(m-i)
print(sum)
# 解法为按位计算,第一次循环统计各位,第二次十位以此类推...每一位的个数随循环减少
# 555
# 55
# 5
# pow(10, i)效率还可以提升,利用迭代
sum = 0
tmp = 1
for i in range(m):
sum += n*tmp*(m-i)
tmp *= 10
print(sum)
22.组合数
n = int(input())
count = 0
for a in range(10):
for b in range(10):
for c in range(10):
d = n-a-b-c
if 0 <= d <= 9:
count += 1
print(count)
23对称数
integer = int(input())
def isSymmetry(a, b):
if (a=='9'and b=='6') or (a=='6'and b=='9') or a == b:
return True
else:
return False
flag = True
for i in range(len(str(integer))>>1):
if False == isSymmetry(str(integer)[i], str(integer)[len(str(integer))-i-1]):
flag = False
if flag:
print("Yes")
else:
print("No")
24.平行线
x1 = float(input())
y1 = float(input())
x2 = float(input())
y2 = float(input())
x3 = float(input())
y3 = float(input())
x4 = float(input())
y4 = float(input())
if(y2-y1)/(x2-x1) == (y4-y3)/(x4-x3):
print("Yes")
else:
print("No")
25.阶乘末尾
n = int(input())
product = 1
for i in range(1, n+1):
product *= i
num = 0
for i in range(len(str(product))):
if('0' == str(product)[len(str(product))-i-1]):
num+=1
else:
break
print(num)
26.操作数
n = int(input())
def count(n):
sum = 0
while(n > 0):
sum += int(n%10)
n /= 10
return sum
nums = 0
while n>0:
n -= count(n)
nums += 1
print(nums)
27.斐波那契数列
n = int(input())
def fib(n):
if 1 == n or 2 == n:
return 1
a = 0
b = 1
c = 1
for i in range(n-2):
a = b
b = c
c = a+b
return c
print(fib(n))
28.圆
import math
x1 = int(input())
y1 = int(input())
x2 = int(input())
y2 = int(input())
x3 = int(input())
y3 = int(input())
a=2*(x2-x1)
b=2*(y2-y1)
c=2*(x3-x2)
d=2*(y3-y2)
e=x2*x2+y2*y2-x1*x1-y1*y1
f=x3*x3+y3*y3-x2*x2-y2*y2
x=(b*f-d*e)/(b*c-d*a)
y=(c*e-a*f)/(b*c-d*a)
r=math.sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1))
print("{:.3f}".format(r), "{:.3f}".format(x), "{:.3f}".format(y), sep = ",")
29.回文数
integer = int(input())
def isSymmetry(a, b):
if a == b:
return True
else:
return False
flag = True
for i in range(len(str(integer))>>1):
if False == isSymmetry(int(str(integer)[i]), int(str(integer)[len(str(integer))-i-1])):
flag = False
if flag:
print("Yes")
else:
print("Not")
代码改进
n = int(input())
s = str(n)
res = ""
for i in range(len(str(n))):
res += str(s[i])
if int(res) == n:
print("Yes")
else:
print("Not")
30.方程组
a = float(input())
b = float(input())
c = float(input())
d = float(input())
e = float(input())
f = float(input())
if a/d == b/e and a/d != c/f:
print("error")
else:
x=(c*e-f*b)/(a*e-d*b)
y=(c*d-a*f)/(b*d-e*a)
print("{:.3f}".format(x), "{:.3f}".format(y), sep = " ")