第五周作业

1、一元二次方程求根

import math  
a = eval(input())
b = eval(input())
c = eval(input())
d = pow(b,2)-4*a*c  
if a == 0:
    if b == 0:
        print('Data error!')
    else:
        print(-c/b)
else:
    if d < 0:   
        print('该方程无实数解')
    elif d == 0:
        x = (math.sqrt(b*b-4*a*c)-b)/(2*a)
        print(x)
    elif d > 0:
        x1 = (math.sqrt(b*b-4*a*c)-b)/(2*a)  
        x2 = (-math.sqrt(b*b-4*a*c)-b)/(2*a)
        if x1 > x2:   
            print('{} {}'.format(x1,x2))
        else:
            print('{} {}'.format(x2,x1))

2.百钱买百鸡

cock_1= 5           #一只公鸡价格
hen_1 = 3             #一只母鸡价格
chicken_1 = 1/3       #一只小鸡价格
a = int(100/cock_1)      #100元最多购买公鸡的数量整数
b = int(100/hen_1)       #100元最多购买母鸡的数量整数
c = int(100/chicken_1)   #100元最多购买小鸡的数量整数
cock = []      #公鸡数量空列表
hen = []       #母鸡数量空列表
chicken = []   #小鸡数量空列表
for i in range(1,a+1):               #遍历公鸡可能数量
    for j in range(1,b+1):            #遍历母鸡可能数量
        for k in range(1,c+1):        #遍历小鸡可能数量
            if i+j+k == 100:     #鸡的总数为100
                if cock_1*i+hen_1*j+chicken_1*k == 100:  #鸡的总价格为100
                    cock.append(i)    #将计算的可能公鸡数量加入公鸡列表
                    hen.append(j)     #将计算的可能母鸡数量加入母鸡列表
                    chicken.append(k) #将计算的可能小鸡数量加入小鸡列表
for l in range(len(cock)):   #获取公鸡数量的长度,并遍历循环
    print('{} {} {}'.format(cock[l],hen[l],chicken[l]))
#因为位置三者对应,即都按照遍历公鸡数量列表位置进行打印

鸡兔同笼

a,b = map(int,input().split(' '))
if (a>0 and b>0 and a<b and b%2==0 and b-2*a>0):  
    for x in range(1,a+1):    
        y = a-x
        if 2*x + 4*y == b:   
            print('{} {}'.format(x,y))
            break
        if y==0:
            print('Data Error!')
            
else:
    print("Data Error!")

3、最大公约数和最小公倍数

import math
a=int(input())
b=int(input())
r1=math.gcd(a,b)
r2=a*b/r1
print('{} {}'.format(int(r1),int(r2)))

4、判断三角形并计算面积

import math
a=float(input())
b=float(input())
c=float(input())
if(a+b>c and a+c>b and b+c>a):
    d=a+b+c
    p=d/2
    area=math.sqrt(p*(p-a)*(p-b)*(p-c))
    print('YES')
    print('{:.2f}'.format(area))
else:
    print('NO')
    

5、判断IP地址合法性

ip=input()
ip=ip.split('.')
if len(ip)==4:
    for i in range(4):
        if ip[i].isdigit()==False or eval(ip[i])>255 or eval(ip[i])<0:
            print('No')
            break
    else:
        if i==3:
            print('Yes')
else:
    print('No')

6、回文素数

a = int(input())
b = []   
n = 1   
while len(b) < a:   
    n = n+1   
    if str(n) == str(n)[::-1]:  
        for i in range(1,n):   
            if n % i == 0:
                x = i          
        else:
            if x == 1:    
                b.append(str(n))
print("{}".format(" ".join(b)),end=" ")  

7.反素数

a = int(input())
b = []
n = 1
while len(b) < a: 
    n = n + 1   
    if (n>10) and (str(n) != str(n)[::-1]):  
        for i in range(1,n):
            if n % i == 0:
                x = i
        else:
            if x == 1:   
                c = str(n)[::-1]  
                for j in range(1,int(c)): 
                    if int(c) % j == 0:
                            y = j
                else:
                    if y == 1:
                        b.append(str(n))  
print("{}".format(" ".join(b)),end=" ")   

8、今天是第几天

a=eval(input())
b=eval(input())
c=eval(input())
month=[0,31,59,90,120,151,181,212,243,273,304,334]
sum=month[b-1]+c
if(a%4==0 and a%100!=0)or a%400==0:
    if(b<=2):
        print('{}年{}月{}日是{}年第{}天'.format(a,b,c,a,sum))
    else:
        print('{}年{}月{}日是{}年第{}天'.format(a,b,c,a,sum+1))
else:
   print('{}年{}月{}日是{}年第{}天'.format(a,b,c,a,sum))
        

9、提取首字符

a=input()
a=a.split(' ')
b=[]
for i in range(len(a)):
    b.append(a[i][0])
print('{}'.format(''.join(b)))

10、判断火车票位置

a = input()
if 2 <= len(a) <= 3:  
    if a[0:-1].isdigit() == True and 1<=int(a[0:-1])<=17:
        if a[-1] in ['a','f','A','F']:    
            print("窗口")
        elif a[-1] in ['c','d','C','D']:  
            print("过道")
        else:
            print("输入错误")
    else:
        print("输入错误")
else:
    print('输入错误')

 

posted @ 2020-04-13 18:07  树懒君  阅读(258)  评论(0)    收藏  举报