算法找到了一些,其中的4安培数的乘积; 7是最大的给定范围 [英] algorithm to find a number in which product of number of 4 & 7 is maximum in given range

查看:165
本文介绍了算法找到了一些,其中的4安培数的乘积; 7是最大的给定范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被困在一个问题中,下界和上限 U 给出。
现在,在整数的小数重presentation假设X 数字4出现 A 时间和数字7出现 B 次。
问题是要找到 X 具有 A的最大值* B L< = X&LT = U
。 有没有什么有效的算法来解决呢?

I am stuck in a question in which lower bound L and Upper bound U is given.
Now suppose in the decimal representation of integer X digit 4 appears A times and digit 7 appears B times.
Problem is to find X which has maximum value of A*B for L<=X<=U.
Is there any efficient algorithm to solve it?

推荐答案

如果我理解正确的问题,下面应该工作:

If I understood the problem correctly, the following should work:

  • 在假设所有的数字具有相同位数的(如果如有小于 U ,我们只需填写开头的0秒位)。
  • 我们的以Z = U -
  • 现在,我们从第一个(/最高/最左边)的位最后一个。如果我们在看的个数位,让(我), U (我),以Z (I)和 X (i)是对应的数字。
    • 在所有领先的以Z (我),这是0,我们将 X (我)= (我)(我们不'吨有一个选择)。
    • 对于第一个不为0的以Z (一)检查:是否有4或间隔7 [(我), U (我)-1]?如果是让 X (我)是有4或7以其他方式允许的 X (我)= U (我)-1。
    • 现在,填补了 X 与您所选购的4,如果您分配了更多的7S到目前为止,反之亦然4S和7S等。
    • 休息
    • Assume all numbers have the same number of digits (if e.g. L has less digits than U, we can just fill in the beginning with 0 s).
    • Let Z = U - L.
    • Now we go from the first (/highest/leftmost) digit to the last one. If we are looking at the i th digit, let L(i), U(i), Z(i) and X(i) be the corresponding digit.
      • for all leading Z(i) which are 0, we set X(i) = L(i) (we don't have a choice).
      • For the first not 0 Z(i) check: is there a 4 or a 7 in the interval [L(i), U(i)-1]? If yes let X(i) be that 4 or 7 otherwise let X(i) = U(i)-1.
      • Now fill up the rest of X with 4s and 7s such that you choose a 4 if you have assigned more 7s so far and vice versa.

      也许一个例子可以理解这方面的帮助:

      Maybe an example can help in understanding this:

      由于 U = 5000和 = 4900。

      Given U = 5000 and L = 4900.

      现在的以Z = 0100。

      Now Z = 0100.

      这是我们设置的算法

      • X (1)= (1)= 4(我们没有选择)
      • X (2)= U (2)-1 = 9(第一个非0的数字在以Z
      • X (3)= 7(我们已经有了一个4)
      • X (4)= 4(可任意选择)
      • X(1) = L(1) = 4 (we have no choice)
      • X(2) = U(2)-1 = 9 (the first non 0 digit in Z)
      • X(3) = 7 (we already had a 4)
      • X(4) = 4 (can be chosen arbitrarily)

      领导为 X = 4974与客观2 * 1 = 2

      Leading to X = 4974 with an objective of 2*1=2

      这篇关于算法找到了一些,其中的4安培数的乘积; 7是最大的给定范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆