写这道题真是蛋疼,一些细节上都没有处理好;WA了十几次,真是不应该呀!!!
刚开始WA的代码
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
//freopen("debug\\in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取
//freopen("debug\\out.txt","w",stdout);
int t;
cin>>t;
while(t--)
{
double low=0;
double end1=100;
int i;
double y;
cin>>y;
if(y<6||y>8*100000000+7*1000000+2*10000+3*100+6)//刚开始没有预处理,天哪,这么低级的错误我现在犯!。
cout<<"No solution!"<<endl;
else
{
for(i=1;i<=40;i++)
{
if(end1-low>0.0000001)//刚开始写的是(end1>=low)这样写其实是错的,因为题目指出了精度
{
double mid=(low+end1)/2;
if((8*mid*mid*mid*mid+7*mid*mid*mid+2*mid*mid+3*mid+6)>y)
end1=mid-0.00001;
else
low=mid+0.00001;
}
}
cout<<setiosflags(ios::fixed)<<setprecision(4)<<low<<endl;//应该输出的是end1
}
}
//fclose(stdin);//关闭文件
//fclose(stdout);//关闭文件
}
下面是AC的代码
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
//freopen("debug\\in.txt","r",stdin); //输入重定向,输入数据将从in.txt文件中读取
//freopen("debug\\out.txt","w",stdout);
int t;
cin>>t;
while(t--)
{
double low=0;
double end1=100;
int i;
double y;
cin>>y;
if(y<6||y>8*100000000+7*1000000+2*10000+3*100+6)
cout<<"No solution!"<<endl;
else
{
for(i=1;i<=100;i++)
{
if(end1-low>0.0000001)
{
double mid=(low+end1)/2;
if((8*mid*mid*mid*mid+7*mid*mid*mid+2*mid*mid+3*mid+6)>y)
end1=mid;
else
low=mid;
}
}
cout<<setiosflags(ios::fixed)<<setprecision(4)<<end1<<endl;
}
}
//fclose(stdin);//关闭文件
//fclose(stdout);//关闭文件
}
这道题就是考察人的仔细程度,自己太坑了。。。。。