1Can you solve this equation
来源:互联网 发布:php html实体编码转换 编辑:程序博客网 时间:2024/05/17 10:53
简单题意
给出公式,与结果,在0到100之间找出代入公式可以等于给定的值
解题思路形成过程
老师刚讲完二分算法肯定是用二分算法啦,要是用普通方法肯定会超时不断用二分法逼近函数值
感想
虽然百般小心,但还是超时了好几次,不断改细节终于AC了
AC代码
#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <fstream>
#include <vector>
#include <cmath>
using namespace std;
double f(double x){
return (8*pow(x,4)+7*pow(x,3)+2*pow(x,2)+3*x+6);
}
int main(){
//ifstream cin("in.txt");
freopen("in.txt","r",stdin);
double low,high;
double m;
double y;
int t;
scanf("%d",&t);//cin>>t;
while(t--){
scanf("%lf",&y);//cin>>y;
if(f(0)>y||f(100)<y){
printf("No solution!\n");//cout<<"No solution!"<<endl;
continue;
}
low=0.0;high=100.0;m=50.0;
while(fabs(f(m)-y)>1e-5){
if(f(m)<y){
low=m;
m=(low+high)/2;
}else if(f(m)>y){
high=m;
m=(low+high)/2;
}
}
printf("%.4lf\n",m);//cout<<setiosflags(ios::fixed)<<setprecision(4)<<m+0.00005<<endl;
}
return 0;
}
- 1Can you solve this equation
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- Can you solve this equation?
- linux高级编程基础系列:线程间通信
- App工程结构搭建:几种常见Android代码架构分析
- msketrans
- tomcat类加载机制
- 程序思维
- 1Can you solve this equation
- 吴向宏:当前中国经济形势是无解的&未来两年经济走向与全球资产配置
- 【Raspberry Pi系列】1. 启动与进入树莓派
- Python学习笔记(二)list和tuple
- 81. Search in Rotated Sorted Array II
- Matlab调用C语言编写MEX文件
- iOS UIButton左字右图 + 选择
- 【8】Linux-进程管理子系统
- 前端编码工具利器