python案例Demo1-Demo30

Demo1

"""

数据:cel fah

步骤:

1.输入cel

2.计算fah

3.输出fah

"""

cel = eval(input())

fah = 9 / 5 * cel + 32

print("%0.1f" %fah)

Demo2

"""

数据: radius length area volume

步骤:

1.输入radius length

2.计算area底面积

3.计算volume体积

4.输出

"""

radius, length = eval(input())

area = radius * radius * 3.14159267

volume = area * length

print("%0.2f" %area)

print("%0.2f" %volume)

Demo3

"""

一.数据:feet  meter

二.步骤:meter = feet * 0.305

三.输出

"""

feet = eval(input())

meet = feet * 0.305

print("%0.4f" %meet)

Demo4

"""

一.数据:小计sub  酬金率fee 合计total 小费money

二.步骤:total = sub + sub * fee *0.01   money = sub*fee*0.01

三.输出

"""

sub,fee = eval(input())

money = sub * fee * 0.01

total = sub + fee * 0.01 * sub

print("%0.2f" %money)

print("%0.2f" %total)

Demo5

"""

数据:num ge shi bai total

步骤:输入一个数[0,1000]

按顺序从左往右拆

三者先加 输出

"""

num = eval(input())

ge = num % 10

num //= 10

shi = num % 10

num //= 10

bai = num % 10

nsum = ge + shi + bai

print(nsum)

Demo6

"""

数据:minutes hours days years

步骤:

1.输入minutes

2.计算总小时数 minutes / 60

3.计算总天数 hours / 24

4.计算总年数 days / 365

5.输出

"""

minutes = eval(input())

hours = minutes // 60

days = hours // 24

years = days // 365

days = days % 365

print(years)

print(days)

Demo7

"""

数据 :水量M , 初始温度itemperature, 最终温度ftemperature, 热量Q

步骤:1.输入水量 初始温度 最终温度

2.Q = M *(ftemperature - itemperature) * 4184

3.输出

"""

M = eval(input())

itemperature = eval(input())

ftemperature = eval(input())

Q = M * (ftemperature - itemperature) * 4184

print("%.1f" % Q)

Demo8

"""

数据: 四位整数M , 个ge, 十shi, 百bai, 千qian    例如:1234

步骤:一.输入四位整数

二.1234 // 1000 = 1 = qian

1234 % 100 = 12

12 % 10 = 2 = bai

1234 // 10 = 123

123 % 10 = 3 = shi

1234 % 10 = 4 = ge

三.输出

"""

M = eval(input())

qian = M // 1000

bai = M // 100 % 10

shi = M // 10 % 10

ge = M % 10

print(ge)

print(shi)

print(bai)

print(qian)

Demo9

"""

数据: 三个顶点坐标x-1-2-3  y-1-2-3 三边side1-2-3 半周长s area

步骤:

1.输入

2.计算三边

3.计算三边面积

"""

x1, y1, x2, y2, x3, y3 = eval(input())

side1 = ((x1 - x2)**2 + (y1-y2)**2)**0.5

side2 = ((x3-x2)**2 + (y3-y2)**2)**0.5

side3 = ((x1-x3)**2 + (y1-y3)**2)**0.5

s = (side1 + side2 + side3) / 2

area = (s * (s-side1) * (s-side2) * (s-side3))**0.5

print("%0.1f" %area)


 

Demo10

"""

数据: 时区timezone , 当前时间nowtime, 当前时区时间currenttime 时hours 分minu 秒second

步骤:

1.导入时间库

2.输入所在时区,计算出当前时区时间

3.输出

"""

import time

timezone = eval(input())

nowtime = time.time()

currenttime = nowtime + timezone * 60 * 60

secend = currenttime % 60

minutes = currenttime // 60 % 60

hours = currenttime // 60 // 60 % 24

print("%d:%d:%d"%(hours,minutes,secend))

Demo11

"""

数据:三角形三个坐标 (x1,y1) (x2,y2) (x3,y3) 三个角度数A B C

步骤:

1. 输入坐标

2.计算a, b, c的长度

3.用余弦定理计算出三个角A B C的度数

"""

import math

x1, y1, x2, y2, x3, y3 = eval(input())

a = ((x2 - x3)**2 + (y2 - y3)**2) ** 0.5

b = ((x1 - x3)**2 + (y1 - y3)**2) ** 0.5

c = ((x1 - x2)**2 + (y1 - y2)**2) ** 0.5

A = math.degrees(math.acos((b**2 + c**2 - a**2) / (2 * b * c)))

B = math.degrees(math.acos((a**2 + c**2 - b**2) / (2 * a * c)))

C = math.degrees(math.acos((a**2 + b**2 - c**2) / (2 * a * b)))

print("%.2f" % A)

print("%.2f" % B)

print("%.2f" % C)

Demo12

"""

数据:一元yiyuan  两角五分ljwufen 一角yijiao 五分wufen 一分yifen 总额total

步骤:

1.输入总额

2.将总额兑换若干硬币

3.输出每种硬币个数

"""

total = eval(input())

yiyuan = total // 1

total %= 1

ljwufen = total // 0.25

total %= 0.25

yijiao = total // 0.1

total %= 0.1

wufen = total // 0.05

total %= 0.05

yifen = total // 0.01

print("%d个一元硬币" %yiyuan)

print("%d个两角五分硬币" %ljwufen)

print("%d个一角硬币" %yijiao)

print("%d个五分硬币" %wufen)

print("%d个一分硬币" %yifen)

Demo13

"""

数据:正n边形 边长s 面积area

步骤:

1.输入n与s

2.由公式计算出正多边形面积

3.输出

"""

import math

n, s = eval(input())

area = (n * s**2) / (4 * math.tan(math.pi / n))

print("%.2f" %area)

Demo14

"""

数据:weight height

步骤:

1.输入体重身高

2.BMI=体重 / 身高**2  判断BMI

3.输出

"""

weight, height = eval(input())

BMI = weight / height**2

if BMI < 18.5:

    print("超轻")

elif BMI < 25.0:

    print("标准")

elif BMI < 30.0:

    print("超重")

else:

    print("肥胖")

Demo15

"""

数据:年份year

步骤:

1.输入年份

2.判断是否为闰年(if)

3.输出

"""

year = eval(input())

if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:

    print("Yes")

else:

     print("No")

Demo16

"""

数据:随机产生的数字randNum 用户输入的数字 userNum r1 r2 u1 u2

    randNum = r1 * 10 + r2

    userNum = u1 * 10 + u2

步骤:

1.输入

2.拆数字

3.判断

4.输出

randNum == userNum

r1 == u2 and r2 == u1

r1 == u1 or r1 ==u2 or r2 == u1 or r2 == u2

"""

import random

randNum = random.randint(10,99)

print(randNum)

userNum = eval(input())

r1 = randNum // 10

r2 = randNum % 10

u1 = userNum // 10

u2 = userNum % 10

if randNum == userNum:

    print(10000)

elif r1 == u2 and r2 == u1:

    print(5000)

elif r1 == u1 or r1 == u2 or r2 == u1 or r2 == u2:

    print(3000)

else:

    print(0)

Demo17

"""

数据: a,b,c,delt,x1,x2

步骤:

1.输入a,b,c

2.计算 delt = b^2 - 4ac

3.delt>0

delt=0

delt<0

4.输出

"""

a,b,c = eval(input())

delt = b**2 - 4 * a * c

if delt > 0:

    x1 = (-b + delt ** 0.5) / (2 * a)

    x2 = (-b - delt ** 0.5) / (2 * a)

    print("%0.2f" %x1)

    print("%0.2f" %x2)

elif delt == 0:

    x = -b / (2 * a)

    print(x)

else:

    print("无实数解")

Demo18

"""

数据: a,b,c,d,e,f,x,y

步骤:

1.输入a,b,c,d,e,f,g,x,y

2.计算 delt = ad - bc

3.delt=0 输出无解

4.输出

"""

a,b,c,d,e,f = eval(input())

if a * d - b * c == 0 :

    print("无解")

else:

    x =( e * d - b * f ) / (a * d - b * c)

    y = ( a * f - e * c ) / (a * d - b * c )

    print("%.1f" %x)

    print("%.1f" %y)

Demo19

"""

数据:今天today 未来天数future

步骤:

1.输入今天是星期几,未来天数

2.星期为七进制,future % 7

3.输出

"""

today, future = eval(input())

n = (today + future) % 7

if n == 0:

    print("周日")

elif n == 1:

    print("周一")

elif n == 2:

    print("周二")

elif n == 3:

    print("周三")

elif n == 4:

    print("周四")

elif n == 5:

    print("周五")

else:

    n == 6

    print("周六")

Demo20

"""

数据:年year 月month 日day

步骤:

1.输入年月日

2.计算给定日期距离

3.判断是否为闰年

4.输出

"""

years, months, day = eval(input())

days = 0

if(years % 4 == 0 and years % 100 != 0) or years % 400 == 0:

    for i in range(1,months):

        if i in (1, 3, 5, 7, 8, 10, 12):

            days += 31

        elif i in (4, 5, 9, 11):

            days += 30

        else:

            days += 29

else:

    for i in range(1, months):

            if i in (1, 3, 5, 7, 8, 10, 12):

                days += 31

            elif i in (4, 6, 9, 11):

                 days += 30

            else:

                    days += 28

days += day

print(days)

Demo21

"""

数据:剪刀0 石头1 布2

步骤:

1.导入random库

2.定义随机数在[0,2]

3.输出

"""

import random

randomNum = random.randint(0,2)

num = eval(input())

if randomNum == 0 :

    print("计算机出的是剪刀")

elif randomNum == 1 :

    print("计算机出的是石头")

else:

    randomNum == 2

    print("计算机出的是布")

if num == 0 :

    print("用户出的是剪刀")

elif num == 1 :

    print("用户出的是石头")

else:

    num == 2

    print("用户出的是布")

if (randomNum == 0 and num == 1) or (randomNum == 1 and num == 2) or (randomNum == 2 and num == 0):

    print("用户赢")

elif(randomNum == 0 and num == 2) or (randomNum == 1 and num == 0) or (randomNum == 2 and num == 1):

    print("计算机赢")

else:

    randomNum = num

    print("平局")

Demo22

"""

数据:三角形三条边 a,b,c   周长d

步骤:1.输入三角形三条边

2.三角形属性俩边之和大于第三边

3.输出

"""

a, b, c = eval(input())

if (a + b > c) and (a + c > b) and (b + c > a):

    d = a + b + c

    print(d)

else:

    print("非法")

Demo23

"""

数据:一周的星期几h  一月的哪一天q  月份m   世纪数j  一个世纪的某一年k

步骤:

1.输入年月日

2.利用公式计算

3.输出

"""

import math

years, months, days = eval(input())

h = (days + math.floor(26 * (months + 1 ) // 10 ) + (years % 100) + math.floor(years % 100 // 4) + math.floor((math.floor(years // 100)) // 4) + 5 * math.floor(years // 100)) % 7

if h == 0:

    print("星期六")

elif h == 1:

    print("星期天")

elif h == 2:

    print("星期一")

elif h == 3:

    print("星期二")

elif h == 4:

    print("星期三")

elif h == 5:

    print("星期四")

else:

    h == 6

    print("星期五")

Demo24

"""

数据:A上俩点(x1,y1) (x2,y2)  B上俩点(x3,y3) (x4,y4) 交点(x,y)

步骤:

1.输入四个点的坐标

2.据线性等式判断是否有解

3.输出

"""

import sympy 

x, y = sympy.symbols('x y')

x1, y1, x2, y2, x3, y3, x4, y4 = eval(input())

ka = (y2 - y1) / (x2 - x1) 

kb = (y4 - y3) / (x4 - x3)

if ka != kb: 

    eq1 = (y1 - y2) * x - (x1 - x2) * y - (y1 - y2) * x1 + (x1 - x2) * y1 

    eq2 = (y3 - y4) * x - (x3 - x4) * y - (y3 - y4) * x3 + (x3 - x4) * y3 

    solutions = sympy.solve([eq1, eq2], [x, y])  

    print(solutions)

else:

    print("无解")

Demo25

"""

数据:ge shi bai

步骤:

1.输入一个三位数

2.判断是否为回文数

3.输出

"""

num = eval(input())

bai = num // 100

ge = num % 10

if bai == ge:

    print("Yes")

else:

    print("No")

Demo26

#判断两个矩形之间的包含,重叠,相离关系

# 读取输入数据

rect1 = list(map(float, input().split())

rect2 = list(map(float, input().split()))

# 解析矩形1的数据

x1, y1, w1, h1 = rect1

left1 = x1 - w1 / 2

right1 = x1 + w1 / 2

top1 = y1 + h1 / 2

bottom1 = y1 - h1 / 2

# 解析矩形2的数据

x2, y2, w2, h2 = rect2

left2 = x2 - w2 / 2

right2 = x2 + w2 / 2

top2 = y2 + h2 / 2

bottom2 = y2 - h2 / 2

# 判断两个矩形的关系

if left1 >= right2 or left2 >= right1 or top1 <= bottom2 or top2 <= bottom1:

    relation = "相离"

elif left1 <= left2 and right1 >= right2 and top1 >= top2 and bottom1 <= bottom2:

    relation = "包含"

elif left1 >= left2 and right1 <= right2 and top1 <= top2 and bottom1 >= bottom2:

    relation = "包含"

else:

    relation = "重叠"

# 输出结果

print(relation)

Demo27

print("模式a")

for n in range(1,7):

    for i in range(1,n + 1):

        print(i,end = " ")

    print()

print("模式b")

for n in range(1,7):

    for i in range(1,8-n):

        print(i, end = " ")

    print()

print("模式c")

for n in range(1, 7):

    for k in range(6 - n):

        print(" ",end = " ")

    for i in range(n, 0, -1):

        print(i, end = " ")

    print()

print("模式d")

for n in range(6, 0, -1):

    for k in range(6 - n):

        print(" ",end = " ")

    for i in range(1, n + 1):

        print(i, end = " ")

    print()

Demo28

for n in range(1, 8):

    for k in range(7 - n):

        print(" ", end = "")

    for x in range(1 - n, n):

        print(abs(x) + 1,end = " ")

    print()

Demo29

# 打印数字

for n in range(1, 8):

    for k in range(7 - n):

        print(" ", end = " ")

    for x in range(1 - n, n):

        num = 2 ** (n - 1 -abs(x))

        print("%2d" % (num), end = " ")

    print()

Demo30

#打印菱形

n = eval(input())

for i in range(1, n + 1):

    for k in range(abs(n // 2 + 1 - i)):

        print(" ", end = "")

    for j in range(1, i + 1):

        if j <= i and i + j <= n + 1:

            print("*", end = " ")

    print()

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值