鱼C论坛

 找回密码
 立即注册
查看: 1650|回复: 9

[已解决]我不知道是哪里出问题了,急急急

[复制链接]
发表于 2017-3-2 11:22:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
#include <stdio.h>
#include <math.h>
    //输入 2 = 00000000 00000000 00000000 00000010
    //输出 4294967294 = 11111111 11111111 11111111 11111110
   
int main(void){

  int num_binary[32] = {0}, index;
  unsigned int num, i, j = 32, a;  
  double count, total;

  scanf("%u", &num);
  while(num){
    a = num % 2;
    num_binary[j-1] = a;
    num = num / 2;
    j--;
  }

  for(i = 0;i < j;i++){
    if(num_binary[i]==0){
      num_binary[i] = 1;
    }
    else{
      break;
    }
  }

  for(index=0;index<j;index++){
    count = num_binary[index] * pow(2, j);
    total += count;
    j--;
  }
  printf("%u\n", (unsigned int)total);
}


按理说输出应该是4294967294,但是我这个输出的是2147418112. 求大神帮我看看为啥会出现这问题?
最佳答案
2017-3-5 12:02:18
我解释的没错,不过循环次数少了一次
  1. #include <stdio.h>
  2. #include <math.h>
  3.     //输入 2 = 00000000 00000000 00000000 00000010
  4.     //输出 4294967294 = 11111111 11111111 11111111 11111110

  5. int main(void){

  6.   int num_binary[32] = {0}, index;
  7.   unsigned int num, i, j = 32, a;
  8.   double count, total;

  9.   scanf("%u", &num);
  10.   while(num){
  11.     a = num % 2;
  12.     num_binary[j-1] = a;
  13.     num = num / 2;
  14.     j--;
  15.   }//此处j值已经改变并非32

  16.   for(i = 0;i < 32;i++){   //虽然此处用j不影响结果但是不符合逻辑
  17.     if(num_binary[i]==0){
  18.       num_binary[i] = 1;
  19.     }
  20.     else{
  21.       break;
  22.     }
  23.   }
  24.    j = 32;
  25.   for(index=0;index<32;index++){       //循环范围是0-31,j在变化
  26.     j--;
  27.     count = num_binary[index] * pow(2, j);
  28.     total += count;

  29.   }
  30.   printf("%u\n", (unsigned int)total);
  31.   return 0;
  32. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2017-3-2 13:50:46 | 显示全部楼层
就1个 j 值在变化,看代码有解释,如下。 另外可以学一下调试,这种错误调试一下一目了然
  1. #include <stdio.h>
  2. #include <math.h>
  3.     //输入 2 = 00000000 00000000 00000000 00000010
  4.     //输出 4294967294 = 11111111 11111111 11111111 11111110

  5. int main(void){

  6.   int num_binary[32] = {0}, index;
  7.   unsigned int num, i, j = 32, a;
  8.   double count, total;

  9.   scanf("%u", &num);
  10.   while(num){
  11.     a = num % 2;
  12.     num_binary[j-1] = a;
  13.     num = num / 2;
  14.     j--;
  15.   }//此处j值已经改变并非32

  16.   for(i = 0;i < 32;i++){   //虽然此处用j不影响结果但是不符合逻辑
  17.     if(num_binary[i]==0){
  18.       num_binary[i] = 1;
  19.     }
  20.     else{
  21.       break;
  22.     }
  23.   }
  24.    j = 32;
  25.   for(index=0;index<31;index++){       //循环范围是0-31,j在变化
  26.     j--;
  27.     count = num_binary[index] * pow(2, j);
  28.     total += count;

  29.   }
  30.   printf("%u\n", (unsigned int)total);
  31.   return 0;
  32. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-2 14:50:30 | 显示全部楼层
1
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2017-3-3 21:19:49 | 显示全部楼层
根据二楼大神的代码,输入2得到4294967294是正确的,但是输入3结果应该是4294967295才对呀,为啥结果还是4294967294呢???????
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-4 09:03:26 | 显示全部楼层
逻辑不对吧,题目贴出来看看。另外我也是一只小菜鸡。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-3-4 14:55:00 | 显示全部楼层
0mrli0 发表于 2017-3-4 09:03
逻辑不对吧,题目贴出来看看。另外我也是一只小菜鸡。

就是二楼的那个代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-4 14:56:31 | 显示全部楼层
xiaa22663 发表于 2017-3-4 14:55
就是二楼的那个代码

我说的题目不是说的代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-3-5 01:22:57 | 显示全部楼层
0mrli0 发表于 2017-3-4 14:56
我说的题目不是说的代码

比如说输入2=00000000000000000000000000000010,要把1前面的零全部变成1,输出的结果就是4294967294=11111111111111111111111111111110。
再举个例子789=00000000000000000000001100010101,输出的数字是4294967061=11111111111111111111111100010101。
代码的问题是输入3的时候结果应该是4294967295,但是输出的却是4294967294
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-3-5 12:02:18 | 显示全部楼层    本楼为最佳答案   
我解释的没错,不过循环次数少了一次
  1. #include <stdio.h>
  2. #include <math.h>
  3.     //输入 2 = 00000000 00000000 00000000 00000010
  4.     //输出 4294967294 = 11111111 11111111 11111111 11111110

  5. int main(void){

  6.   int num_binary[32] = {0}, index;
  7.   unsigned int num, i, j = 32, a;
  8.   double count, total;

  9.   scanf("%u", &num);
  10.   while(num){
  11.     a = num % 2;
  12.     num_binary[j-1] = a;
  13.     num = num / 2;
  14.     j--;
  15.   }//此处j值已经改变并非32

  16.   for(i = 0;i < 32;i++){   //虽然此处用j不影响结果但是不符合逻辑
  17.     if(num_binary[i]==0){
  18.       num_binary[i] = 1;
  19.     }
  20.     else{
  21.       break;
  22.     }
  23.   }
  24.    j = 32;
  25.   for(index=0;index<32;index++){       //循环范围是0-31,j在变化
  26.     j--;
  27.     count = num_binary[index] * pow(2, j);
  28.     total += count;

  29.   }
  30.   printf("%u\n", (unsigned int)total);
  31.   return 0;
  32. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2017-3-6 01:30:05 | 显示全部楼层
0mrli0 发表于 2017-3-5 12:02
我解释的没错,不过循环次数少了一次

感谢大神
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-1 04:39

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表