【python】牛客竞赛语法入门班选择结构习题 python解法

题目链接:牛客竞赛语法入门班选择结构习题_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ

目录

A 比大小

B 卡拉兹函数

C 默契

D 整除判断

E CSimplemathproblem

F 吃瓜群众

G jyq跳格子

H 小名的回答

I 牛妹数

J 判断闰年

K 统计数据正负个数

L 小乐乐是否被叫家长

M 最大最小值

N 送分题

O 四季

P B是不是太迟了

Q 前天是哪天

R L1-2单位换算

S 纸牌

T 排队领水

U 可编程拖拉机比赛

V [NOIP2004]不高兴的津津

W [NOIP2008]ISBN号码


A 比大小

a,b = map(int,input().split())
if a > b:
    print('>')
if a == b:
    print('=')
if a < b:
    print('<')

B 卡拉兹函数

n = int(input())
if n%2 == 0:
    k = n//2
    print(k)
if n%2 != 0:
    k = 3*n+1
    print(k)

C 默契

x,y = map(int,input().split())
if x==y :
    print('Tacit!')
else:
    print('No Tacit!')

D 整除判断

m,n = map(int,input().split())
if m % n == 0:
    print('YES')
else:
    print('NO')

E CSimplemathproblem

X,Y = map(int,input().split())
if Y % X == 0:
    Z = X + Y
    print(Z)
else:
    Z = Y - X
    print(Z)

F 吃瓜群众

weight = int(input())
if weight % 2 == 0 and weight != 2:
    print(r'''YES, you can divide the watermelon into two even parts.''')
else:
    print(r'''NO, you can't divide the watermelon into two even parts.''')

G jyq跳格子

n = int(input())
if ((n+1) % 2 == 0 or (n+1) % 4 == 0 or (n+1) % 6 == 0) and n >= 1:
    print(n)
else:
    print('-1')

H 小名的回答

a,b,n = map(int,input().split())
if n < (a+b) or (n-a-b) % 2 != 0:
    print("NO")
else:
    print("YES")

I 牛妹数

n = int(input())
if n % 2 == 0 and n > 50:
    print('yes')
else:
    print('no')

J 判断闰年

# 闰年的判断,不能简单以“四年一闰”来概括。更加科学全面的解释,应该是:能够被4整除,但同时不能够被100整除,或者说 能够被400整除的年份,才是闰年。因此民间有说法:四年一闰,百年不润,四百年再闰。
n = int(input())
if ( n % 4 == 0 and n % 100 != 0 ) or n % 400 == 0:
    print('yes')
else:
    print('no')

K 统计数据正负个数

list = input().split()
positive = 0
negative = 0
for i in list:
    if i > '0' :
        positive += 1
    if i < '0' :
        negative += 1
print('positive:{}'.format(positive))
print('negative:{}'.format(negative))

L 小乐乐是否被叫家长

a,b,c = map(int,input().split())
if (a+b+c) // 3 < 60:
    print('YES')
else:
    print('NO')

M 最大最小值

a,b,c = map(int,input().split())
print('The maximum number is : {}'.format(max(a,b,c)))
print('The minimum number is : {}'.format(min(a,b,c)))

N 送分题

a = int(input())
b = int(input())
c = int(input())

d = a+b+c
e = (a+b)*c
f = a*(b+c)
g = a+b*c
h = a*b+c
i = a*b*c
print(max(d,e,f,g,h,i))

O 四季

a = int(input())
b = int(input())
c = int(input())

d = a+b+c
e = (a+b)*c
f = a*(b+c)
g = a+b*c
h = a*b+c
i = a*b*c
print(max(d,e,f,g,h,i))

P B是不是太迟了

S = input()
month = int(S[5:7])
day   = int(S[-2:])
if month < 10:
    print(r'''No. It's not too late.''')
if month == 10:
    if day < 29:
        print(r'''No. It's not too late.''')
    elif day >= 29:
        print('QAQ')
if month > 10:
    print('QAQ')

Q 前天是哪天

# 这道题其实考究的是进行判断的先后顺序,先判断哪个后判断哪个,最终得出来的结果可能是不一样的。不细心的朋友一开始很容易做错,比如鄙人。。
n = input()
year = int(n[0:4])  # 年
month = int(n[5:7]) # 月
day = int(n[-2:])   # 日
large_month = {5,7,10,12}  # 大月
small_month = {2,4,6,8,9,11}   # 小月	  # 注意此处将8月归于小月,因为8月的上一个月(7月)是有31天的。而又因为此题求的是今天的前一天是哪天,所以此处将2月归于小月。
teshu_month = {1,3}		# 这个集合在这里面我是没有用到的,只是列出来方便读者观看。

# 需要另行判断的特殊日子有:3月的1日、2日,1月的1日、2日,
if day > 2:
    day -= 2
elif month in large_month and day < 3:
    day = day + 28
    month -= 1
elif month in small_month and day < 3:
    day = day + 29
    month -= 1
elif month == 3 and day < 3:
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        day = day + 27
        month -= 1
    else:
        day = day + 26
        month -= 1
elif month == 1 and day < 3:
    day = day + 29
    month = 12
    year -= 1

if day < 10 or month < 10:	# 当月份或者年份为个位数,对其进行左边填充0
    month = str(month).rjust(2,'0')
    day   = str(day).rjust(2,'0')

year = str(year)
month = str(month)
day = str(day)

print('{}-{}-{}'.format(year,month,day))

R L1-2单位换算

N = int(input())
sum = float(N * 12 * 2.54 * 10)
if sum % 2 == 0:
    print('%d' % int(sum))
else:
    print('%.1f' % sum)

S 纸牌

n = int(input())
if n % 2 == 0:
    print(int(n/2))
else:
    print(n//2 + 1)

T 排队领水

n,a,b = map(int,input().split())
if a+b <= (n-1):
    print(b+1)
else:
    print(n-a)

U 可编程拖拉机比赛

# 第一种 把所有可能情况列举出来
n = int(input())
a = n * 0.1
b = n * 0.2
c = n * 0.3
if a%1 != 0:
    if b%1 != 0 and c%1 != 0:
        print('1 2 3')
    if b%1 == 0 and c%1 == 0:
        print('1 1 0')
    if b%1 == 0 and c%1 != 0:
        print('1 1 2')
    if b%1 != 0 and c%1 == 0:
        print('1 2 1')
else:
    print('0 0 0')
    
# 第二种 是第一种的简化版
n = int(input())
if n % 10 == 0:
    print('0 0 0')
elif n % 5 == 0:
    print('1 1 2')
else:
    print('1 2 3')

V [NOIP2004]不高兴的津津

sum = {}
unhappy = 1
max_num = 0
for i in range(1,8):
    x,y = map(int,input().split())
    sum[i] = x + y
    if sum[i] > max_num:
        unhappy = i
        max_num = sum[i]
print(unhappy)

W [NOIP2008]ISBN号码

# 这道题的易错点是输入的ISBN的识别码如果是X,无法直接转成int型进行比较判断,尝试进行int(n[-1])强制转换,会出现报错。所以要将n[-1]==X单独拿出来进行判断。
n = input()
str_all = ''.join(n.split('-'))[:-1]
sum_str = 0
z = 1
for i in str_all:
    sum_str += int(i)*z
    z += 1
if n[-1] == 'X':
    a = 10
else:
    a = int(n[-1])
if sum_str % 11 == a:
    print('Right')
else:
    if sum_str % 11 == 10:
        print(n[:-1] + 'X')
    else:
        print(n[:-1] + str(sum_str % 11))

本篇内容如有错误或对解题过程存有疑问、更改建议和提升空间等,欢迎私信讨论,一起变得更强!

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

CHOITAKWAI

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

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

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

打赏作者

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

抵扣说明:

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

余额充值