【入门2】分支结构 - 题单 - 洛谷https://www.luogu.com.cn/training/101#problems一、【深基3.例2】数的性质 - 洛谷
一些数字可能拥有以下的性质:
小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=item:
count+=1
print(count)
十五、【深基3.习8】三角形分类 - 洛谷
#include
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
十六、[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=" ")