C语言中的二元分解

C语言中的二元分解,c,binary,C,Binary,我正在一个培训网站上编写一些代码,将一个数字分解成二进制。我已经在我的本地编译器上对它进行了100次测试,它工作得很好,但是培训网站告诉我有错误 (我的代码既不优雅也不高效,尤其是循环,但我分解了代码以了解错误可能在哪里)。有人能告诉我是否有错误吗 #include <stdio.h> #include <stdlib.h> //function that displays the greatest power of 2 less than a fixed numbe

我正在一个培训网站上编写一些代码,将一个数字分解成二进制。我已经在我的本地编译器上对它进行了100次测试,它工作得很好,但是培训网站告诉我有错误

(我的代码既不优雅也不高效,尤其是循环,但我分解了代码以了解错误可能在哪里)。有人能告诉我是否有错误吗

#include <stdio.h>
#include <stdlib.h>


//function that displays the greatest power of 2 less than a fixed number N
int residu(int N)

{
    int i;
    int M=2;
    for(i=0;i<N;i++){
        if(M>N){break;}else{M=2*M;i++;}
    }
    return M/2;
}


int main()
{
    int i;

    //N is the input to decompose
    int N;
    scanf("%d",&N);
    //We will search for the greatest power of 2 less than a fixed number N, 
    //than repeating the some process with the residue of N with the greatest power of 2        //less than N, so we have to store the value of N for the loop (see below) we will use to work //correctly
    int M;
    M=N;
    //D displays the diffrence betwenn two successive powers of 2 that appears in the    //binary decomposition, (we will then print "O")
    int D;
    D=log(residu(N))/log(2);

        for(i=0;i<M;i++){
            //If N==residu(N), the decomposition is finished
            if(N==residu(N)){printf("1");int k;
                for(k=0;k<D;k++){printf("0");}break;}
            else{
             // N is a the residue of the former value of N and the greatest power of 2 //less than N
                N=N-residu(N);
                D=D-log(residu(N))/log(2);
                printf("1");
                int k;
                for(k=0;k<D-1;k++){printf("0");
                }
                D=log(residu(N))/log(2);
            }        
    }
}
#包括
#包括
//显示比固定数字N小2的最大幂的函数
内部剩余(内部N)
{
int i;
int M=2;
对于(i=0;iN){break;}或者{M=2*M;i++;}
}
返回M/2;
}
int main()
{
int i;
//N是要分解的输入
int N;
scanf(“%d”和“&N”);
//我们将搜索小于固定数字N的2的最大幂,
//而不是用最大幂为2/小于N的N的余数重复某个过程,因此我们必须为循环存储N的值(见下文),我们将使用它来正确地工作
int M;
M=N;
//D显示//二元分解中出现的2的两个连续幂之间的差异,(然后我们将打印“O”)
int D;
D=对数(剩余(N))/log(2);

对于(i=0;i,您需要包括数学库

#include <math.h>
#包括

如前所述,您缺少数学库的包含:

#include <math.h>
#包括

此外,还有一个错误,即此程序对输入“0”不起作用。

请尝试以下修复:

1) 为日志函数包括math.h

#include <math.h>
3) 从main返回一些东西。它是返回类型“int”,所以添加一个

return 0;
4) 如果仍然不行,您可以尝试显式键入强制转换以下语句的返回:

D=log(residu(N))/log(2);
D=D-log(residu(N))/log(2);
D=log(residu(N))/log(2);

它们会发出一个关于丢失数据的警告,即获取双重结果并将其存储在整数中。

这是浮点计算的典型问题。函数
log
与浮点一起工作

log(8)/log(2)
被计算为
2.999…
,然后在转换为
int
时被截断为
2

这就是为什么您会得到错误的结果。确切的行为取决于编译器/机器。有关进一步的阅读,请参阅例如

一般来说,以这种方式混合整数和浮点计算是一个坏主意。您的函数
residu
应该报告精确的二进制对数。或者您实现了一个专用函数来计算登录整数,如

unsigned binlog(unsigned n) {
    unsigned i = 0;
    while (n > 1) { n /= 2; ++i; }
    return i;
}

请告诉我-这些错误是什么?培训网站没有告诉你任何比“有错误”更具体的事情?我完全不知道,它告诉我我的代码输出的是110而不是1010,但我在10上尝试了它,我得到了正确的结果!还有两个警告:“警告:函数‘log’的隐式声明第30行:警告:内置函数‘log’的隐式声明不兼容”“@user1611830-这些警告是有原因的。我猜这可能是你的问题的根源。include没有抑制它们吗?是的,事实上我添加了这个特殊的情况,但我不想让我编辑的代码太大。谢谢你,我将尝试它。第2项:自1999年以来,你可以用C混合语句和声明(虽然有些人认为这是不好的做法)。这当然不是问题所在。@undur_gongor-我们(至少我)不知道“培训地点”是什么使用,我们也不知道它的选项。gcc可以使用它,但是,例如,如果您使用MS 2010 express,它实际上会在操作后轰炸声明。因为我们不知道发生了什么问题的详细信息,所以最好是安全的,然后抱歉。非常感谢,它工作得很好。我选择第二个选项。但是您有什么想法通过精确的二进制对数?是否可以使用本机日志函数?“本机”
log
函数(您应该使用
log2
btw.)计算近似于对数的浮点值(不精确)。由于您需要2次幂的二进制日志的精确值,
log
不适用。使用整数算法,您可以计算基数幂的精确对数(不是近似值)。
unsigned binlog(unsigned n) {
    unsigned i = 0;
    while (n > 1) { n /= 2; ++i; }
    return i;
}