如何否定-2号基数? [英] How to negate base -2 numbers?
问题描述
我最近接受了Codility测试,我想知道如何否定 -2 base 数字?
I was given a Codility test lately and I was wondering how can I negate -2 base numbers?
例如数组 [1,0,0,1,1]
在 base -2 中表示 9 :
-2 bases:
1,-2,4,-8,16
1 + (-8) + 16 = 9
[1,0,0,1,1]
基数-2 中的负9为:
-2 bases:
1,-2,4,-8
1 + (-2) + -8 = -9
[1,1,0,1]
我对这个问题一无所知。必须有一些直观的解决方案。你有任何提示吗?
I'm in the dark regarding the question. There must be some intuitive solution for this. Do you have any hints?
推荐答案
在base− 2,位置 i 的1表示(-2) i 。
In base −2, a 1 at position i means (−2)i.
因此,[ i]位置为[1,1] , i +1]表示(-2) i + ( - 2) I 1 &NBSP; =&NBSP;( - 2) I &NBSP; +&NBSP;( - 2)( - 2)< SUP> I &NBSP; =&NBSP;(1&NBSP; +&NBSP; -2)( - 2) I &NBSP; = - ( - 2) i 。
So, a [1,1] in positions [i,i+1] means (−2)i + (−2)i+1 = (−2)i + (−2)(−2)i = (1 + −2)(−2)i = −(−2)i.
所以你可以否定任何出现的事件[1,0 ]通过将其更改为[1,1],反之亦然。
So you can negate any occurrence of a [1,0] by changing it to a [1,1], and vice versa.
当然,任何其他出现的0都可以保持不变:-0 =  ; 0。
Any other occurrences of 0, of course, can be left intact: −0 = 0.
所以在你的例子中,我们将[1,0,0,1,1]分成[{1,0},{0},{1 ,1}],否定每个部分得到[{1,1},{0},{1,0}],即[1,1,0,1,0],并删除不必要的高0,产生[1,1,0,1]。
So in your example, we split [1,0,0,1,1] into [{1,0}, {0}, {1,1}], negate each part to get [{1,1}, {0}, {1,0}], i.e., [1,1,0,1,0], and remove the unnecessary high 0, producing [1,1,0,1].
这篇关于如何否定-2号基数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!