洛谷刷题记录【入门2】分支结构

【入门2】分支结构 - 题单 - 洛谷https://www.luogu.com.cn/training/101#problems一、【深基3.例2】数的性质 - 洛谷

一些数字可能拥有以下的性质:

  • 性质 1:是偶数;
  • 性质 2:大于 4 且不大于 12。

小A 喜欢这两个性质同时成立的数字;Uim 喜欢这至少符合其中一种性质的数字;八尾勇喜欢刚好有符合其中一个性质的数字;正妹喜欢不符合这两个性质的数字。

一、暴力写法

num = int(input())

if num >=0 and num <= 1000:
    if (num % 2 == 0) and (num > 4 and num <= 12):
        print("1",end=" ")
    else:
        print("0",end=" ")
    if (num % 2 == 0) or (num > 4 and num <= 12):
        print("1",end=" ")
    else:
        print("0",end=" ")

    if (num % 2 != 0) and (num > 4 and num <= 12):
        print("1",end=" ")
    elif (num % 2 == 0) and (num <= 4 and num > 12):
        print("1",end=" ")
    else:
        print("0",end=" ")
    
    if (num % 2 == 0) and (num > 4 and num <= 12):
        print("0",end=" ")
    else:
        print("1",end=" ")

二、Python选择结构特有写法

n = eval(input())
p1 = n % 2 == 0
p2 = 12 >= n > 4
print(1 if p1 and p2 else 0,end=" ")
print(1 if p1 or p2 else 0,end=" ")
print(1 if (p1 and not p2) or (not p1 and p2) else 0,end=" ")
print(1 if not p1 and not p2 else 0,end="")

分析:选择结构的两种写法

1. 表达式为真输出区 if 表达式 else 表达式为假输出区

2.[表达式为假输出区,表达式为真输出区][判断表达式]

二、【深基3.例3】闰年判断 - 洛谷

输入一个年份(大于 1582 的整数 ),判断这一年是否是闰年,如果是输出 1,否则输出 0。

方法一:算法实现

year = int(input())
if year > 1582:
    if year % 4 ==0 and year %100 != 0 or year % 400 == 0:
        print(1)
    else:
        print(0)

方法二:调用库函数

year = int(input())
if year > 1582:
    if isleap(year):
        print(1)
    else:
        print(0)

分析:简单的闰平年判断题

三、【深基3.例4】Apples - 洛谷

num = int(input())

if 0<=num<=100:
    if num<=1:
        print("Today, I ate {} apple.".format(num))
    else :
        print("Today, I ate {} apples.".format(num))

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

num = int(input())

Local = num*5

Luogu = num*3+11

if num<=100:
    if Local < Luogu:
        print("Local")
    elif Local == Luogu:
        print("Local"+"和"+"Luogu"+"时间一样")
    else:
        print("Luogu")

五、【深基3.例7】肥胖问题 - 洛谷

m,h = map(float,input().split())
if 40<=m<=120 and 1.4<=h<=2:
    BMI = m/(h*h)
    if BMI < 18.5:
        print("Underweight")
    elif 18.5 <= BMI < 24:
        print("Normal")
    elif BMI >= 24:
        print("%g"%BMI)
        print("Overweight")

六、【深基3.例8】三位数排序 - 洛谷

a,b,c = map(int,input().split())
p = 0<= a,b,c <=100
if p:
    if a>=b:
        a,b =b,a
    if a>=c:
        a,c=c,a
    if b>=c:
        b,c=c,b
print("{} {} {}".format(a,b,c))

七、【深基3.例9】月份天数 - 洛谷

y, m = map(eval, input().split())
if m ==2 :
    p1 = y % 4 == 0  # 被4整除是闰
    p2 = y % 100 == 0  # 被100整除不是闰
    p3 = y % 400 == 0  # 是闰
    if p3 or  (p1 & (not p2)):
        print(29)
    else:
        print(28)
else:
    if m in (1, 3, 5, 7, 8, 10, 12):
        print(31)
    else:
        print(30)

八、[NOIP2004 普及组] 不高兴的津津 - 洛谷

max_cd = 0
day = 0
for i in range(1,8):
    a,b = map(int,input().split())
    if (a+b)-8 > max_cd:
        max_cd = a+b -8
        day = i
print(day)

九、[NOIP2016 普及组] 买铅笔 - 洛谷

n_need=int(input())
money = [None]*3
for kind in range(3):
    num,PenMoney = map(int,input().split())
    for i in range(1,100000):
        if num*i>=n_need:
            money[kind]=i*PenMoney
            break
print(min(money))

十、[NOIP2008 普及组] ISBN 号码 - 洛谷

ISBN = input()
strNum = ISBN[:12]
Sum = 0
j = 1
for i in strNum:
    if i != '-':
        n = int(i)
        Sum = Sum + n * j
        j += 1
    else:
        continue
if Sum % 11 == 10:
    ID = 'X'
else:
    ID = str(Sum % 11)
if ID == ISBN[12]:
    print("Right")
else:
    strNum = strNum + ID
    print(strNum)

十一、小玉家的电费 - 洛谷

n = int(input())
s = 0.0
if n <= 150:
    s = 0.4463 * n
elif n <= 400:
    s = (n - 150) * 0.4663 + 150 * 0.4463
else:
    s = (n - 400) * 0.5663 + 250 * 0.4663 + 150 * 0.4463
print('%.1f' % s)

十二、小鱼的航程(改进版) - 洛谷

x, n = map(int, input().split())
ans = (n//7)*5
n %= 7
while n > 0 :
    if  x != 6 and x != 7 :
        ans += 1
    x += 1
    n -= 1
 
print(ans*250)

十三、三角函数 - 洛谷

from fractions import Fraction
list_1 = list(map(int,input().split()))
list_1 = sorted(list_1)
a1,a3 = list_1[0],list_1[2]
a = Fraction(a1, a3)
print(a)
a = input().split(' ')
a = [int(i) for i in a]
a = sorted(a)
a1 = a[0]
a3 = a[2]
# 约分  
while a1:
    yushu = a3%a1
    a3 = a1
    a1 = yushu
print ("%d/%d"%(a[0]/a3,a[2]/a3))

十四、[NOIP2005 普及组] 陶陶摘苹果 - 洛谷

AppHeight = map(int,input().split())
TTHeight = int(input())
count = 0
if 100<TTHeight<120:
    for item in AppHeight:
        if 100<=item<=200:
            if TTHeight+30>=item:
                count+=1
print(count)

十五、【深基3.习8】三角形分类 - 洛谷

#include<stdio.h>
int main(){
	int a,b,c;  //三边
	scanf("%d%d%d",&a,&b,&c);
	int t; //临时
	//先由小到大排列三边 以便实现三角形的特性
	if(a>b){
		t=a;
		a=b;
		b=t;
	} 
	if(a>c){
		t=a;
		a=c;
		c=t;
	} if(b>c){
		t=b;
		b=c;
		c=t;
	} 
	if(a+b<=c){ //三角形 任意两边要大于第三边 按大小排序过 所以a+b>c就是三角形
		printf("Not triangle");
	}else{  //一定把判断三角形当作一个块 其他的全放到else里 因为1 3 3不是三角形但是它满足等腰三角形的要求
		if(a*a+b*b==c*c){
		printf("Right triangle\n");
	}
	if(a*a+b*b>c*c){
		printf("Acute triangle\n");
	}
	if(a*a+b*b<c*c){
		printf("Obtuse triangle\n");
	}
	if(a==b||b==c){ //锐角等腰 两长边一短边  钝角等腰 一长边两短边
		printf("Isosceles triangle\n");
	}
	if(a==b&&b==c){
		printf("Equilateral triangle");
	}
	}
	return 0;
} 

十六、[COCI2006-2007#2] ABC - 洛谷

a,b,c = map(int,input().split())
order = input().upper()
if a+b+c<=300:
    MinNum =min(min(a,b),c) 
    MaxNum =max(max(a,b),c)
    MidNum = (a+b+c)-MinNum-MaxNum
    for item in order:
        if item == 'A':
            print(MinNum,end=" ")
        if item == 'B':
            print(MidNum,end=" ")
        if item == 'C':
            print(MaxNum,end=" ")

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

踢足球没有假动作

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值