开发者星球c语言给定一个,求助,急急急

这段Java代码计算了给定平方数值内格点的数量。它首先检查输入范围是否在1到2000000000之间,然后通过两个嵌套循环找出平方根范围内所有点对(i, j),当i²+j²等于输入的平方时,计数增加。最后,根据平方根是否为整数调整结果并返回。
摘要由CSDN通过智能技术生成

dfe6f3a343de1edc3a46997b6291c233.png

朱利尔1124

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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值