基础编程题目集

前言

这是一份不错的练习题目,链接:https://pintia.cn/problem-sets/14/exam/problems/type/7?problemSetProblemId=781

1-20

厘米换算英尺英寸
解题思路:用类比的思想来解决问题。题目中英尺英寸的换算就相当于元和角之间的换算。
1英尺=12英寸;1元=10角;
m=(foot+inch/12)×0.3048; cm=(foot+inch/12)×30.48;foot=cm/30.48;
inch=(cm/30.48-foot)×12;类比到元和角的计算:12.5=12(元)+5(角);元=价格的整数部分;
角=(价格-元)×10;

cm = int(input())
meters = cm / 100
total_feet = meters / 0.3048
foot = int(total_feet)
inch = int((total_feet - foot) * 12)
print(foot, inch)

然后是几点

s, e = map(int, input().split())
h = s // 100
m = s % 100
t = h * 60 + m + e
fh = t // 60
fm = t % 60
print(f"{fh}{fm:02d}")    

逆序的三位数

num = int(input())
res = 0
for i in range(len(str(num))-1,-1,-1):
    res = res*10+int(str(num)[i])
print(res) 

BCD解密

w = int(input())
h = hex(w)[2:]
if len(h) == 1:
    d1 = int(h[0], 16)
    d2 = 0
else:
    d1 = int(h[0], 16)
    d2 = int(h[1], 16)
c = d1 * 10 + d2
print(c)

表格输出

print("------------------------------------")
print("Province      Area(km2)   Pop.(10K)")
print("------------------------------------")
print("Anhui         139600.00   6461.00")
print("Beijing        16410.54   1180.70")
print("Chongqing      82400.00   3144.23")
print("Shanghai        6340.50   1360.26")
print("Zhejiang      101800.00   4894.00")
print("------------------------------------")

混合类型数据格式化输入

f1, i, c, f2 = input().split()
i = int(i)
f1 = float(f1)
f2 = float(f2)
print(f"{c} {i} {f1:.2f} {f2:.2f}")

12-24小时制

t=input()
h,m=map(int,t.split(":"))
p="AM" if h<12 else "PM"
if h>12:h-=12
if h==0:h=0
print(f"{h}:{m} {p}")   

超速判断

v = int(input())
if v<=60:
    s="OK"
else:
    s="Speeding"
print(f"Speed: {v} - {s}")

用天平找小球

a, b, c = map(int, input().split())
if a == b:
    print('C')
elif a == c:
    print('B')
else:
    print('A')

计算工资

years, hours = map(int, input().split())
if years >= 5:
    wage = 50
else:
    wage = 30
if hours <= 40:
    salary = hours * wage
else:
    salary = 40 * wage + (hours - 40) * wage * 1.5
print(f"{salary:.2f}")
posted @ 2025-04-23 15:05  傻傻的小小豪  阅读(38)  评论(0)    收藏  举报