2020.5.1
学习第十天了吧,我都快记不清日子了~~~
放假的第一天
早起码代码~~
自定义函数求一元二次方程
Java 代码:
import java.util.Scanner;
class Solving {
int a, b, c;
Solving(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
void ShowAnswer() {
double t = b * b - 4 * a * c;
double x1 = (double)-b / (2 * a), x2;
if (t >= 0) {
x2 = (Math.sqrt(t)) / (2 * a);
System.out.println("x1=" + String.format("%.3f", x1) + "+" + String.format("%.3f", x2) + " x2=" + String.format("%.3f", x1) + "-" + String.format("%.3f", x2));
} else {
x2 = (Math.sqrt(-t)) / (2 * a);
System.out.println("x1=" + String.format("%.3f", x1) + "+" + String.format("%.3f", x2) + "i x2=" + String.format("%.3f", x1) + "-" + String.format("%.3f", x2) + "i");
}
}
}
public class SolvingEquation {
public static void main(String[] args) {
int a, b, c;
Scanner input = new Scanner(System.in);
a = input.nextInt();
b = input.nextInt();
c = input.nextInt();
Solving solv = new Solving(a, b, c);
solv.ShowAnswer();
}
}
C 代码
#include<stdio.h>
#include<math.h>
void SolvingEquation(int a, int b, int c);
int main()
{
int a, b, c;
scanf("%d%d%d",&a,&b,&c);
SolvingEquation(a,b,c);
return 0;
}
void SolvingEquation(int a, int b, int c)
{
double t,x;
x = (double)-b / (2 * a);
t = (double)(b * b - 4 * a * c);
if (t >= 0)
printf("x1=%.3lf+%.3lf x2=%.3lf-%.3lf", x, sqrt(t) / (2 * a), x, sqrt(t) / (2 * a));
else
printf("x1=%.3lf+%.3lfi x2=%.3lf-%.3lfi", x, sqrt(-t) / (2 * a), x, sqrt(-t) / (2 * a));
}
好好学习,天天向上!!
如有错误恳请大佬指正,感激不尽~~
算法题目来源:https://www.dotcpp.com/