39.輸入任意的a,b,c求一元二次方程ax*x+bx+c=0的根?

來源:互聯網
上載者:User

標籤:style   blog   color   使用   os   io   檔案   div   

//從這個小題中可以熟悉標頭檔#include<cmath>的使用//1、題目較為簡單,只需運用判斷語句即可//2、注意,聲明變數時要想到根的無理性,同時需注意函數結果的強制類型轉換,sqrt只支援double和float類型#include<iostream>#include<cmath>//可以使用一些常用函數using namespace std;int main(){    int a,b,c,d;    float x1,x2,x3;    cout<<"please input a,b and c: "<<endl;    cin>>a>>b>>c;    d=b*b-4*a*c;    if(d>0)    {        x1=((-b+(float)sqrt(d))/(2*a));//開根號函數的使用,注意強制類型轉換        x2=((-b-(float)sqrt(d))/(2*a));        cout<<"方程有兩個解:"<<endl;        cout<<"x1="<<x1<<" x2="<<x2<<endl;    }else if(d==0)    {        cout<<"方程有一個解:"<<endl;        x3=((-b)/(2*a));        cout<<"x="<<x3<<endl;    }else    {        cout<<"方程沒有解。"<<endl;    }    return 0;}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.