代码不知道哪里出错了

img


#include
#include
    int main(void)
    {  
      /*********Begin*********/
      int M,N;
      float h,s;
      scanf("%d %d",&h,&s);
      h=M*pow(1/2,N);
      s=M+2*M*(1-pow(1/2,N-1))+h;
      printf("%.2f %.2f",h,s);
      /*********End**********/ 
       return 0;
    }

#include<stdio.h>
#include<math.h>
    int main(void)
    {  
      /*********Begin*********/
      float M,N,h,s;
      scanf("%f %f",&M,&N);
      h=M*pow(1.0/2,N);
      s= 2*2*M*(1-pow(1.0/2,N))-M;
      printf("%.2f %.2f",h,s);
      /*********End**********/ 
       return 0;
    }

参考GPT和自己的思路:

根据你提供的代码,有以下几个问题:

  1. 输入的变量类型与scanf格式符不匹配。h和s的类型应该为float,而输入的格式符为%d,应该改为%f。

  2. M和N没有初始化,直接赋值给h和s,会出现未定义的行为。需要先对M和N进行初始化,或者在scanf中给M和N赋值。

  3. pow函数的第二个参数应该是整型,而1/2是两个整数相除,结果为0,应该改为0.5。另外,建议使用sqrt函数开根号,更为简洁易懂。

修改后的代码如下:

#include <stdio.h>
#include <math.h>

int main(void)
{  
    int M = 1, N = 1;
    float h, s;
    scanf("%f %f", &h, &s);
    h = M * sqrt(2.0) * pow(0.5, N);
    s = M + 2 * M * (1 - pow(0.5, N - 1)) + h;
    printf("%.2f %.2f", h, s);
    return 0;
}

希望能对你有所帮助!

不是输入M和N的值吗?
怎么你这里输入的是h和s的值?
h和s的值不是计算得到的吗?

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632