Jiangxi University of Finance and Economics 1st Programming Contest F Question - Solving Equations

Topic link https://www.nowcoder.com/acm/contest/115/F

Problem-solving idea: We can first find the value range of y in the domain it gives. if there is a solution. We can have two ways

1. Dichotomous solution.

2. Traverse one by one, x is traversed from 0 to 100, increasing by 0.0001 each time; find which solution is closest to y.

ps: Note that the output of the result must be wrapped, because this bug has been searched for a long time

 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\n",ans);
30             }
31             x1=x;
32             x=x+0.0001;
33         }
34         if(!flag) cout<<-1<<endl;
35     }
36   
37     return 0;
38 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324622518&siteId=291194637