数组:数学序列
整数数组 A[i] (i > 1) 按以下方式定义:元素 A[k] ( k > 1) 是大于 A[k-1] 的最小数,使得它的数字等于数字 4* A[k-1] 的数字之和。
您需要编写一个程序,根据给定的第一个元素 A[1] 计算该数组中的第 N 个数字。
输入: 在一行标准输入中,有两个数字以一个空格分隔:A[1] (1 <= A[1] <= 100) 和 N (1 <= N <= 10000)。
输出: 标准输出应该只包含一个整数 A[N] ,即定义序列的第 N 个数字。
输入: 7 4
输出: 79
解释: 数组的元素如下:7、19、49、79...,第 4 个元素是解。
我尝试通过编写一个单独的函数来解决这个问题,该函数对于给定的数字 A[k] 计算其数字之和,并找到大于 A[k-1] 的最小数字,正如问题中所述,但没有成功。第一次测试由于内存限制而失败,第二次测试由于时间限制而失败,现在我不知道如何解决这个问题。一位朋友建议递归,但我不知道如何设置。
任何可以以任何方式帮助我的人请写信,并提出一些关于使用递归/DP 来解决这个问题的想法。谢谢。
An array of integers A[i] (i > 1) is defined in the following way: an element A[k] ( k > 1) is the smallest number greater than A[k-1] such that the sum of its digits is equal to the sum of the digits of the number 4* A[k-1] .
You need to write a program that calculates the N th number in this array based on the given first element A[1] .
INPUT:
In one line of standard input there are two numbers seperated with a single space: A[1] (1 <= A[1] <= 100) and N (1 <= N <= 10000).
OUTPUT:
The standard output should only contain a single integer A[N] , the Nth number of the defined sequence.
Input:
7 4
Output:
79
Explanation:
Elements of the array are as follows: 7, 19, 49, 79... and the 4th element is solution.
I tried solving this by coding a separate function that for a given number A[k] calculates the sum of it's digits and finds the smallest number greater than A[k-1] as it says in the problem, but with no success. The first testing failed because of a memory limit, the second testing failed because of a time limit, and now i don't have any possible idea how to solve this. One friend suggested recursion, but i don't know how to set that.
Anyone who can help me in any way please write, also suggest some ideas about using recursion/DP for solving this problem. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这与递归无关,几乎与动态编程无关。您只需要找到可行的优化以使其足够快。只是一个提示,尝试理解这个解决方案:
http://codepad.org/LkTJEILz
This has nothing to do with recursion and almost nothing with dynamic programming. You just need to find viable optimizations to make it fast enough. Just a hint, try to understand this solution:
http://codepad.org/LkTJEILz
这是 python 中的一个简单解决方案。它只使用迭代,即使对于快速而肮脏的解决方案,递归也是不必要的且效率低下。
附言。我知道我们实际上不应该给出家庭作业答案,但看起来 OP 是 C++ 类的介绍,所以可能还不知道 python,希望它看起来像伪代码。此外,该代码缺少许多简单的优化,这可能会使解决方案变得太慢。
Here is a simple solution in python. It only uses iteration, recursion is unnecessary and inefficient even for a quick and dirty solution.
PS. I know we're not really supposed to give homework answers, but it appears the OP is in an intro to C++ class so probably doesn't know python yet, hopefully it just looks like pseudo code. Also the code is missing many simple optimizations which would probably make it too slow for a solution as is.
这是相当递归的。
问题的核心是:
找到大于 K 的最小数 N,并且 digitalsum(N) = J。
如果 digitalsum(K) == J 则测试 N = K + 9 是否满足条件。
如果数字和(K) <那么 J 可能仅在个位数上与 K 不同(如果可以在不超过 9 的情况下实现数和)。
否则,如果digitsum(K) <= J,则新的数字为9,并且问题递归为“找到大于(K/10)且digitsum(N') = J-9的最小数字N',则N = N'*10 + 9"。
如果digitsum(K)> J 那么???
在每种情况下,N≤4*K
9 -> 18 根据第一条规则
52-> 55 根据第二条规则
99 -> 189 通过第三条规则,递归时使用第一条规则
25→ 100 需要第四种情况,我原本认为不需要。
还有更多反例吗?
It is rather recursive.
The kernel of the problem is:
Find the smallest number N greater than K having digitsum(N) = J.
If digitsum(K) == J then test if N = K + 9 satisfies the condition.
If digitsum(K) < J then possibly N differs from K only in the ones digit (if the digitsum can be achieved without exceeding 9).
Otherwise if digitsum(K) <= J the new ones digit is 9 and the problem recurses to "Find the smallest number N' greater than (K/10) having digitsum(N') = J-9, then N = N'*10 + 9".
If digitsum(K) > J then ???
In every case N <= 4 * K
9 -> 18 by the first rule
52 -> 55 by the second rule
99 -> 189 by the third rule, the first rule is used during recursion
25 -> 100 requires the fourth case, which I had originally not seen the need for.
Any more counterexamples?