Description
求ax2+bx+c=0方程的实根。a,b,c由键盘输入. 解方程要考虑系数a等于零的情况,且解x1、x2必须是float型。a等于零有两种情况(b==0,b!=0),a不等于零有三种情况(delta>0、==0、<0),先计算得到x1、x2,再printf输出
Input
输入三个数a,b,c
Output
输出方程的实根,如果方程有实根,则输出根;如果方程有2个不等实根,则分2行输出,第一行输出较大根,第二行输出较小根。 其余情况(如无实根等)则输出No
Sample Input
1 -3 2
Sample Output
2.000000
1.000000
HINT
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(void)
{
float a,b,c;
float data;
float x1,x2;
scanf("%f%f%f",&a,&b,&c);
if(a == 0)
{
if(b == 0)
printf("No\n");
else
{
x1 = -c / b;
printf("%f\n",x1);
}
}
else
{
data = b * b - 4 * a * c;
x1 = (-b + sqrt(data)) / (2 * a);
x2 = (-b - sqrt(data)) / (2 * a);
if(data < 0)
{
printf("No\n");
}
else
{
if(data == 0)
{
printf("%f\n",x1);
}
else
{
if(x1 > x2)
{
printf("%f\n",x1);
printf("%f\n",x2);
}
else
{
printf("%f\n",x2);
printf("%f\n",x1);
}
}
}
}
return 0;
}
#include<stdio.h>
double a,b,c,d,x[3];
double f(double x){
return (a*x*x*x + b*x*x + c*x + d);
}
int main(){
double f1,f2,f3,f4,high,mid,low;
int i,j = 0;
scanf("%lf %lf %lf %lf",&a,&b,&c,&d);
f1 = f(-100);f2=f(-99);
for(int i=-99;i <= 100;i ++){
double ans=f1*f2;
//printf("%.2lf %.2lf\n",f1,f2);
if(ans==0){
if(f1==0){
x[j ++] =i-1;
}
}else if(ans<0){
high = i+1; low = i;
do{
mid = (high + low) / 2;
f4 = f(low); f2 = f(high);
f3 = f(mid);
if(f4 * f3 <= 0) high = mid;
if(f2 * f3 <= 0) low = mid;
}while((high - low) < 0.0001);
x[j ++] = high;
}
f1=f(i);
f2=f(i+1);
}
printf("%.2lf %.2lf %.2lf\n",x[0],x[1],x[2]);
return 0;
}
https://www.luogu.org/problemnew/show/P1024
题解:
/*
*@Author: STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
#define endl "\n"
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=100000+10;
const int M=100000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,p,l,r,u,v;
int ans,cnt,flag,temp,sum;
double a,b,c,d,x[3];
double f(double x){
return (a*x*x*x + b*x*x + c*x + d);
}
int main()
{
#ifdef DEBUG
freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
#endif
//ios::sync_with_stdio(false);
//cin.tie(0);
//cout.tie(0);
//scanf("%d",&t);
//while(t--){
double f1,f2,f3,f4,high,mid,low;
int j = 0;
scanf("%lf %lf %lf %lf",&a,&b,&c,&d);
f1 = f(-100);f2=f(-99.5);
for(double i=-99.5;i <= 100;i +=0.5){
double ans=f1*f2;
if(ans==0){
if(f1==0){
x[j ++] =i-0.5;
}
}else if(ans<0){
high = i; low = i-0.5;
do{
mid = (high + low) / 2;
f2 = f(high);
f3 = f(mid);
f4 = f(low);
//printf("%.2f %.2f %.2f\n",f2,f3,f4);
if(f4 * f3 <= 0) high = mid;
if(f2 * f3 <= 0) low = mid;
}while((high - low) > 0.0001);
x[j ++] = high;
}
f1=f(i);
f2=f(i+0.5);
}
printf("%.2f %.2f %.2f\n",x[0],x[1],x[2]);
//}
#ifdef DEBUG
printf("Time cost : %lf s\n",(double)clock()/CLOCKS_PER_SEC);
#endif
//cout << "Hello world!" << endl;
return 0;
}