Question 1 / 1
You want to create a staff to use in your martial arts training, and it has to meet some specific requirements.
1. You want it to be composed of two smaller staves of equal length so that you can either use it as a single staff or as two smaller ones.
2. You want the full sized staff's center of gravity to be exactly in the middle of the staff.
You have a very, very long branch from which you can cut the pieces for your staff. The mass of the branch varies significantly throughout it, so you use just any two pieces of the same length. Given a description of the mass throughout the branch, determine the longest staff you can make, then return three integers on a single line, the first two indicating the first index of each half-staff, and the third indicating the length of each half-staff.
The input will be given on a single line as a string of digits [1-9], each digit representing the mass of a section of the branch. All sections are the same size. Here is an example:
41111921111119
11119 11119
If the indicated sections are cut from the branch they will satisfy your requirements. They are both the same length, and they can be put together as either 9111111119 or 1111991111, both of which have a center of gravity exactly in the center of the staff.
Center of gravity can be determined by taking a weighted average of the mass of each section of the staff. Given the following distances and masses:
Distance: 12345678
Mass: 22241211
Sum of the mass of each section: 2 + 2 + 2 + 4 + 1 + 2 + 1 + 1 = 15
Weighted sum of the masses:
2*1 + 2*2 + 2*3 + 4*4 + 1*5 + 2*6 + 1*7 + 1*8 = 60
Weighted sum / regular sum = 60 / 15 = 4
This means that the center of mass is in section 4 of the staff. If we wanted to use this staff the center of gravity would need to be (8+1)/2 = 4.5.
Here is an example problem:
131251141231
---- ----
If we take the sections indicated we get 1312 and 1231. By reversing the first one and putting them together we get21311231
Sum of the mass of each section: 2 + 1 + 3 + 1 + 1 + 2 + 3 + 1 = 14
Weight sum of the masses:
2*1 + 1*2 + 3*3 + 1*4 + 1*5 + 2*6 + 3*7 + 1*8 = 63
Weighted sum / regular sum = 63 / 14 = 4.5
This puts the center of mass exactly in the center of the staff, for a perfectly balanced staff. There isn't a longer staff that can be made from this, so the answer to this problem is
0 8 4
Because the half-staves begin at indices 0 and 8 (in that order) and each is of length 4.
------------------------------------------------------------------------------
Input 1: 123232111119232333277777999
Output 1: 7 15 6
Input 2: 7512839182731294837512653698759387212532563849823857812519853546649398328875256156256652116394915985281859358394738256421937941843758954891723598716547856473245243546392898871987152656238458214518158188152527386384518234758325165316563487283746285745938476523546127534721652812736459874658475366423876152387491872658763218276354827768598716283764571652637451962837648726876547826359871629836547862534761798346918275676473829648651672346981726587619462561625162561527384273482748237482734827348274827
Output 2: 10 262 229
-----------------------------------------------------------------------------------------------
How to implement it?
分享到:
相关推荐
这本书的书名——《打造Facebook:亲历Facebook爆发的5年》很嚣张,谁有资格可以说这句话呢,当然,扎克伯格最有资格,但他不会亲自来告诉你,至少从目前的情况来看,近几年都不大可能。而且,这不是一个人的公司。...
Facebook PHP SDK 4.0
facebook 分享文档 twitter 分享文档 vue 使用 facebook 使用分享 找到index.html 添加一下代码 <!-- facebook 分享 --> <div id=fb-root></div> [removed](function (d, s, id) { var js, fjs = d....
Facebook(脸书)是美国的一个社交网络服务网站 ,创立于2004年2月4日,总部位于美国加利福尼亚州门洛帕克,2012年3月6日发布Windows版桌面聊天软件Facebook Messenger [1] 。主要创始人马克·扎克伯格。 Facebook是...
Iphone开发系列源码——FaceBook基于Iphone的多个应用源码Iphone开发系列源码——FaceBook基于Iphone的多个应用源码Iphone开发系列源码——FaceBook基于Iphone的多个应用源码Iphone开发系列源码——FaceBook基于...
facebook分享实例代码
facebook安卓安装包,Facebook 是一个联系朋友的社交工具。大家可以通过它和朋友、同事、同学以及周围的人保持互动交流,分享无限上传的图片,发布链接和视频,更可以增进对朋友的 ...
里面包含facebook 的登录SDK 和 facebook的数据统计依赖工程
2019Facebook旅游白皮书-Facebook-201910.pdf2019Facebook旅游白皮书-Facebook-201910.pdf
Facebook开发包 Facebook开发包 Facebook开发包 Facebook开发包
Facebook-Android-sdk-4.8.2.zip
Facebook 数据中心曝光 机密信息一览无遗 一.Facebook 数据中心概括 【PConline 资讯】据来自 Google 的 Double Click 服务标准显示,Facebook 是目前世界上最受欢迎的网站,每月有超过 6900 亿的页面浏览量。...
Facebook基础培训
前几天弄过来的 facebook api挂出来希望对大家有帮助
kaggle-Facebook-V, 在Kaggle上,Facebook竞争对手的第二 2nd-place Facebook Facebook Facebook Facebook Facebook Facebook这是我在 Facebook V: 预测中的工作库,它预测了我在。关于这些想法和一些讨论的概述可以...
Facebook社交图景剖析,详细分析facebook是如何拉近人们之间的关系的。
facebook提示音效Facebook提示音效.mp3
facebook 的学习资料 帮助学习和了解facebook
Facebook提供的Unity3D插件。
bitlbee-facebook, 用于BitlBee的Facebook协议插件 bitlbee的Facebook协议插件。 这个插件使用基于 Facebook Messenger mqtt的协议。这里项目与 Facebook 。Inc不相关。用法bitlbee wiki中提供了通用用法说明:...