怒刷python作業

2020-10-11 15:00:17

以下作業題僅為參考答案,為了鍛鍊思維的目的,所有的底層操作都是獨立實現,儘量少的引包,大家對題有更好的思路和更方便的包歡迎大家多多留言,祝願大家共同進步
在這裡插入圖片描述

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 = " ")