背景:一直WA,一直以为是double精度问题,结果是:在运算过程中,超出int数据范围。
思路:我的思路是分类讨论,网上好的思路是ceil(d/2r)即可。
学习:
1.数据类型的范围:int 2.1的10次方
longlong 9.2的19次方(longlong在 linux下为%lld,windows下为%I64d).
2.这类数学类型的题,思考就好,不要畏惧!
#include<stdio.h>
#include<math.h>
int main(void){
long long int r,x1,y1,x2,y2;
while(scanf("%I64d%I64d%I64d%I64d%I64d",&r,&x1,&y1,&x2,&y2)!=EOF){
double xx=(double)((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
double temp=sqrt(xx)-(double)r*2.0;
int count=0;
while(1) {
if((temp-(double)r*2.0) >= 0){
temp-=(double)r*2.0;
count++;
}
else break;
}
if(sqrt(xx) <= (double)r*2.0){
if(xx==0.0) printf("0\n");
else printf("1\n");
}else if(temp == 0.0){
printf("%d\n",count+1);
}else{
printf("%d\n",count+2);
}
}
return 0;
}
原文:http://blog.csdn.net/jibancanyang/article/details/43191863