第三周练习

1、鸡兔同笼  程序设计题

n = eval(input())
l = list()
for k in range(n):
    a = int(input())
    if (a%2) != 0:
        min = max = 0
    else:
        j = int(a/4)
        i = a%4
        m = int(i/2)
        min = m+j
        max = int(a/2)
    l += [(min,max)]
for b,c in l:
    print(b,c,end='')
    print(end='\n')

2、算24

from itertools import permutations
n1 = input("")
n2 = input("")
n3 = input("")
n4 = input("")
n = n1+n2+n3+n4
sum = 1
for i in n:
    sum *= eval(i)
if sum < 24:
    print("NO")
    exit()
notation = ['+', '-', '*', "/"]
st = set()
num = 0
number = set(permutations(n))
for i in notation:
    s = i
    t1 = notation.copy()
    t1.remove(i)
    for j in t1:
        s += j
        t2 = t1.copy()
        t2.remove(j)
        for p in t2:
            s += p
            st.add(s)
            s = i+j
        s = i
newst = set()
for i in number:
    for j in st:
        newst.add(i[0]+j[0]+i[1]+j[1]+i[2]+j[2]+i[3])
# print(newst)
all = set()
for i in newst:
    i1 = '('+i[0:3]+')'+i[3:]
    i2 = i[0:2]+'('+i[2:5]+')'+i[5:]
    i3 = i[0:4] + '(' + i[4:] + ')'
    i4 = '(('+i[0:3]+')'+i[3:5]+")"+i[5:]
    i5 = i[0:2]+'(('+i[2:5]+')'+i[5:]+")"
    i6 = '(' + i[0:2] + '(' + i[2:5] + '))' + i[5:]
    i7 = i[0:2]+'('+i[2:4]+'('+i[4:]+"))"
    all.add(i1)
    all.add(i2)
    all.add(i3)
    all.add(i4)
    all.add(i5)
    all.add(i6)
    all.add(i7)
result = []
for i in all:
    try:
        if eval(i) == 24:
          result.append(i)
    except:
        pass
print("YES")
print("("+sorted(result)[0]+")")

3、温度转换异常处理

try:
    TempStr = input()
    if TempStr[-1] in ['F', 'f']:
        C = (eval(TempStr[0:-1]) - 32) / 1.8
        print("{:.2f}C".format(C))
    elif TempStr[-1] in ['C', 'c']:
        F = 1.8 * eval(TempStr[0:-1]) + 32
        print("{:.2f}F".format(F))
    else:
        print("输入错误,末位只能是'C','c','F','f'")
except NameError:
    print('试图访问的变量名不存在')
except SyntaxError:
    print('存在语法错误')
except Exception as e:
    print(e)

4、同符号数学运算

n = eval(input())
N = abs(n)
a = N + 10
b = N - 10
c = N * 10
if n < 0:
    a = -abs(a)
    b = -abs(b)
    c = -abs(c)
else:
    a = abs(a)
    b = abs(b)
    c = abs(c)
print(N, a , b, c, end = "")

5、三角函数计算

import math

a = eval(input())
b = eval(input())
x = (-b+math.sqrt(2 * a * math.sin(math.pi / 3)*math.cos(math.pi / 3)))/(2 * a)
print(x)

6、棋盘放米

sum=0
for i in range(64):
    t=2**i
    sum=sum+t
print(sum)

7、鸡兔同笼

head,foot = map(int,input().split())
if int(foot-head*2)/2==(foot-head*2)/2 and foot-head*2 > 0 and head>=0 and foot>=0:
    print(int(head-(foot-head*2)/2), int((foot-head*2)/2))
else:
    print('Data Error!')

8、数列求和

a = int(input())       
n = int(input())
if 9 >= a >= 0:        
    tmp = a
    sum = 0           
    for i in range(n):
        sum = sum + tmp       
        tmp = tmp * 10 + a    
    print(sum)

猜你喜欢

转载自www.cnblogs.com/slj-xt/p/12693096.html
今日推荐