题解 | #计算一元二次方程#

计算一元二次方程

http://www.nowcoder.com/practice/7da524bb452441b2af7e64545c38dc26

#include <stdio.h>
int main(){
    float a,b,c,d;
    while(~scanf("%f %f %f",&a,&b,&c)){
        if(a==0){
            printf("Not quadratic equation");}
        else{
            d=b*b-4*a*c;
            if(d==0){
                if(b==0.0) printf("x1=x2=%.2f\n",0);
               else
                   printf("x1=x2=%.2f\n",-b/a/2); }
            else if(d>0){
                if(b==0.0) printf("x1=%.2f;x2=%.2f\n",(0-sqrt(d))/a/2,(0+sqrt(d))/a/2);
                else
           printf("x1=%.2f;x2=%.2f\n",(-b-sqrt(d))/a/2,(-b+sqrt(d))/a/2);}
        else if(d<0){
             if(b==0.0)
                 if(sqrt(-d)/2/a>0.0)
                 printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n",0.0,sqrt(-d)/2/a,0.0,sqrt(-d)/2/a);
            else
                printf("x1=%.2f%.2fi;x2=%.2f%.2fi\n",0.0,sqrt(-d)/2/a,0.0,sqrt(-d)/2/a);
            else
                if(sqrt(-d)/2/a>0.0)
                printf("x1=%.2f-%.2fi;x2=%.2f+%.2fi\n",-b/2/a,sqrt(-d)/2/a,-b/2/a,sqrt(-d)/2/a);
            else
                printf("x1=%.2f%.2fi;x2=%.2f+%.2fi\n",-b/2/a,sqrt(-d)/2/a,-b/2/a,-sqrt(-d)/2/a);}
            //printf("x1=%.2f%.2fi\n",-b/2/a,-sqrt(-d)/2/a);
            //printf("x2=%.2f%.2fi\n",-b/2/a,sqrt(-d)/2/a);
        }
    }
}
题目思路不难,输出种类是真的多,第一个就是输出-0与0的区别,第二个就是在特征多项式小于0有复数时,复数的大小和符号的输出又分为两种,虚部为正和虚部为负,之前折腾好几次都卡住了,
索性继续刷后面的,现在后头来,整理一遍思路重新写了一个详详细细的老实本分版,提交测试,再修改可算是完善了,过了,原码如上。

全部评论

相关推荐

HNU_fsq:建议直接出国,这简历太6了。自愧不如
点赞 评论 收藏
分享
头像
10-09 19:35
门头沟学院 Java
洛必不可达:java的竞争激烈程度是其他任何岗位的10到20倍
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务