- 1、本文档共16页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
2009有道编程复赛
??题目: UnrepeatingNunbers (350 points)
Problem Statement:
如果一个数字十进制表达时,不存在连续两位数字相等,则称之为“不重复数”。例如,105,1234和12121都是“不重复数”,而11,100和1225不算。给定一个long类型数字A,返回大于A的最小“不重复数”。
Definition:
Class: UnrepeatingNumbers Method: next Parameters: long Returns: long Method signature: long next(long A)(be sure your method is public)
Constraints:
A 取值范围是[0, 10^17],注意是闭区间。
Examples:
0) 54 returns: 56 大于54的最小数字是55,但55不是“不重复数”。下一个数字是56,它满足条件。
1) 10 returns: 12
2) 9 returns: 10
3) 98 returns: 101 99和100都不是“不重复数”, 101是。
4) 21099 returns: 21201 解题报告:
The first solution that one could think of is to just increase A until it becomes a unrepeating number. Verifying that a number is unrepeating requires to decompose it into a list of digits, which is a simple task. Unfortunately, the constraints for the problem said that A can be as large as 10^17, we can notice that with a value of A equal to 1 and 17 zeros, our algorithm would take a lot of time before the two most significant repeated digits are dealt with.However, the observation about spending too much time before dealing with significant repeated digits can be helpful. A good idea to optimize the previous algorithm would be to simply increase the digits that we need to increase. Instead of increasing the whole number until the repetition is handled correctly. This is actually an idea good enough to solve the problem. Unfortunately a lot of coders didnt consider special cases when implementing it which caused plenty of failed solutions. This was mostly due to the examples all having the repeated numbers in the least significant digits, which made it challenging to analyze the problem correctly. Lets analyze a good case in order to design an algorithm, A = 122055123, since the result must be greater than A, we will begin with 122055124:We find a first pair of repeated digits 22, the number needs to be changed within the second 2, we might increase the left side of the number and leave the other side intact.
文档评论(0)