zznuoj 2168 大家好 我是水题

http://47.93.249.116/problem.php?id=2168

求一元三次函数的递增区间 y=ax^3+bx^2+cx+d;

只需要统计系数a b c   求导之后成为一元二次函数,只要是一元二次函数大于零就是一元三次函数的递增区间。即求根。3a  2b  c  这三者之间的作用。

利用求根公式x=-b-+sqrtb*b-4ac/(2a)求出相应的解,根据a的正负来解决

#include<algorithm>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<numeric>
using namespace std;
#define LL long long
#define N  1006
int Q[N],falg;
char str[N];
void prime(char str[])
{
    int len=strlen(str);
    int d=1,e=0,f=0,w=0;
    for(int i=0; i<len; i++)
    {
        if(str[i]>='0'&&str[i]<='9')
        {
            if(!w) e=e*10+str[i]-'0';
            else f=f*10+str[i]-'0';
        }
        else if(str[i]=='x')
            w=1;
        else if(str[i]=='+'||str[i]=='-')
        {
            if(w&&!f) f=1;
            if(e==0&&w) e=1;
            Q[f]+=d*e;
            e=f=w=0;
            if(str[i]=='-') d=-1;
            else d=1;
        }
    }
    if(!e) e=1;
    if(w&&!f) f=1;
   Q[f]+=e*d;
}
int main()
{
    ///freopen("1.txt","w",stdout);
    while(scanf("%s",str)!=EOF)
    {
        falg=0;
        memset(Q,0,sizeof(Q));
        prime(str+2);///提取系数
        int a=3*Q[3];
        int b=2*Q[2];
        int c=Q[1];
        if(b*b-4*a*c<=0)
        {
            if(a>0) printf("(-oo,+oo)\n");
            else printf("ooo\n");
            continue;
        }
        double x1=(-b-sqrt(1.0*b*b-4.0*a*c))/2/a;
        double x2=(-b+sqrt(1.0*b*b-4.0*a*c))/2/a;
        if(x1>x2) swap(x1,x2);///设置存放位置
        if(fabs(x1)<1e-8) x1=0.00;///避免出现-0.00的现象
        if(fabs(x2)<1e-8) x2=0.00;
        if(a>0) printf("(-oo,%.2f) (%.2f,+oo)\n",x1,x2);
        else printf("(%.2f,%.2f)\n",x1,x2);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/a719525932/p/9498290.html