这个代码有问题的地方是没有计算出实际的判别式disc的值,导致无法正确判断三种情况。可以在输入a、b、c之后立即计算出disc的值,如下所示:

#include "stdio.h" #include "math.h"

int main(){ float a,b,c,disc; scanf("%f%f%f",&a,&b,&c); disc = bb-4ac; if(disc > 0){ float x1,x2,p,q; x1 = -b / (2.0a); x2 = sqrt(disc) / (2.0a); p = x1 + x2; q = x1 - x2; printf("%f,%f",p,q); } else if(disc == 0){ float x1; x1 = -b / (2.0a); printf("%f",x1); } else{ printf("无实数根!"); } return 0; }

#include stdioh#include mathhfloat Disc1float afloat bfloat c float disc = bb-4ac; float x1x2pq; x1 = -b 20a; x2 = sqrtdisc 20a; p = x1 + x2; q = x1 - x2; printfffpq;float Disc2

原文地址: https://www.cveoy.top/t/topic/bNuJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录