余五阳
2019-11-24 10:59
采纳率: 80%
浏览 188

请问我这个十进制转化二进制程序哪里有问题?

#include
int main ()
{
int n,x,d,m=0;
scanf("%d",&n);
while(n--)
{
scanf("%d",&x);
if(x>0&&x<=10000)
m=0;
d=x;
do
{
m=m*10+x%2;
x=x/2;
}while(x!=0);
if(d%2!=0)
printf("%d\n",m);
if(d%2==0)
printf("%d\n",m*10);
}
return 0;
}
它编译无问题,就是运行时输入2,12等数时出现问题,5,10却没问题

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • bostonAlen 2019-11-24 11:44
    已采纳
    #include <stdio.h>
    #include <math.h>
    int main()
    {
        int n, x, d, m = 0;
        scanf_s("%d", &n);
        while (n--)
        {
            scanf_s("%d", &x);
            if (x>0 && x <= 10000)
                m = 0;
            d = x;
            for (int i = 0; x != 0; i++, x = x / 2){
                m = (x % 2) * pow((double)10, i - 1) + m;
            }
    
            if (d % 2 != 0)
            {
                if (d == 1)
                    printf("%d\n", d);
                else
                printf("%d\n", m);
            }   
            if (d % 2 == 0)
                printf("%d\n", m * 10);
        }
        return 0;
    }
    
    点赞 打赏 评论
  • yuanxie112233 2019-11-24 11:22
    #include <stdio.h>
    #include <math.h>
    int main ()
    {
        int n,x,d,m=0;
        scanf("%d",&n);
        while(n--)
        {
            scanf("%d",&x);
            if(x>0&&x<=10000)
                m=0;
            d=x;
            for(int i = 0;x != 0; i++,x = x/2 ){
                m = (x%2) * pow((double)10, i - 1) + m;
            }
    
            if(d%2!=0)
                printf("%d\n",m);
            if(d%2==0)
                printf("%d\n",m*10);
        }
        return 0;
    }
    
    点赞 打赏 评论

相关推荐 更多相似问题