zoukankan      html  css  js  c++  java
  • 洛谷 [P1024]一元三次方程求解

    一道水题然而坑点很多。

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstdlib>
    using namespace std;
    int read(){
    	int rv=0,fh=1;
    	char c=getchar();
    	while(c<'0'||c>'9'){
    		if(c=='-') fh=-1;
    		c=getchar();
    	}
    	while(c>='0'&&c<='9'){
    		rv=(rv<<1)+(rv<<3)+c-'0';
    		c=getchar();
    	}
    	return fh*rv;
    }
    double a,b,c,d;
    double f(double x){
    	return a*x*x*x+b*x*x+c*x+d;
    }
    int main(){
    	freopen("in.txt","r",stdin);
    	scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
    	for(int i=-100;i<=100;i++){
    		double x=f((double)i),y=f((double)(i+1));//一定要强制类型转换
    		if(x==0){
    			printf("%.2lf ",(double)i);
    		}else if(y==0){
    			printf("%.2lf ",(double)(i+1));
    			i++;
    		}else if(x*y<0){
    			double l=(double)i,r=(double)(i+1),m;
    			while(r-l>=0.0001){
    				m=(r+l)/2;
    				double t=f(m);
    				if(t==0){
    					printf("%.2lf ",m);
    					break;
    				}
    				if(f(m)*f(l)<0){
    					r=m;
    				}else l=m;
    			}
    			printf("%.2lf ",r);
    		}
    	}
    	fclose(stdin);
    	return 0;
    }
    
    

    从本题中学到了什么:

    1>整数与实数的转换,千万不要相信编译器的水平,最好强制手动转换。
    2>实数的二分写法。

    盛金公式##

    百度。。

  • 相关阅读:
    my first android test
    VVVVVVVVVV
    my first android test
    my first android test
    my first android test
    ini文件
    ZZZZ
    Standard Exception Classes in Python 1.5
    Python Module of the Week Python Module of the Week
    my first android test
  • 原文地址:https://www.cnblogs.com/Mr-WolframsMgcBox/p/7868369.html
Copyright © 2011-2022 走看看