1 条题解
-
0
C :
#include<stdio.h> #include"math.h" float yishigen(m,n,k) float m,n,k; {float x1,x2; x1=(-n+sqrt(k))/(2*m); x2=(-n-sqrt(k))/(2*m); printf("x1=%.3f x2=%.3f\n",x1,x2); } float denggen(m,n) float m,n; {float x; x=-n/(2*m); printf("x1=%.3f x2=%.3f\n",x,x); } float xugen(m,n,k) float m,n,k; {float x,y; x=-n/(2*m); y=sqrt(-k)/(2*m); printf("x1=%.3f+%.3fi x2=%.3f-%.3fi\n",x,y,x,y); } main() {float a,b,c,q; //printf("input a b c is "); scanf("%f%f%f",&a,&b,&c); //printf("\n"); q=b*b-4*a*c; if(q>0) yishigen(a,b,q); else if(q==0) denggen(a,b); else xugen(a,b,q); }C++ :
#include <iostream> #include <stdio.h> //#include <conio.h> #include <math.h> using namespace std; int main() { double a,b,c,k,a1,a2; cin >> a >> b >> c; k = b*b-4*a*c; if (k>0) { a1 = (-b+sqrt(k))/(2*a); a2 = (-b-sqrt(k))/(2*a); cout << "x1=" << a1 << " x2=" << a2; } else if (k==0){ a1 = -b/(2*a); cout << "x1=" << a1 << " x2=" << a1; } else { a1 = -b/(2*a); a2 = sqrt(-k)/(2*a); //cout << "x1=" << a1 << "+" << a2 << "i x2=" << a1 << "-" << a2 << "i"; printf("x1=%.3f+%.3fi x2=%.3f-%.3fi",a1,a2,a1,a2); } //_getch(); return 0; }Java :
import java.util.Scanner; public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner input = new Scanner(System.in); int a = input.nextInt(); int b = input.nextInt(); int c = input.nextInt(); double x1,x2; if(b*b > 4*a*c){ x1 = (-b + Math.sqrt(b*b - 4*a*c))/(2.0*a); x2 = (-b - Math.sqrt(b*b - 4*a*c))/(2.0*a); System.out.printf("x1=%.3f x2=%.3f",x1,x2); }else if(b*b == 4*a*c){ x1 = x2 = (-b/(2.0*a)); System.out.printf("x1=%.3f x2=%.3f",x1,x2); }else{ x1 = (-b/(2.0*a)); x2 = (Math.sqrt(4*a*c - b*b))/(2.0*a); System.out.printf("x1=%.3f+%.3fi x2=%.3f-%.3fi",x1,x2,x1,x2); } input.close(); } }Python :
a = map(lambda x:float(x), raw_input().split()) b = a[1] c = a[2] a = a[0] def f(x): if x>=0: return '+' else: return '' delta = b**2 - 4*a*c if delta > 0: x1 = "%.3f"%((-b+delta**0.5)/(2*a)) x2 = "%.3f"%(-b-delta**0.5)/(2*a) else : x1 = "x1=%.3f%s%.3fi"%(-b/(2*a),f((-delta)**0.5/(2*a)), (-delta)**0.5/(2*a)) x2 = "x2=%.3f%s%.3fi"%(-b/(2*a),f(-(-delta)**0.5/(2*a)), -(-delta)**0.5/(2*a)) print x1,x2
- 1
信息
- ID
- 2012
- 时间
- 1000ms
- 内存
- 128MiB
- 难度
- (无)
- 标签
- (无)
- 递交数
- 0
- 已通过
- 0
- 上传者