综合练习
1.站编号和站名对应关系如下:
1=朱辛庄
2=育知路
3=平西府
4=回龙观东大街
5=霍营
//....
将以上对应关系的数据存储到map集合中,key:表示站编号,value:表示站名,并遍历打印(可以不按顺序打印):
第10站: 森林公园南门
第6站: 育新
第12站: 奥体中心
第13站: 北土城
//...
2.计算地铁票价规则:
总行程 3站内(包含3站)收费3元,
3站以上但不超过5站(包含5站)的收费4元,
5站以上的,在4元的基础上,每多1站增加2元,
10元封顶;
3.打印格式(需要对键盘录入的上车站和到达站进行判断,如果没有该站,提示重新输入,直到站名存在为止):
注意:每站需要2分钟
请输入上车站:
沙河
您输入的上车站:沙河不存在,请重新输入上车站:
上地
您输入的上车站:上地不存在,请重新输入上车站:
朱辛庄
请输入到达站:
沙河
您输入的到达站:沙河不存在,请重新输入到达站:
西二旗
您输入的到达站:西二旗不存在,请重新输入到达站:
西小口
从朱辛庄到西小口共经过6站收费6元,大约需要 12分钟
#需求1
list_sta = ['朱辛庄', '育知路', '平西府', '回龙观东大街', '霍营', '育新', '西小口', '永泰庄', '林萃桥', '森林公园南门','奥林匹克公园', '奥体中心', '北土城', '安华桥', '安德里北街', '鼓楼大街', '什刹海', '南锣鼓巷', '中国美术馆']
for i, j in enumerate(list_sta):
i += 1
print(f'第{i}站: {j}')
# print(list_sta.index('育知路'))
#需求2
def station_charge(station):
if station >= 1 and station < 3:
charge = 3
elif 3 <= station and station <= 5:
charge = 4
elif station > 5:
charge = 4 + (station - 5) * 2
if charge >= 10:
charge = 10
else:
charge = 0
print(f'从{sta_in}到{sta_out}共经过{station}站收费{charge}元,大约需要{station*2}分钟')
#需求3
sta_in = input('请输入上车站:')
sta_out = input('请输入到达站:')
while True:
if sta_in not in list_sta:
sta_in = input(f'您输入的上车站:{sta_in}不存在,请重新输入上车站:')
elif sta_out not in list_sta:
sta_out = input(f'您输入的到达站:{sta_out}不存在,请重新输入到达站:')
else:
station = list_sta.index(sta_out) - list_sta.index(sta_in)
station_charge(station)
break
35.问题描述
某校的惯例是在每学期的期末考试之后发放奖学金。发放的奖学金共有五种,获取的条件各自不同:
院士奖学金,每人8000元,期末平均成绩高于80分(>80),并且在本学期内发表1篇或1篇以上论文的学生均可获得;
五四奖学金,每人4000元,期末平均成绩高于85分(>85),并且班级评议成绩高于80分(>80)的学生均可获得;
成绩优秀奖,每人2000元,期末平均成绩高于90分(>90)的学生均可获得;
西部奖学金,每人1000元,期末平均成绩高于85分(>85)的西部省份学生均可获得;
班级贡献奖,每人850元,班级评议成绩高于80分(>80)的学生干部均可获得;
只要符合条件就可以得奖,每项奖学金的获奖人数没有限制,每名学生也可以同时获得多项奖学金。
例如姚林的期末平均成绩是87分,班级评议成绩82分,同时他还是一位学生干部,那么他可以同时获得五四奖学金和班级贡献奖,奖金总数是4850元。
基本要求
现在给出若干学生的相关数据,请计算哪些同学获得的奖金总数最高(假设总有同学能满足获得奖学金的条件)。
输入数据格式格式:
输入的第一行是一个整数N(1 <= N <= 100),表示学生的总数。
接下来的N行每行是一位学生的数据,从左向右依次是姓名,期末平均成绩,班级评议成绩,是否是学生干部,是否是西部省份学生,以及发表的论文数。
姓名是由大小写英文字母组成的长度不超过20的字符串(不含空格);期末平均成绩和班级评议成绩都是0到100之间的整数(包括0和100);
是否是学生干部和是否是西部省份学生分别用一个字符表示,Y表示是,N表示不是;发表的论文数是0到10的整数(包括0和10)。
每两个相邻数据项之间用一个空格分隔。
输出数据格式:
输出包括三行,第一行是获得最多奖金的学生的姓名,第二行是这名学生获得的奖金总数。如果有两位或两位以上的学生获得的奖金最多,
输出他们之中在输入文件中出现最早的学生的姓名。第三行是这N个学生获得的奖学金的总数。
'''
YaoLin 87 82 Y N 0
ChenRuiyi 88 78 N Y 1
LiXin 92 88 N N 0
ZhangQin 83 87 Y N 1
'''
#
N = int(input('输入一个整数N:'))
n = 1
d = {}
while n <= N:
line = input(f'请输入第{n}名学生信息')
stu_lst = line.split(' ')
d[stu_lst[0]] = stu_lst[1:]
n += 1
r_d = {}
#开始计算每一位学生的奖学金
for stu_name,stu_info in d.items():
if int(stu_info[0]) > 80 and int(stu_info[4]) > 0:
r_d[stu_name] = r_d.get(stu_name, 0) + 8000
if int(stu_info[0]) > 85 and int(stu_info[1]) > 80:
r_d[stu_name] = r_d.get(stu_name, 0) + 4000
if int(stu_info[0]) > 90:
r_d[stu_name] = r_d.get(stu_name, 0) + 2000
if int(stu_info[0]) > 85 and stu_info[3] == 'Y':
r_d[stu_name] = r_d.get(stu_name, 0) + 1000
if int(stu_info[1]) > 80 and stu_info[2] == 'Y':
r_d[stu_name] = r_d.get(stu_name, 0) + 850
sum = 0
max = 0
max_stu = ''
for name,money in r_d.items():
if max < money:
max = money
max_stu = name
sum += money
print(max_stu)
print(max)
print(sum)
任务目的
0.培养编程思维,提高分析问题能力
1.掌握二维数组
2.掌握循环,分支条件的用法
3.类的抽象
任务描述
设想有一只机械海龟,它在程序的控制下在屋里四处爬行。海龟拿了一支笔,这只笔或朝
上或朝下,当笔朝下时,海龟用笔画下自己的移动轨迹,当笔朝上时,海龟在移动过程中什么也不画。
使用一个50x50的数组,并把数组初始化为0。从一个装有命令的数组中读取各种命令。不
管是笔朝上还是笔朝下,都要跟踪海龟的当前位置。假定海龟总是从地板上(0,0)出发
,并且开始时笔是朝上的。程序必须处理的一组命令如下:
命令 含义
1 笔朝上
2 笔朝下
3 右转弯
4 左转弯
5,10 向前走10格(或其他的格数)
6 打印50x50的数组
9 数据结束(标记)
假设海龟现在处于靠近地板中心的某个位置,下面的“程序”绘制并打印出了一个12*12的方框。
2
5,12
3
5,12
3
5,12
3
5,12
1
6
9
在海龟爬行过程中,如果笔朝下,把数组floor中对应于海龟所处位置的元素置1。当给出命令6(打印)后,在数组中元素为1的位置全部用#号显示,元素为0的位置全部用*号显示。
编写一个可以处理上述海龟命令的程序。
编写一些其他的海龟命令,用自己编写的海龟命令处理程序解析,画出有趣的形状,比如一个“日”字。
import numpy as np
command = """
1
5,13
3
5,10
4
2
5,13
3
5,7
3
5,13
3
5,7
3
3
5,13
4
5,13
4
5,7
1
6
9
"""
lst = command.split()
print(lst)
floor = np.zeros((50,50),dtype='int32')
# print(map)
isPenUp = True
#0,1,2,3--->向右,向下,向左,向上
direction = 0
xPos = 0
yPos = 0
def print_floor():
for i in range(floor.shape[0]):
for j in range(floor.shape[1]):
if floor[i,j] == 0:
print('*',end=' ')
else:
print('#',end=' ')
print()
for cmd in lst:
true_cmd_lst = cmd.split(',')
if len(true_cmd_lst) == 1:
true_cmd = int(true_cmd_lst[0])
if true_cmd == 1:
isPenUp = True
elif true_cmd == 2:
isPenUp = False
elif true_cmd == 3:
direction += 1
if direction > 3:
direction = 0
elif true_cmd == 4:
direction -= 1
if direction < 0:
direction = 3
elif true_cmd == 6:
print_floor()
elif true_cmd == 9:
break
else:
true_cmd = int(true_cmd_lst[0])
step = int(true_cmd_lst[1])
if true_cmd == 5:
#向右
if direction == 0:
target = xPos + step
# if target >= 50:
# target = 49
if not isPenUp:
floor[yPos,xPos:target] = 1
xPos = target - 1
else:
xPos = target - 1
# 向下
if direction == 1:
target = yPos + step
# if target >= 50:
# target = 49
if not isPenUp:
floor[yPos:target,xPos] = 1
yPos = target - 1
else:
yPos = target - 1
# 向左
if direction == 2:
target = xPos - (step - 1)
# if target < 0:
# target = 0
if not isPenUp:
floor[yPos, target:xPos] = 1
xPos = target
else:
xPos = target
# 向上
if direction == 3:
target = yPos - (step - 1)
# if target < 0:
# target = 0
if not isPenUp:
floor[target:yPos, xPos] = 1
yPos = target
else:
yPos = target
网友评论