输入为一个整数,即为圆半径的平方,范围在32位int范围内。
输出为一个整数,即为优雅的点的个数
25
12
#include<iostream>
#include<cmath>
using namespace std;
int main() {
int n;
cin>>n;
int count = 0;
int r = (int)sqrt(n);
if (r*r == n) {
count += 4;
--r;
}
for (int i = r; i > 0; i--) {
int x = (int)sqrt(n - i*i);
if(x*x + i*i == n) count += 4;
}
cout<<count<<endl;
}
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int n = scan.nextInt(); System.out.println(count(n)); } public static int count(int n) { int count = 0; double r = Math.sqrt(n); for (int i = 0; i < r; i++) { double j = Math.sqrt(n - i * i); if (Math.abs(j - Math.round(j)) <= 0.000000001) { count++; } } return 4 * count; } }
import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); int rSquare = in.nextInt(); int count =0; double r = Math.sqrt(rSquare); //存储值 for(int i=0;i<r;i++){ /*运行超时 for(int j=1;j<r+1;j++){ if(i*i+j*j==rSquare){ count++; } } */ //优化点1 double j = Math.sqrt(rSquare-i*i); if((int) j==j){ count++; } } //优化点2 System.out.print(count<<2); } }
//选第一象限,判断i是整数的时候,j是不是整数就行了 //此处把double强制转换为int,如果有小数点,那么转换后就是变小了 //需要注意的是,double仅能在一定范围内(正负2^53)精确的表示整数 //如果超过这个范围,就不是精确的,此时用 == 比较是不正确的 //本题输入是int,所以可以这么做
#include <iostream> #include <math.h> using namespace std; int main(){ int n = 0; while(cin >> n){ int res = 0; for( int i = 0; i < sqrt(n); i++ ){ double j = sqrt(n - i*i); if( (int)j >= j ) res++; } cout<<4*res<<endl; } }
from math import sqrt def nCouple(N): r=int(sqrt(N)) count=0 for i in range(r+1): j=sqrt(N-i**2) if j in range(r+1) : if i==0 or j==0: count+=2 else: count+=4 return count if __name__=='__main__': n=int(input()) print(nCouple(n))
from math import sqrt
def countGracePoint(number):
squareSet = set()
for i in range(int(sqrt(number)) + 1):
squareSet.add(i ** 2)
res = 0
for i in range(int(sqrt(number)) + 1):
if number - i ** 2 in squareSet:
if i != 0 and number - i ** 2 != 0:
res += 4
else:
res += 2
return res
a = int(input())
print(countGracePoint(a))
importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStreamReader;publicclassMain {publicstaticvoidmain(String[] args) throwsNumberFormatException, IOException {// TODO Auto-generated method stubBufferedReader read = newBufferedReader(newInputStreamReader(System.in));doubler2 = Integer.parseInt(read.readLine());doubler = Math.sqrt(r2);f(r2);}publicstaticvoidf(doubler2) {intcount = 0;for(intx = 0; x < Math.sqrt(r2); x++) {doubley = Math.sqrt(r2 - x * x);if((int) y == y) {count += 4;}}System.out.print(count);}}
// 判断是否为整数,使用double类型保证精度bool isInt(double y){
double a = y - (int)y;
if(a==0)
return 1;
else
return 0;
}
void elegantPoint()
{
int rSquare;
cin >> rSquare;
int result = 0;
double r = sqrt(rSquare);// 求第一象限满足要求的点,乘以4得到所有象限满足要求的点的个数for(int x=1; x*x<rSquare; x++)
{
double y = sqrt(rSquare-x*x);
if(isInt(y))
{
result+=4;
}
}// 判断X轴和y轴上的点是否满足整数条件,若满足则加上4个点if(isInt(r))
{
result+=4;
}
cout << result;
}
bool isInt(double x) {double a = x - (int) x;
if(a==0) return 1;
else return 0;
}
int main(){
int radius;
int cnt = 0;;
while(cin >> radius){
for(int i=1; i<= sqrt(radius); i++){
if (isInt(sqrt(radius - i*i)))
cnt++;
}
cout << 4*cnt << endl;
}
return 0;}一开始用float类型通不过,换成double就可以了。原来题目上写了32位精度,不够细心。
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; import java.util.HashSet; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int r2 = Integer.parseInt(br.readLine()); double r = Math.sqrt(r2); // 半径 HashSet<Integer> set = new HashSet<>(); int count = ((int)r)*((int)r) == r2? 4: 0; for(int x = 0; x <= (int)r; x++) set.add(x*x); // 先将0~r中整数的平方存储起来便于之后查找 for(int x = 1; x < r; x++) if(set.contains(r2 - x*x)) count += 4; // 满足勾股数,计数自增4 System.out.println(count); } }
import java.util.Scanner; public class Main11 { public static void main(String[] args) { Scanner sr =new Scanner(System.in); while (sr.hasNext()){ int res=0; int c=sr.nextInt(); int cc=(int)Math.sqrt(c); for (int i=cc;i>0;i--){ if(Math.pow(c-Math.pow(i,2),0.5)%1==0){//a²=c²-b²,对a²开根,为整数时res++ res++; } } System.out.println(res*4); } } }