作业帮 > 综合 > 作业

C#求方程的根,题目要求无实根时带i.

来源:学生作业帮 编辑:作业帮 分类:综合作业 时间:2024/05/29 03:05:58
C#求方程的根,题目要求无实根时带i.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string i = Console.ReadLine();
string[] str = i.Split();
int a = int.Parse(str[0]);
int b = int.Parse(str[1]);
int c = int.Parse(str[2]);
double k=b*b-4*a*c;
double j=Math.Sqrt(k);
double x1,x2,j2;
if (j > 0)
{
x1 = (-b + j) / 2.0 * a;
x2 = (-b - j) / 2.0 * a;
Console.WriteLine("x1={0} x2={1}",x1,x2);
}
else if (j == 0)
{
x1 = (-b) / 2.0 / a;
x2 = (-b) / 2.0 / a;
Console.WriteLine("x1={0} x2={1}",x1,x2);
}
else
{
}
}
}
}
写了一半了,就是无实根不会写.顺便看看我前面的有没有错?
double k=b*b-4*a*c; //后面开始分类讨论一下就可以了
int i=0;//标记位 ,记录是否有虚根,有的话1,没有就是0
if(k