返回
Featured image of post 洛谷-入门2-分支结构

洛谷-入门2-分支结构

在这里写下你的题注

P2433 【深基1-2】小学数学 N 合一

【深基1-2】小学数学 N 合一

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import math 

num = int(input())

match num:
    case 1:
        print("I love Luogu!")
    case 2:
        print("6 4")
    case 3:
        print("3\n12\n2")
    case 4:
        print(f"{500.0/3:.6g}")
    case 5:
        print((260+220)//(12+20))
    case 6:
        print(f"{math.sqrt(pow(6.0,2)+pow(9.0,2)):.6g}")
    case 7:
        print("110\n90\n0")
    case 8:
        pi = 3.141593
        r = 5.0
        print(f"{2*pi*r:.6g}")
        print(f"{pi*pow(r,2):.6g}")
        print(f"{4/3*pi*pow(r,3):.6g}")
    case 9:
        print(22)
    case 10:
        print(9)
    case 11:
        print(f"{100.0 / 3.0:.6g}")
    case 12:
        print("13\nR")
    case 13:
        print(16)
    case 14:
        print(50)

P5709【深基2.习6】Apples Prologue / 苹果和虫子

【深基2.习6】Apples Prologue / 苹果和虫子

1
2
3
4
5
6
m, t, s = map(float, input().split())
if t == 0:
    res = 0    
else:
    res = max(0, int(m - s / t))    
print(res)

P5710 【深基3.例2】数的性质

【深基3.例2】数的性质

1
2
3
4
5
6
7
num = int(input())
x = num % 2 == 0
y = num > 4 and num <= 12
print(int(x and y), end=" ")
print(int(x or y), end=" ")
print(int(x != y), end=" ")
print(int(not x and not y), end=" ")

P5711 【深基3.例3】闰年判断

【深基3.例3】闰年判断

1
2
3
year = int(input())
x, y, z = year%4==0, year%100==0, year%400==0
print(int((x and not y) or z))

P5712 【深基3.例4】Apples

【深基3.例4】Apples

1
2
num = int(input())
print(f"Today, I ate {num} apple{'s' if num > 2 else ''}.")

P5713 【深基3.例5】洛谷团队系统

【深基3.例5】洛谷团队系统

1
2
3
num = int(input())
local = 5*num < 11+3*num 
print(f"{'Local' if local else 'Luogu'}")

P5714 【深基3.例7】肥胖问题

【深基3.例7】肥胖问题

1
2
3
4
5
6
7
8
9
m, h = map(float, input().split())
bmi = m * pow(h, -2)
if bmi < 18.5:
    print("Underweight")
elif bmi >= 24:
    print(f"{bmi:.6g}")
    print("Overweight")
else:
    print("Normal")

P5715 【深基3.例8】三位数排序

【深基3.例8】三位数排序

1
2
3
4
nums = list(map(int, input().split()))
nums.sort()  
for num in nums:
    print(num, end=" ")

P5716 【深基3.例9】月份天数

【深基3.例9】月份天数

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
month_days = {
    1: 31,  # January
    2: 28,  # February (non-leap year)
    3: 31,  # March
    4: 30,  # April
    5: 31,  # May
    6: 30,  # June
    7: 31,  # July
    8: 31,  # August
    9: 30,  # September
    10: 31, # October
    11: 30, # November
    12: 31  # December
}

def is_leap_year(year):
    return (year%4==0 and year%100!=0) or year%400==0

year, month = map(int, input().split())

if is_leap_year(year) and month == 2:
    print(29)
else:
    print(month_days[month])

P1085 [NOIP 2004 普及组] 不高兴的津津

[NOIP 2004 普及组] 不高兴的津津

1
2
3
4
5
6
7
cur_day, cur_hour, unhappy_hour = 0, 0, 8
for day in range(1, 8):
    school, extra = map(int, input().split())
    today = school + extra 
    if today > max(cur_hour, unhappy_hour):
        cur_hour, cur_day = today, day 
print(cur_day)

P1909 [NOIP 2016 普及组] 买铅笔

[NOIP 2016 普及组] 买铅笔

这里注意一下无限大的用法

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import math 

def cal_cost(num, price, want):
    return price * math.ceil(want/num)

cost = math.inf

want = int(input())

for _ in range(3):
    num, price = map(int, input().split())
    cost = min(cost, cal_cost(num, price, want))

print(cost)

P5717 【深基3.习8】三角形分类

【深基3.习8】三角形分类

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
import math

nums = list(map(int, input().split()))
nums.sort()
a, b, c = nums 

if a + b <= c:
    print("Not triangle")
else:
    if pow(a, 2) + pow(b, 2) < pow(c, 2):
        print("Obtuse triangle")
    elif pow(a, 2) + pow(b, 2) > pow(c, 2):
        print("Acute triangle")
    else:
        print("Right triangle")
    if a == b or b == c:
        print("Isosceles triangle")
    if a == c:
        print("Equilateral triangle")
        
        

P1422 小玉家的电费

小玉家的电费

1
2
num = int(input())
print(f"{num*0.4463+max(0,num-150)*0.02+max(0,num-400)*0.1:.1f}")

P1424 小鱼的航程(改进版)

小鱼的航程(改进版)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
swim, sum = 250, 0
day, duration = map(int, input().split())
for i in range(duration):
    if day == 7:
        day = 1
    elif day == 6:
        day += 1
    else:
        day, sum = day + 1, sum + swim
print(sum)
        

P1888 三角函数

三角函数

1
2
3
4
5
6
7
nums = list(map(int, input().split()))
nums.sort()
short, slide = nums[0], nums[2]
for i in range(short, 0, -1):
    if short % i == 0 and slide % i == 0:
        short, slide = short//i, slide//i 
print(f"{short}/{slide}")

P1046 [NOIP 2005 普及组] 陶陶摘苹果

[NOIP 2005 普及组] 陶陶摘苹果

这题主要要记得filter出来也是迭代器,加一个list就行了

1
2
3
4
nums = map(int, input().split())
bar = int(input()) + 30
nums = list(filter(lambda x : x <= bar, nums))
print(len(nums))

P4414 [COCI 2006/2007 #2] ABC

[COCI 2006/2007 #2] ABC

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
nums = list(map(int, input().split()))
nums.sort()
line = input()
for i in range(3):
    char = line[i]
    match char:
        case 'A':
            print(nums[0], end=" ")
        case 'B':
            print(nums[1], end=" ")
        case 'C':
            print(nums[2], end=" ")

P1055 [NOIP 2008 普及组] ISBN 号码

[NOIP 2008 普及组] ISBN 号码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 读取输入的 ISBN 号码
isbn = input()

# 提取前 9 位数字
digits = isbn[0] + isbn[2:5] + isbn[6:-1]

# 计算识别码
cnt = 0
for i in range(9):
    cnt += (i + 1) * int(digits[i])
cnt %= 11

# 处理识别码为 10 的情况
correct_check = "X" if cnt == 10 else str(cnt)

# 获取输入的识别码
input_check = isbn[-1]

# 判断识别码是否正确
if input_check == correct_check:
    print("Right")
else:
    # 输出修正后的 ISBN 号码
    print(f"{isbn[:-1]}{correct_check}")
Licensed under CC BY-NC-SA 4.0
© 2023 - 2025 壹壹贰捌· 0Days
共书写了258.6k字·共 93篇文章 京ICP备2023035941号-1