简体   繁体   English

正负数&&负数负数对正数

[英]Positive to negative && Negative to positive in negative base

We are given an array consisting of 0's and 1's. 给定一个由0和1组成的数组。 They represent a number in base -2. 它们代表以2为底的数字。 Example: A = (1, 1, 0, 1, 0) in decimal = (-2)^0 *(1) + (-2)^1 *(1) + (-2)^2 *(0) + (-2)^3 *(1) + (-2)^4 *(0) = 1 + (-2) + 0 + (-8) + 0 = -9 示例:A =(1,1,0,1,0)十进制=(-2)^ 0 *(1)+(-2)^ 1 *(1)+(-2)^ 2 *(0) +(-2)^ 3 *(1)+(-2)^ 4 *(0)= 1 +(-2)+ 0 +(-8)+ 0 = -9

Now, we need to convert -9 to 9 in base -2. 现在,我们需要将以-2为底的-9转换为9。 Here's my code so far: 到目前为止,这是我的代码:

vector<int> negative_base(vector<int> &A) {
    //first convert number to decimal base
    int n = 0;
    long count = A.size();
    int power_of_two = 1;
    for(int i=0;i<count;i++){
        n+=power_of_two*A[i];
        power_of_two = power_of_two*-2;
    }
    cout<<"number: "<<n<<endl;
    vector<int> base_minus_two;
    n=-n;
    while(n!=0){
        int x;
        if(n<0) {

            x = n%2;
            if(x!=0) x+=2;
            n = (n/-2) +1;

        } else {
            x = n%2;
            n = n/-2;
        }
        base_minus_two.push_back(x);
    }
    return base_minus_two;
}

I am asked to return the shortest possible chain of 0's and 1's. 我被要求返回最短的0和1链。 However, my code does not always do that. 但是,我的代码并不总是这样做。 For this example, it generates (1, 0, 1, 1, 1). 对于此示例,它生成(1、0、1、1、1)。 I think it's fine for this example, yet in some cases it give me long chains while there are other shorter versions. 我认为此示例很好,但是在某些情况下,它给了我长链,而另一些短版本却给了我。 In some cases, it's generating wrong results. 在某些情况下,它会产生错误的结果。 For instance, if we have to convert 23 to -23, we get {1, 0, 0, 0, 0, 0, 1, 1} as a result. 例如,如果必须将23转换为-23,则结果为{1,0,0,0,0,0,1,1}。 However, this number is not equal to -23, but to -63. 但是,此数字不等于-23,而是-63。 So, there must be something wrong going on with my calculation. 因此,我的计算肯定有问题。 I am following the simplest base conversion algorithm, where you keep dividing until you hit zero, saving all the remainders in a vector as you go on. 我遵循的是最简单的基本转换算法,在该算法中,您一直进行除法运算直到达到零,然后将所有余数保存在向量中。 It's a negative base, so result * (-2) + remainder should give you what you had previously. 这是一个负数基数,因此结果*(-2)+余数应为您提供以前的结果。

-23 = (-2) *  12  + 1
 12 = (-2) * (-6) + 0
- 6 = (-2) *   3  + 0
  3 = (-2) * (-1) + 1
 -1 = (-2) *   1  + 1
  1 = (-2) *   0  + 1

The result should be (1, 0, 0, 1, 1, 1), yet I am getting {1, 0, 0, 0, 0, 0, 1, 1} as I stated. 结果应该是(1、0、0、1、1、1),但是正如我所说的,我得到了{1、0、0、0、0、0、1、1}。 What's wrong with my code? 我的代码有什么问题?

I found what's wrong with the code. 我发现代码出了什么问题。 Here's the new version: 这是新版本:

vector<int> negative_base(vector<int> &A) {
//first convert number to decimal base
int n = 0;
long count = A.size();
int power_of_two = 1;
for(int i=0;i<count;i++){
    n+=power_of_two*A[i];
    power_of_two = power_of_two*-2;
}
cout<<"number: "<<n<<endl;
vector<int> base_minus_two;
n=-n;
while(n!=0){
    int x;
    if(n<0){
        x = n%2;
        if(x!=0){ x+=2;
        n = (n/-2) +1;
        }else{
            n = (n/-2);
        }

    }else{
        x= n%2;
        n = n/-2;

    }
    cout<<"n: "<< n <<" x: "<<x<<endl;
    base_minus_two.push_back(x);

}
return base_minus_two;
}

Your logic to test if remainder is negative is wrong (and you can split into sub functions). 您测试余数是否为负的逻辑是错误的(并且您可以拆分为子函数)。

struct div_with_positive_remainder_t
{
    int quot;
    int rem;
};

div_with_positive_remainder_t div_with_positive_remainder(int x, int y)
{
    if (y == 0)
        throw std::runtime_error("division by zero");
    int r = x % y;
    if (r < 0) {
        r += std::abs(y);
    }
    int q = (x - r) / y;
    return {q, r};
}

std::vector<int> to_negbase(int n, int negbase = -2)
{
    std::vector<int> res;

    while (n != 0) {
        auto div = div_with_positive_remainder(n, negbase);
        res.push_back(div.rem);
        n = div.quot;
    }
    return res;
}

And

int to_int(const std::vector<int> &digits, int base = -2) {
    int res = 0;
    int power = 1;
    for (auto digit : digits){
        res += power * digit;
        power *= base;
    }
    return res;
}

std::vector<int> negative_base(const std::vector<int> &v)
{
    return to_negbase(-to_int(v));
}

Demo 演示版

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM