江西财经大学第一届程序设计竞赛 F题 -解方程

题目链接https://www.nowcoder.com/acm/contest/115/F

解题思路:我们可以先求出y在它所给的定义域中y的值域。如果存在解。我们可以有两种方法

1、二分求解。

2、逐个遍历,x从0遍历到100,每次增加0.0001;找到哪个解最接近y。

ps:注意要结果输出要换行,因为这个找了半天的bug

 1 /*
 2 暴力做的
 3 */
 4 #include<iostream>
 5 #include<bits/stdc++.h>
 6 #define Abs 0.0001
 7 using namespace std;
 8 int main(){
 9     int T;
10     cin>>T;
11     while(T--){
12         double y;
13         cin>>y;
14         double x=0.0001;
15         int flag=0;
16         double x1=0.0000;
17         for(int i=0;i<1000000;i++){
18             double s,s1;
19             s=2018.0000*x*x*x*x+21.0000*x+5.0000*x*x*x+5.0000*x*x +14.0000;
20             s1=2018.0000*x1*x1*x1*x1+21.0000*x1+5.0000*x1*x1*x1+5.0000*x1*x1 +14.0000;
21             if(s>y&&s1<y){
22                 double ans;
23                 if(abs(s1-y)>abs(s-y)){
24                     ans=x;
25                 }else{
26                     ans=x1;
27                 }
28                 flag=1;
29                 printf("%.4lf
",ans);
30             }
31             x1=x;
32             x=x+0.0001;
33         }
34         if(!flag) cout<<-1<<endl;
35     }
36   
37     return 0;
38 }
原文地址:https://www.cnblogs.com/ISGuXing/p/8907206.html