题目描述
假设圆的圆心位于(0,0),半径为r,圆的"某积"公式为S=r2,请问"某积"为s的圆上有多少个以整数为坐标的点?
输入
多组测试数据,请处理到文件结束。
对于每组测试数据,只包含一个整数S。
1<=S<=2,000,000,000。
输出
输出一个整数,代表以整数为坐标的点的个数。
样例输入
253
样例输出
120
提示
总结:
这道题总体思路很清晰,只要遍历x从0到r内的所有整数,再计算对应点y,如果y是整数,那么这个点就在圆上,否则不在,看似简单,但是这种会出现浮点数的等于什么的处理一直是我的弱点,这道题正好补上了这个点
#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;
const double ex=1e-8;
int main()
{
//freopen("input.txt","r",stdin);
int s;
while(scanf("%d",&s)!=EOF)
{
int counter=0;
double b=sqrt(s);
for(int i=1;i<b||abs(b-i)<ex;i++)
{
double temp=sqrt(s-i*i);
int y=temp;
if(abs(temp-y)<ex)
{
if(abs(b-i)<ex) counter++;
else counter+=2;
}
}
counter=2*counter;
int temp=b;
if(abs(temp-b)<ex) counter+=2;
printf("%d\n",counter);
}
return 0;
}