输入为一个整数,即为圆半径的平方,范围在32位int范围内。
输出为一个整数,即为优雅的点的个数
25
12
import java.util.*; //把平面图形用公式转成一维思想 public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); int r2 = sc.nextInt(); double r = Math.sqrt(r2); int res = 0; for(int i = 0; i < r; i++){ double t = Math.sqrt(r2-i*i); if((int)t == t) res++; } System.out.println(res*4); sc.close(); } }
import java.util.Scanner;
public class Main {
public static void main(String args[]) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int flag=0;
double b=Math.sqrt(n);
for (int i=0;i<b;i++){
double j= Math.sqrt(n-ii);
if ((int)j==j)
flag++;
}
System.out.println(flag4);
}
}
import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
double r=Math.sqrt(n);
int count=0;
for(int i=-(int)r;i<=(int)r;i++) {
for(int j=-(int)r;j<=(int)r;j++) {
if(i*i+j*j==n) {
count++;
}else {
continue;
}
}
}
System.out.print(count);
}
}
AC百分之八十,算法复杂度太高
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); System.out.println(elegant(n)); } public static int elegant(int n) { int cnt = 0; int r = (int) Math.pow(n, 0.5); for (int i = 1;i <= r;i ++) { int m = n - i * i; if (Math.pow(m, 0.5) == Math.ceil(Math.pow(m, 0.5))) { cnt ++; } } return cnt * 4; } }
import java.util.HashSet; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while(in.hasNext()){ int radius = in.nextInt(); System.out.println(solution_2(radius)); } } private static int solution(int radius) { int max = (int)Math.sqrt(radius); HashSet<Integer> hs = new HashSet<Integer>(); int loop = max; //将0至max的平方加入hs。 while(loop>=0){ hs.add(loop*loop); loop--; } int result = 0; //问题:0,5 这种情况,只用乘2,而请他情况要乘4,怎么解决? //从1开始检测,不从0,因为如果检测到(max,0)是符合的,那么直接乘4,就包含了(0,max)的情况。 //即只需判断(0,max)这一对是否满足,然后乘4即可,不用判断(max,0)。 //而像(3,4)和(4,3)这种,就必须都算上然后乘4。 for(int i=1; i<=max; i++){ int tmp = radius - i*i; if(hs.contains(tmp)){ result++; } } return result*4; } }
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main{
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int num = Integer.parseInt(br.readLine());
int sqnum = (int) Math.sqrt(num);
int count = 0;
if (sqnum * sqnum == num){
count = 4;
}
for (int i = 1; i <= sqnum; i++) {
for (int j = 1; j <= sqnum; j++) {
if (i * i + j * j == num) {
count += 4;
break;
}
}
}
System.out.println(count);
}
}
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc=new Scanner(System.in); int r2=sc.nextInt(); Set<Integer> set=new HashSet<>(); int count=0; for(int i=0;i*i<=r2;i++){ if(set.contains(r2-i*i)){ if(i*i==0||r2-i*i==0)count+=4; if(i!=0&&r2-i*i!=0)count+=8; } if(r2==2*i*i)count+=4; set.add(i*i); } System.out.println(count); } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { int num = in.nextInt(); int countTotal = 0; int countFlag = 0; int start = (int)Math.ceil(Math.sqrt(num/2)); //向上取整 int end = (int)Math.floor(Math.sqrt(num)); for (int i = start; i <= end; i++) { double temp = Math.sqrt(num - i*i); double ceilTemp = Math.ceil(temp); double floorTemp = Math.floor(temp); if (ceilTemp - floorTemp <= 0.000000001) countTotal++; if (i*i*2 == num) countFlag++; } if (Math.ceil(Math.sqrt(num)) == Math.floor(Math.sqrt(num))) countFlag++; System.out.println((countTotal-countFlag)*8 + countFlag*4); } } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main{ //优雅的点 83 9000多呢 public static int n = 0; public static void main(String[] args) throws IOException{ BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); n = Integer.valueOf(bf.readLine()); int max = (int) Math.sqrt(n); int sum = 0; for(int i=-max; i<=max; i++){ sum += add(i); } System.out.println(sum); bf.close(); } public static int add(int i){ double p = Math.sqrt(n-i*i); double q = Math.ceil(p); if((int)p != (int)q) return 0; if(n == i*i) return 1; return 2; } }
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println(method(sc.nextLong()));
}
public static int method(long r){
int count = 0;
int limit = (int)Math.sqrt(r / 2);
for(int i = 1; i < limit + 1; i++){
long res = r - i * i;
double tmp = Math.sqrt(res);
if(tmp - (int)tmp == 0){
if(i - (int)tmp == 0){
count += 4;
}
else{
count += 8;
}
}
}
double r1 = Math.sqrt(r);
if(r1 - (int)r1 == 0){
return count + 4;
}
else{
return count;
}
}
}