double 与0比较时有个精度问题,有时需精确到小数点后面几位,例如与>0.0001,而不能与>0比较
例如杭电1408
盐水的故事
ac
#include<iostream>
using namespace std;
int main()
{
double vul,d;
int sum;
int n;
while(cin>>vul>>d)
{
sum=0;
n=1;
while(vul>d*n)
{
sum=sum+n+1;
vul=vul-d*n;
n++;
}
while(vul>0.001)//此处最小精度得>0.001,不能为while(vul>0)
{
vul=vul-d;
sum++;
}
cout<<sum<<endl;
}
return 0;
}
盐水的故事
ac
#include<iostream>
using namespace std;
int main()
{
double vul,d;
int sum;
int n;
while(cin>>vul>>d)
{
sum=0;
n=1;
while(vul>d*n)
{
sum=sum+n+1;
vul=vul-d*n;
n++;
}
int k=vul/d;
sum=sum+k;
if((vul/d)-k < 0.000001) ;
else
sum=sum+1;
/*while(vul>0)
{
vul=vul-d;
sum++;
}*/
cout<<sum<<endl;
}
return 0;
}