• 牛客网语法篇练习分支控制(一)


    1.据说智商140以上者称为天才,KiKi想知道他自己是不是天才,请帮他编程判断。输入一个整数表示一个人的智商,如果大于等于140,则表明他是一个天才,输出“Genius”。

    1. while True:
    2. try:
    3. a = int(input())
    4. if a >=140:
    5. print('Genius')
    6. except:
    7. break

    2.KiKi想知道他的考试分数是否通过,请帮他判断。从键盘任意输入一个整数表示的分数,编程判断该分数是否在范围内,如果及格,即:分数大于等于60分,是输出“Pass”,否则,输出“Fail”。

    1. while True:
    2. try:
    3. a = int(input())
    4. if a>=60:
    5. print("Pass")
    6. else:
    7. print("Fail")
    8. except:
    9. break

    3.KiKi想知道一个整数的奇偶性,请帮他判断。从键盘任意输入一个整数(范围-231~231-1),编程判断它的奇偶性。

    1. while True:
    2. try:
    3. num = int(input())
    4. if num%2 ==0:
    5. print("Even")
    6. else:
    7. print("Odd")
    8. except:
    9. break

    4.KiKi开始学习英文字母,BoBo老师告诉他,有五个字母A(a), E(e), I(i), O(o),U(u)称为元音,其他所有字母称为辅音,请帮他编写程序判断输入的字母是元音(Vowel)还是辅音(Consonant)。

    1. v = ['A','a','E','e','I','i','O','o','U','u']
    2. while True:
    3. try:
    4. s = input()
    5. if s in v:
    6. print('Vowel')
    7. else:
    8. print('Consonant')
    9. except:
    10. break

    5.牛牛从键盘输入整数 x 和左右边界 l 和 r 共三个整数。请你判断 x 是否在 l 和 r 之间。

    1. c=(input())
    2. x=int(c.split(' ')[0])
    3. l=int(c.split(' ')[1])
    4. r=int(c.split(' ')[2])
    5. if l<=x<=r:
    6. print('true')
    7. else:
    8. print('false')

    6.判断一个整数n是否是闰年。

    1. n = int(input())
    2. if n%400==0 or (n%4==0 and n%100!=0):
    3. print("yes")
    4. else:
    5. print("no")

    7.从键盘任意输入一个字符,编程判断是否是字母(包括大小写)。

    1. try:
    2. while True:
    3. a = input()
    4. if a.isalpha():
    5. print("YES")
    6. else:
    7. print("NO")
    8. except:
    9. print()

    8.气象意义上,通常以3~5月为春季(spring),6~8月为夏季(summer),9~11月为秋季(autumn),12月~来年2月为冬季(winter)。请根据输入的年份以及月份,输出对应的季节。

    1. n=input()
    2. if(3<=int(n[-2:])<=5):
    3. print("spring")
    4. elif(6<=int(n[-2:])<=8):
    5. print('summer')
    6. elif(9<=int(n[-2:])<=11):
    7. print('autumn')
    8. else:
    9. print('winter')

    9.BMI指数(即身体质量指数)是用体重公斤数除以身高米数平方得出的数字,是目前国际上常用的衡量人体胖瘦程度以及是否健康的一个标准。例如:一个人的身高为1.75米,体重为68千克,他的BMI=68/(1.75^2)=22.2(千克/米^2)。当BMI指数为18.5~23.9时属正常,否则表示身体存在健康风险。编程判断人体健康情况。

    1. weight,height = map(float,input().split(' '))
    2. BMI = weight / (height ** 2)
    3. if (BMI >= 18.5) and (BMI <= 23.9):
    4. print('Normal')
    5. else:
    6. print('Abnormal')

    10.小乐乐获得4个最大数,请帮他编程找到最大的数。

    print(max(list(map(int, input().split(' ')))))

    11.KiKi想判断输入的字符是不是字母,请帮他编程实现。

    1. while True:
    2. try:
    3. s = input()
    4. if s.isalpha():
    5. print("{} is an alphabet.".format(s))
    6. else:
    7. print('{} is not an alphabet.'.format(s))
    8. except:
    9. break

    12.牛牛从键盘输入一个整数,请你判断这个整数能被 2 3 7 中哪几个数整除,并按升序输出。如果不能被 2 3 7 任意一个数整除则输出 n。

    1. n = int(input())
    2. if n % 2 == 0:
    3. print("2", end=" ")
    4. if n % 3 == 0:
    5. print("3", end=" ")
    6. if n % 7 == 0:
    7. print("7")
    8. if n % 2 != 0 and n % 3 != 0 and n % 7 != 0:
    9. print("n")

    13.输入10个整数,分别统计输出正数、负数的个数。

    1. num_list = list(map(int, input().split()))
    2. pos = 0
    3. neg = 0
    4. for i in num_list:
    5. if i > 0:
    6. pos += 1
    7. else:
    8. neg += 1
    9. print('positive:%d' % pos)
    10. print('negative:%d' % neg)

    14.KiKi非常喜欢网购,在一家店铺他看中了一件衣服,他了解到,如果今天是“双11”(11月11日)则这件衣服打7折,“双12” (12月12日)则这件衣服打8折,如果有优惠券可以额外减50元(优惠券只能在双11或双12使用),求KiKi最终所花的钱数。

    1. a,b,c,d = input().split()
    2. a = float(a)
    3. b,c,d = map(int,(b,c,d))
    4. if b==11 and c==11:
    5. if d==1:
    6. m = a*0.7-50
    7. else:
    8. m = a*0.7
    9. elif b==12 and c==12:
    10. if d==1:
    11. m = a*0.8-50
    12. else:
    13. m = a*0.8
    14. else:
    15. m = a
    16. if m <= 0.00:
    17. print('0.00')
    18. else:
    19. print('%.2f'%m)

    15.牛牛正在寄快递,他了解到快递在 1kg 以内的按起步价 20 元计算,超出部分按每 kg 1元计算,不足 1kg 部分按 1kg计算。如果加急的话要额外付五元,请问牛牛总共要支付多少快递费。

    1. n = input().split()
    2. a, b = float(n[0]), n[1]
    3. if a <= 1:
    4. price = 20
    5. elif a > 1 and a % 1 == 0:
    6. price = (a - 1) * 1 + 20
    7. else:
    8. price = (a - 1 + 1) * 1 + 20
    9. if b == 'y':
    10. price = price + 5
    11. print(int(price))

    16.牛牛商场促销活动:

    满100打9折;满500打8折;满2000打7折;满5000打6折;

    牛阿姨算不清楚自己应该付多少钱,请你帮忙算一下。

    1. money = float(input())
    2. if money >= 5000:
    3. print('{:.1f}'.format(money*0.6))
    4. elif money >= 2000:
    5. print('{:.1f}'.format(money*0.7))
    6. elif money >= 500:
    7. print('{:.1f}'.format(money*0.8))
    8. elif money >=100:
    9. print('{:.1f}'.format(money*0.9))

     17.牛牛的通勤路上有两种选择,要么走路,要么打车,牛牛走路的速度是 1m/s 。打车的速度的 10m/s ,但是打车需要等出租车 10 s,请你计算牛牛想尽快到公司应该选择打车还是走路。

    1. a = int(input())
    2. if a < a / 10 + 10:
    3. print('w')
    4. else:
    5. print('v')

    18.牛牛的一周有七天,从周一到周日对应的英文是
    星期一:Monday、星期二:Tuesday、星期三:Wednesday
    星期四:Thursday、星期五:Friday、星期六:Saturday、星期日:Sunday
    牛牛知道今天是星期几,请输出这天的英文。

    1. day = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday']
    2. a = int(input()) - 1
    3. print(day[a])

    19.KiKi访问网站,得到HTTP状态码,但他不知道什么含义,BoBo老师告诉他常见HTTP状态码:200(OK,请求已成功),202(Accepted,服务器已接受请求,但尚未处理。)400(Bad Request,请求参数有误),403(Forbidden,被禁止),404(Not Found,请求失败),500(Internal Server Error,服务器内部错误),502(Bad Gateway,错误网关)。

    1. http_dict = {"200":"OK","202":"Accepted",
    2. "400":"Bad Request","403":"Forbidden",
    3. "404":"Not Found","500":"Internal Server Error",
    4. "502":"Bad Gateway"}
    5. while True:
    6. try:
    7. http_info = input()
    8. print(http_dict[http_info])
    9. except:
    10. break

    20.KiKi想知道已经给出的三条边a,b,c能否构成三角形,如果能构成三角形,判断三角形的类型(等边三角形、等腰三角形或普通三角形)。

    1. while True:
    2. try:
    3. a,b,c = map (int,input().split())
    4. if a+b>c and a+c>b and b+c >a:
    5. if a==b==c:
    6. print('Equilateral triangle!')
    7. elif a ==b or b==c or c==a:
    8. print('Isosceles triangle!')
    9. else:
    10. print('Ordinary triangle!')
    11. else:
    12. print('Not a triangle!')
    13. except:
    14. break

    21.牛牛有一个学习计划,他计划在 y 年 m 月 d 日开始学习,但在之前他是绝不会开始学习的,但是他一旦开始学习就不会停下来,请你判断 y1 年 m1 月 d1 日牛牛应该学习吗。

    1. import datetime
    2. y,m,d = map(int,input().split())
    3. y1,m1,d1 = map(int,input().split())
    4. a = datetime.date(y, m, d)
    5. b = datetime.date(y1,m1,d1)
    6. if a<=b:
    7. print('yes')
    8. else:
    9. print('no')

    22.从键盘输入a, b, c的值,编程计算并输出一元二次方程ax2 + bx + c = 0的根,当a = 0时,输出“Not quadratic equation”,当a ≠ 0时,根据△ = b2 - 4*a*c的三种情况计算并输出方程的根。

    1. import math
    2. while True:
    3. try:
    4. a,b,c=map(float,input().split())
    5. x=b*b-4*a*c
    6. if a==0:
    7. print("Not quadratic equation")
    8. else:
    9. if x==0:
    10. x1=(0-b)/(2*a)+0.00001
    11. print("x1=x2={:.2f}".format(x1))
    12. elif x>0:
    13. x1=((0-b)-math.sqrt(x))/(2*a)
    14. x2=((0-b)+math.sqrt(x))/(2*a)
    15. print("x1={:.2f};x2={:.2f}".format(x1,x2))
    16. else:
    17. if a<0:
    18. x1=(0-b)/(2*a)
    19. x2=0-math.sqrt(0-x)/(2*a)
    20. print("x1={:.2f}-{:.2f}i;x2={:.2f}+{:.2f}i".format(x1,x2,x1,x2))
    21. else:
    22. x1=(0-b)/(2*a)
    23. x2=math.sqrt(0-x)/(2*a)
    24. print("x1={:.2f}-{:.2f}i;x2={:.2f}+{:.2f}i".format(x1,x2,x1,x2))
    25. except:
    26. break

    23.KiKi想获得某年某月有多少天,请你编程实现,输入年份和月份,计算这一年这个月有多少天。

    1. while True:
    2. try:
    3. a,b=map(int,input().split())
    4. list31=[1,3,5,7,8,10,12]
    5. list30 = [4,6,9,10]
    6. if(list31.count(b)==1):
    7. print(31)
    8. elif(b==2):
    9. if(a%4==0):
    10. print(29)
    11. else:
    12. print(28)
    13. else:
    14. print(30)
    15. except:
    16. break

    24.小乐乐的班级进行了一次期中考试,考试一共有3门科目:数学,语文,英语,小乐乐的班主任决定给没有通过考核的同学家长开一次家长会,考核的标准是三科平均分不低于60分,所以现在想请你帮忙算一算小乐乐会不会被叫家长。

    1. scores = list(map(int, input().split()))
    2. mean = sum(scores)/len(scores)
    3. if mean < 60:
    4. print("YES")
    5. else:
    6. print("NO")

    25.每一本正式出版的图书都有一个ISBN号码与之对应,ISBN码包括9位数字、1位识别码和3位分隔符,其规定格式如“x-xxx-xxxxx-x”,其中符号“-”是分隔符(键盘上的减号),最后一位是识别码,例如0-670-82162-4就是一个标准的ISBN码。ISBN码的首位数字表示书籍的出版语言,例如0代表英语;第一个分隔符“-”之后的三位数字代表出版社,例如670代表维京出版社;第二个分隔之后的五位数字代表该书在出版社的编号;最后一位为识别码。
    识别码的计算方法如下:
    首位数字乘以1加上次位数字乘以2……以此类推,用所得的结果mod 11,所得的余数即为识别码,如果余数为10,则识别码为大写字母X。例如ISBN号码0-670-82162-4中的识别码4是这样得到的:对067082162这9个数字,从左至右,分别乘以1,2,…,9,再求和,即0×1+6×2+……+2×9=158,然后取158 mod 11的结果4作为识别码。
    你的任务是编写程序判断输入的ISBN号码中识别码是否正确,如果正确,则仅输出“Right”;如果错误,则输出你认为是正确的ISBN号码。

    1. st=input().replace('-','')
    2. s=sum([(ord(st[i])-48)*(i+1) for i in range(9) if st[i]!='-'])%11
    3. s='X' if s==10 else str(s)
    4. print('Right' if s==st[9] else st[0]+'-'+st[1:4]+'-'+st[4:9]+'-'+s)

    26.KiKi实现一个简单计算器,实现两个数的“加减乘除”运算,用户从键盘输入算式“操作数1运算符操作数2”,计算并输出表达式的值,如果输入的运算符号不包括在(+、-、*、/)范围内,输出“Invalid operation!”。当运算符为除法运算,即“/”时。如果操作数2等于0.0,则输出“Wrong!Division by zero!”

    1. while True:
    2. try:
    3. lines = input()
    4. for ch in lines:
    5. if ch in ['+', '-', '*', '/']:
    6. a, b = map(float, lines.split(ch))
    7. if ch == '+':
    8. print('{:.4f}+{:.4f}={:.4f}'.format(a, b, a+b))
    9. elif ch == '-':
    10. print('{:.4f}-{:.4f}={:.4f}'.format(a, b, a-b))
    11. elif ch == '*':
    12. print('{:.4f}*{:.4f}={:.4f}'.format(a, b, a*b))
    13. else:
    14. if b == 0.0:
    15. print('Wrong!Division by zero!')
    16. else:
    17. print('{:.4f}/{:.4f}={:.4f}'.format(a, b, a/b))
    18. break
    19. else:
    20. print('Invalid operation!')
    21. except:
    22. break

  • 相关阅读:
    死磕面试系列,Java到底是值传递还是引用传递?
    wxpython控件textctrl如何设置enter事件
    461. 汉明距离
    高等教育心理学:学习的基本理论(重要)
    springboot banner
    winform中也可以这样做数据展示✨
    记一次 .NET 某金融企业 WPF 程序卡死分析
    网络安全(黑客)自学
    pg和oracle的区别
    python中的迭代器
  • 原文地址:https://blog.csdn.net/u013157570/article/details/127986042