acm课程练习2--1001

题目描述

Now,given the equation 8x^4 + 7x^3 + 2x^2 + 3x + 6 == Y,can you find its solution between 0 and 100;
Now please try your lucky.

Input

The first line of the input contains an integer T(1<=T<=100) which means the number of test cases. Then T lines follow, each line has a real number Y (fabs(Y) <= 1e10);

Output

For each test case, you should just output one real number(accurate up to 4 decimal places),which is the solution of the equation,or “No solution!”,if there is no solution for the equation between 0 and 100.

Sample Input

2
100
-4

Sample Output

1.6152
No solution!

大意

求解方程的简单水题

思路

简单的水题,使用二分法即可。但需要注意浮点数比较时的精度,我写的是1e-5,如果精度太高的话会超时的

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<cmath>
  4. #include<iomanip>
  5. using namespace std;
  6. double m, res = 0;
  7. int ji = 0;
  8. double f(double res)
  9. {
  10. return res*res*res*res * 8 + res*res*res * 7 + res*res * 2 + res * 3 + 6;
  11. }
  12. int main()
  13. {
  14. //cin.sync_with_stdio(false);
  15. //freopen("date.in", "r", stdin);
  16. //freopen("date.out", "w", stdout);
  17. int N, temp;
  18. double b, e, tem = 50;
  19. scanf("%d", &N);
  20. for (int i = 0; i<N; i++){
  21. b = 0, e = 100, tem = 50;
  22. cin >> m;
  23. if (m<6 || m>807020306)
  24. //cout << "No solution!" << endl;
  25. printf("No solution!\n");
  26. else{
  27. while (fabs(f(tem) - m) >= 1e-5)
  28. if (f(tem)>m){
  29. e = tem;
  30. tem = (b + e) / 2;
  31. }
  32. else{
  33. b = tem;
  34. tem = (b + e) / 2;
  35. }
  36. //cout << fixed << setprecision(4) << tem << endl;
  37. printf("%0.4f\n", tem);
  38. }
  39. }
  40. }
上一篇:Linux学习笔记(八)权限管理命令


下一篇:Linux学习笔记(一):常用命令