另一个按位运算符拼图 [英] Another bitwise operators puzzle

查看:81
本文介绍了另一个按位运算符拼图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个数字,我们需要将其除以3,而不使用*,/,%运算符。那么问题还提到itoa()函数可用!!

Given a number, we need to divide it by 3, without using *, /, % operators. Well the question also mentions that itoa() function is available !!

推荐答案


给定一个数字,我们需要除以3,不使用*,/,%运算符。那么问题还提到itoa()函数可用!!
Given a number, we need to divide it by 3, without using *, /, % operators. Well the question also mentions that itoa() function is available !!



我没有尝试过itoa但它有点像跟随,希望它有所帮助:

I have not tried with itoa but it is something like following, hope it helps:

展开 | 选择 | Wrap | 行号


zodilla58'的解决方案将起作用,我想。但是,如果你已经达到分子,则通过除数检查向上计数必须称为强力算法:可用于小值,或者如果你只想做一次。


让我们看看我们是否不能提出更具可扩展性的东西。


提到itoa,表明我们可能想看一下10数字。


让我们看看3:27,33,102,300,402的几个倍数

啊!


看起来如果你将基数10加在一起,那么当有3的精确倍数时它们将总和为3,6或9。

这是真的吗?谁能找到一个反例?任何人都可以提供这样的数学证明吗?


编写一个快速程序可能更容易测试是否在3的第一个百万倍数上是真的。证明?也许没有,但我会相信。


假设它是真的,算法是直截了当的。


- 转换为ascii字符在基地10(itoa)

- 提取单个字符

- 将每个字符转换为小整数(0:9)

- 和整数

- 如果总和> 9,重复。

- 比较最终的,单个数字的总和与3,6和& 9
zodilla58''s solution will work, I think. However, counting upwards by the divisor checking all the time if you have reached the numerator must be called a brute force algorithm: workable for small values, or if you only want to do this once.

Let''s see if we can''t come up with something a little more scalable.

The mention of itoa, suggests that we might want to look at the base 10 digits.

Let''s look at a few multiples of 3: 27, 33, 102, 300, 402

Ah!

It looks like if you add the base 10 digits together, then they will sum to 3, 6 or 9 when there is an exact multiple of 3.

Is this true? Can anyone find a counter-example? Can anyone provide a mathematical proof that this has to be so?

It might be easier to write a quick program to test if this is true on the first million multiples of 3. Would that be proof? Maybe not, but I would be convinced.

Assuming it is true, the algorithm is straight-forward.

- convert to ascii character in base 10 ( itoa )
- extract individual characters
- convert each character to a small integer ( 0:9 )
- sum integers
- if sum > 9, repeat.
- compare final, single digit sum with 3, 6 & 9



看起来如果你将基数10位加在一起,那么当有一个数字时,它们总和为3,6或9 3的确切倍数。


这是真的吗?谁能找到一个反例?任何人都可以提供这样的数学证明吗?
It looks like if you add the base 10 digits together, then they will sum to 3, 6 or 9 when there is an exact multiple of 3.

Is this true? Can anyone find a counter-example? Can anyone provide a mathematical proof that this has to be so?



这显然不是3333的情况。这可以除以3,但总和为12.除非你再次加12 ...然后你会得到3而你是正确的。但是,如果没有,你有一个反例。

现在,如果我记得正确的表格学校你可以检查一个数字是否可以除以3,如果基数10位数的总和返回一个可除以3的数字。


我没有这方面的证据,这只是他们在数学上给我们买的东西:)


希望这会有所帮助

欢呼,

stefaan

this is obviously not the case take 3333. this is divisable by 3 but sums to 12. unless you sum 12 again ... then you would get 3 and you are correct. however if not there you have a counter example.
Now if I remember correct form school you can check if a number is divisable by 3 if the sum of the base 10 digits returns a number divisable by 3.

I don''t have proof for this, it''s just something they tought us at math :)

hope this helps
cheers,
stefaan


这篇关于另一个按位运算符拼图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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