Spaceship purchase

2023-11-07 14:10
文章标签 purchase spaceship

本文主要是介绍Spaceship purchase,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

为什么80%的码农都做不了架构师?>>>   hot3.png

sofi想要买个飞船,所以需要跟卖船的老头讨价还价。

Write a program that, depending on Sofia and the old man’s initial bargaining sums--the steps by which they will increase or decrease the price during their negotiations, will calculate which price they will agree upon. If Sofia's offer is lower than or equal to the old man's offer, she will accept the old man's price (and vice versa).

Sofi makes her proposition first. She never proposes an amount higher than what is proposed to her. On the other hand, the old man never proposes an amount lower than what is proposed to him.

Input data: Contains four integer numbers: Sofia's initial offer, Sofia's raise to his offer, the initial counteroffer from the old man, and the old man's reduction in his offer;

Output data: The amount of money that Sofia will pay for the spaceship.

总是出错的一段代码:

<!-- lang: python -->
def checkio(offers):
'''the amount of money that Petr will pay for the ride
'''
sofi, raised, oldman, reduction = offers
while(1):# oldman was thinking...if(sofi > oldman):return sofielif(sofi > oldman - reduction):return sofi# sofi was thinking...elif(sofi + raised > oldman):return oldman# a circle was endedelse:sofi = sofi + raisedoldman = oldman - reduction

以下是算法运行步骤:

1.sofi出价,老头考虑。转2

2.老头觉得自己的报价会比sofi低,则接受sofi的价格。否则,转3

3.老头觉得自己下一轮的报价会比sofi低,则接受sofi的价格。否则,转4

4.老头报价,sofi考虑。转5

5.sofi觉得自己下一轮的报价会比老头高,则sofi接受老头的价格。否则,转1

看起来也没问题啊,但是总是出错。看到出错的用例之后,再回过头去考虑,我发现主要还是错在第3轮——这一步好像不该有。

修改算法步骤如下:

1.sofi出价,老头考虑。转2

2.老头觉得自己的报价会比sofi低,则接受sofi的价格。否则,转3

3.老头报价,sofi考虑。转4

4.sofi觉得自己下一轮的报价会比老头高,则sofi接受老头的价格。否则,转1

修改后的代码如下:

<!-- lang: python -->
def checkio(offers):
'''the amount of money that Petr will pay for the ride
'''
sofi, raised, oldman, reduction = offers
while(1):# oldman was thinking...if(sofi > oldman):return sofi# sofi was thinking...elif(sofi + raised > oldman):return oldman# a circle was endedelse:sofi = sofi + raisedoldman = oldman - reduction

通过了。

注释有点问题。老头思考的时候,只是针对sofi当前的出价。出价高于老头打算出的价格,则接受sofi的报价。老头不会在自己还没报出的价格上打折,所以第二个判断是多余的,只需要考虑sofi和oldman的关系就行了。

转载于:https://my.oschina.net/scroll/blog/120793

这篇关于Spaceship purchase的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/364145

相关文章

operator <=> (spaceship operator)

1. C++20 前如何定义比较运算符 C++20 之前,必须为一个类型定义六个操作符,以提供对象所有比较的支持。 例如,若要比较Value 类型的对象(具有整型ID),则须实现以下操作: class Value{private:long id;...public:...// equality operators:bool operator== (const Value& rhs) con

IAP(In App Purchase)流程总结

最近用到IAP内置购买,阅读官方文档,在网上找了些资料,在这里作下整理,以便日后查找和修改,主要流程方向确定,文档和相关转载内容截图不一一指出,google一堆。 1.查找官方文档,两张目录截图,对主要流程大致了解:                               官方文档: https://developer.apple.com/library/mac/#document

Codeforces Edu 74 E. Keyboard Purchase 【状压DP +贡献】

E. Keyboard Purchase 题意 给定一个长度为 n n n 的字符串 s s s 仅由前 m m m 个小写字母组成 现在要求求出包含前 m m m 个小写字母的键盘,使得在键盘上敲出 s s s 要移动的距离最短 移动总距离为: ∑ i = 2 n ∣ p o s s i − 1 − p o s s i ∣ \sum_{i = 2}^{n} | pos_{s

iOS In-App Purchase中涉及到的货币单位

转自:http://www.molotang.com/articles/1437.html iOS In-App Purchase中涉及到的货币单位 发表于  2014 年 2 月 18 日  由  三石 现在iOS的AppStore上各类应用非常丰富,有付费的也有免费的,而免费的应用也可以支持IAP (In-App Purchase,应用内支付) 。 对于应用内支付,

iOS 内付费(in-app purchase)--非消耗品的购买与恢复

iOS内付费的功能对于一个app来说是非常重要的,如果在这一环节出了一些致命的问题,那就很可能会影响app的推广和公司的利益了。 我在很早之前写过一篇关于iOS内付费的文章(文章地址),在那篇博客中讲述了如何在苹果后台生成iOS内付费商品,以及在我们的app工程中如何去添加相应的内付费代码。但是,在后来的日子里面我发现网友在关于内购功能上遇到的问题比我想象的还要多,其中很大一部分都是一些

Purchase order reminder dunning ME91F

Output type:MAHN Dunning 前台数据设置点: 想要实现reminder,前提是需要在PO delivery  Tab设置催促交货的时间1st Rem./Exped , 2nd Rem./Exped , 3rd Rem./Exped  (可以在主数据和采购信息记录中预设,在ME21N创建订单的时候就会带过来) 保存订单,ME91F执行能找到此单据的前提条件是单据至少已

[Python] Spaceship游戏初步实现 Mini-project #7

Spaceship顾名思义是一款太空游戏,受经典的街机游戏Asteroid(1979)启发编写而成。在Mini-project #7中,我们先实现飞船的控制、导弹的发射、星球的随机出现和运动,简单设置UI,从而展示Lives和Score。在Mini-project #8中会增加更多功能。 具体思路如下(注:这次描述不是按照代码显示顺序,而是按照完成的思路写的): 游戏细节描述在Coursera

使用fastlane spaceship 批量自动创建内购素材

使用fastlane spaceship 批量自动创建内购素材 背景 背景当然是产品同学发过来22个AR内购素材要提交,这个啥感觉做过的人都知道,不想多说了。(此处应有表情包) 方案 这时候只能选择fastlane spaceship了,spaceship本来是个独立的项目,后来被fastlane收编进来,为pilot,boarding和deliver等工具提供和 iTC 和 ADC 的交

Datawhale打卡活动 Kaggle Spaceship Titanic Day2

文章目录 Datawhale打卡活动 Kaggle Spaceship TitanicDay 2 比赛数据分析步骤1:使用pandas完成如下数据分析训练集和测试集的行数分别是多少?训练集中每列的类型是什么?训练集中标签是如何分布,与哪一个特征最相关?训练集中列缺失值如何分布的? 步骤2:使用seaborn或matplotlib完成如下可视化HomePlanet 与 Transported

Docker下使用fastlane+spaceship来登陆开发者账户管理证书

fastlane 不多介绍,spaceship 是在 fastlane 的基础上,对常用功能进行了进一步封装。 官方提供的了fastlane的Docker,可以通过 docker search fastlane 命令来查找。 排在第一个的 fastlanetools/fastlane 。docker pull fastlanetools/fastlane 拉取到本地。 拉取结束后,通过 do