JZOJ2133.2017.05.20【usaco2017_Mar Bronze & Silver】C组T1The Lost Cow

农民约翰在寻找丢失的奶牛Bessie,他在一条直线上来回移动,采用'Z'模式搜索策略。根据算法,他至少需要行走9次直接距离才能找到Bessie。题目要求计算按此策略行走的总距离。给定起点和终点坐标,程序需要模拟这一过程并输出总距离。
摘要由CSDN通过智能技术生成

题目描述

    Farmer John has lost his prize cow Bessie, and he needs to find her!
   Fortunately, there is only one long path running across the farm, and Farmer John knows that Bessie has to be at some location on this path. If we think of the path as a number line, then Farmer John is currently at position xand Bessie is currently at position y (unknown to Farmer John). If Farmer John only knew where Bessie was located, he could walk directly to her, traveling a distance of |x−y|. Unfortunately, it is dark outside and Farmer John can't see anything. The only way he can find Bessie is to walk back and forth until he eventually reaches her position.

   Trying to figure out the best strategy for walking back and forth in his search, Farmer John consults the computer science research literature and is somewhat amused to find that this exact problem has not only been studied by computer scientists in the past, but that it is actually called the "Lost Cow Problem
好的,根据您的要求,以下是Python代码实现: ``` import csv constellation_list = [ ('Aries', '03.21-04.19'), ('Taurus', '04.20-05.20'), ('Gemini', '05.21-06.21'), ('Cancer', '06.22-07.22'), ('Leo', '07.23-08.22'), ('Virgo', '08.23-09.22'), ('Libra', '09.23-10.23'), ('Scorpio', '10.24-11.22'), ('Sagittarius', '11.23-12.21'), ('Capricorn', '12.22-01.19'), ('Aquarius', '01.20-02.18'), ('Pisces', '02.19-03.20') ] def get_constellation(month, day): day_in_month = [ 0, # 0 位未使用 20, # 1 - 1 月 50, # 2 - 2 月 79, # 3 - 3 月 110, # 4 - 4 月 141, # 5 - 5 月 172, # 6 - 6 月 203, # 7 - 7 月 234, # 8 - 8 月 265, # 9 - 9 月 295, # 10 - 10 月 325, # 11 - 11 月 355, # 12 - 12 月 ] if month < 1 or month > 12 or day < 1 or day > 31: return "输入日期错误!" if day < day_in_month[month]: return constellation_list[month-1][0] else: return constellation_list[month][0] def get_constellation_by_date(date): try: year = int(date[0:4]) month = int(date[4:6]) day = int(date[6:8]) except: return "输入日期错误!" return get_constellation(month, day) with open('const.csv', 'r', encoding="UTF-8") as csvfile: reader = csv.reader(csvfile) for row in reader: name = row[0] month = int(row[1]) day = int(row[2]) correct_name = get_constellation(month, day) if correct_name != name: print('有错:{} 的生日是{}月{}日,应该是{}座,而不是{}座'.format(name, month, day, correct_name, name)) print(get_constellation_by_date('19930918')) print(get_constellation_by_date('20200231')) ``` 上面的代码中,我们在代码最开始定义了星座列表`constellation_list`,里面存放了每个星座的名字及其起止日期。 接着,我们定义了一个`get_constellation()`函数,用于根据月份和日期计算对应的星座名称。该函数的实现过程为:首先,创建了一个存放每月最后一天的列表`day_in_month`,其中0位未使用,1~12位为相应月份的最后一天;然后,如果输入的月份或日期不合法,就返回一个“输入日期错误!”的提示;否则,当输入的日期小于相应月份的最后一天时,返回相应的星座名称,否则返回下一个月份的星座名称。 接着,我们定义了一个`get_constellation_by_date()`函数,用于读取`const.csv`文件并根据用户输入的出生日期获取对应的星座名称。该函数首先尝试将输入日期的年、月、日解析成整数,如果出错则返回一个“输入日期错误!”的提示;否则,调用`get_constellation()`函数计算对应的星座名称并返回。 最后,我们读取了`const.csv`文件并分别调用了`get_constellation_by_date()`函数获取了两个日期的星座名称,分别为白羊座和“输入日期错误!”(因为2月份只有28/29天,而这里输入的是30号,所以日期不合法)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值