hdu2199 二分搜索

news/2024/7/3 5:46:49

这是很简单的二分搜索;需要注意的是精度。一开始我用的是right-left<1e-4,测试样例中100我输出的最后一位为1,不合要求。

后来自己写了个四舍五入的一段,但交上去WA,于是改成1e-6,AC了。

代码题目如下:

Problem Description
Now,given the equation 8*x^4 + 7*x^3 + 2*x^2 + 3*x + 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!
代码:
My code
 1 #include<stdio.h>
 2 #include<math.h>
 3 double equo(double a);
 4 int y,num;
 5 int main()
 6 {
 7     double left=0.0,right=100.0,mid=0;
 8     int ok=0,s;
 9     double an1,an2,an3,t;
10     scanf("%d",&num);
11     while(num--)
12     {
13         left=0.0;
14         right=100.0;
15         scanf("%d",&y);
16         an1=equo(left);
17         an2=equo(right);
18         while((an2-an1)>1e-6)
19         {
20             if(y<6||y>807020288)
21             {ok=1;break;}
22             mid=(left+right)/2;
23             an3=equo(mid);
24             if(an3*an1<0)
25             {
26                 an2=an3;
27                 right=mid;
28             }
29             else
30             {
31                 left=mid;
32                 an1=an3;
33             }
34         }
35         if((mid<0&&mid>100)||ok==1)printf("No solution!\n");
36          else 
37                  printf("%.4f\n",mid);
38     }
39     return 0;
40 }
41 double equo(double a)
42 {
43     double b;
44     b=8*pow(a,4)+7*pow(a,3)+2*pow(a,2)+3*a+6-y;
45     return b;
46 }

 

转载于:https://www.cnblogs.com/gongcomeon/archive/2012/04/25/hdu2199.html


http://www.niftyadmin.cn/n/2606775.html

相关文章

实用的设计模式1——单例模式

在软件工程中&#xff0c;设计模式&#xff08; design pattern &#xff09;是对软件设计中普遍存在&#xff08;反复出现&#xff09;的各种问题&#xff0c;所提出的解决方案。 看维基上对设计模式的定义&#xff0c;你就知道设计模式的重要性&#xff0c;但是往往编程中设计…

一条502报警引发的胡思乱想

安心倒计时 忙完了今天的工作, 终于到了周五,可以好好休息下了。 睡梦惊醒 就在安心养神的时候, 同事转给了我一条nginx 502的报警, 赶紧去线上一顿排查。 首先得先找出哪台机器报出的(同时喊运维看下线上负载情况), 发现01机器的nginx日志在报警时间点的错误信息: *272881176 …

工作一年了的心得体会

工作至今有一年的时间了&#xff0c;这一年有过开心&#xff0c;也有失落。但是回首看看&#xff0c;似乎更多的是迷茫。 刚来公司的时候&#xff0c;打心底想好好学一学技术&#xff0c;学一点有用的东西。但是进公司之后发现很多和自己想的不太一样。来到了一个做监控平台的组…

Zookeeper学习(一) 概述

0. 前言 前段时间在工作中参与了一个分布式项目的开发&#xff0c;其中一个重要的模块就是Zookeeper。可以说这个项目及其之后的一段学习让我找到了自己的兴趣点&#xff0c;自己最近也学习了一些Zookeeper的知识&#xff0c;在这里也把自己学到的和一些思考写下来~ 1. 分布式协…

[转]React Native 语言基础之ES6

React Native 是基于 React 这个前端框架来构建native app的架构。React Native基于ES6&#xff08;即ECMAScript2015&#xff09;语言进行开发的。 JS的组成 1) 核心&#xff08;ECMAScript&#xff09;&#xff1a;描述了该语言的语法和基本对象。担当的是一个翻译的角色&am…

canvas 实现vue 手势解锁组件

1.手机锁屏九宫格解锁组件 2.代码如下 <template><canvas id"gesture" ref"canvas" :style"style" /></template><script>export default {name: GestureLock,props: {chooseType: {type: Number,default: 3 // 3: 3*3,…

Scala - 快速学习06 - 面向对象

1- 类 1.1- 简介&#xff1a;类、方法及对象 类是用来创建对象的蓝图。Scala文件中包含的多个类之间&#xff0c;都是彼此可见的&#xff0c;不需要声明为public。创建对象定义好类以后&#xff0c;就可以使用new关键字来创建对象。字段默认为public&#xff0c;在类外部和内部…

设计模式-创建型模式-建造者模式

设计模式-创建型模式-建造者模式建造者模式即生成器模式&#xff0c;将一个复杂的构建与它的表示分离&#xff0c;使得同样的构建过程可以创建不同的表示。 代码如下 // 产品类 public class Product{public void doSomething(){// 业务处理} } // 抽象建造者 public abstract …