找到魔法数字的最小数量以获得总和 [英] Find minimum count of magic numbers to get a sum

查看:91
本文介绍了找到魔法数字的最小数量以获得总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有魔数仅包含1和0,例如0,1,10,11,100,101,110,111 .....等等。这些是整数值而不是二进制值。



问题是用最少的幻数来实现总和。



例如:23:11 + 11+ 1(3个幻数)

120:110+ 10(2个幻数)

什么可以是逻辑找到计算总和所需的最小幻数?



我尝试过:



这里的技巧是获取值中的最大数字。例如:对于23,最小幻数将为3,对于120将为2,对于651,它将为6.



我正在尝试实现/代码找到获得总和所需的幻数组合的逻辑,但我不知道如何继续使用代码。

逻辑如下:

第1步:获取值的长度。

步骤2:将值除以10得到模数。如果余数为0,那么幻数的单位数将为0或者如果大于0的单位数将为1.但是如何填充十位或百位的位置???

i面临着难以编码的问题。任何帮助赞赏

There are magic numbers comprising of only 1 and 0, such as 0, 1, 10, 11, 100, 101, 110, 111..... and so on. These are integer values and not binaries.

The question is to achieve a sum with minimum usage of magic numbers.

eg: 23 : 11 + 11+ 1 ( 3 magic numbers)
120: 110+ 10 (2 magic numbers)
What can be the logic to find the minimum magic numbers required to calculate the sum?

What I have tried:

the trick here is to get the max digit in the value. e.g: for 23,the minimum magic number will be 3 and for 120 it will be 2, for 651 it will be 6.

I am trying to implement/code the logic to find the combination of the magic numbers required to get the sum, but I am not sure how to proceed with the code.
The logic is stated below:
Step 1: get the length of the value.
Step 2: Divide the value by 10 and get the modulus. If the remainder is 0 then the unit digit of the magic number will be 0 or if its more than 0 than the unit digit will be 1. but how to fill down the ten's or hundred's position???
i am facing difficulty to code it down. any help appreciated

推荐答案

如果你拿一个数字 - 例如1234 - 并采取模数十:

If you take a number - for example 1234 - and take the modulous ten on it:
1234 % 10 = 4



如果你将整数除以10整数,取模数:


If you then integer divide the original number by ten, and take the modulus of that:

1234 / 10 = 123
123 % 10 = 3

您可以看到十位数字。重复操作会给你数百:

Which is as you can see the "tens" digit. Repeating the operations gives you the hundreds:

123 / 10 = 12
12 % 10 = 2

等等。


这篇关于找到魔法数字的最小数量以获得总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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