编写C语言程序:给定一个rSquare,表示一个圆的半径的平方。某元的圆心在坐标系原点,计算返回处在该圆圆周上的格点
2 回答

慕粉2350305356
TA贡献6条经验 获得超4个赞
#include<stdio.h>
#include<math.h>
int main()
{
float rSquare;
int x,y,temp,res,count = 0;
scanf("%f",&rSquare);
x=(int)sqrt(rSquare);
y=(int)sqrt(rSquare);
for(int i=0;i<=x;i++)
{
for (int j = 0; j <=y; j++)
{
if(i*i+j*j==rSquare)
count++;
}
}
if(sqrt(rSquare)==(long)sqrt(rSquare))
{
temp = count-2;
res =temp*4+4;
printf("%d",res);
}
else
{
res = count*4;
printf("%d",res);
}
return 0;
}
/* 又一个被开发者猩球残害的新同事 */

朱利尔1124
TA贡献1条经验 获得超0个赞
Java版本:
public static int countPoints(int rSquare) { if(rSquare<=2000000000&&rSquare>=1) { int x,y,temp,res,count = 0; x=(int)Math.sqrt(rSquare); y=(int)Math.sqrt(rSquare); for(int i=0;i<=x;i++){ for (int j = 0; j <=y; j++){ if(i*i+j*j==rSquare) { count++; } } } if(Math.sqrt(rSquare)==(long)Math.sqrt(rSquare)){ temp = count-2; res =temp*4+4; System.out.println("格点数为" + res); return res; }else{ res = count*4; System.out.println("格点数为" + res); return res; } }else { System.out.println("请输入1~2000000000之间的数"); return -1; } }
- 2 回答
- 0 关注
- 3931 浏览
添加回答
举报
0/150
提交
取消