#include <stdio.h> #include <math.h> #include <vector> using namespace std; float d(float a, float b, float h){ return sqrt(a*a+b*b+h*h); } bool isPerfectSquare(int n){ int h = n & 0xf; if (h > 9) return false; if (h != 2 && h != 3 && h != 5 && h != 6 && h != 7 && h != 8){ int t = (int)floor(sqrt((double)n) + 0.5 ); return t*t == n; } return false; } int main(void){ int n, n_square; int a,b,h, tmp, tmp_1; int count = 0; scanf("%d", &n); n_square = n * n; for(a = 1; a <= n; a++){ for(b = a; b <= n; b++){ tmp = a*a+b*b; if(tmp>n_square) continue; for(h = 1; h <= n; h++){ tmp_1 = tmp+h*h; if(isPerfectSquare(tmp_1) && tmp_1<=n_square) { count++; } } } } printf("%d\n", count); return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | #include <stdio.h> #include <math.h> #include <vector> using namespace std; float d(float a, float b, float h){ return sqrt(a*a+b*b+h*h); } bool isPerfectSquare(int n){ int h = n & 0xf; if (h > 9) return false; if (h != 2 && h != 3 && h != 5 && h != 6 && h != 7 && h != 8){ int t = (int)floor(sqrt((double)n) + 0.5 ); return t*t == n; } return false; } int main(void){ int n, n_square; int a,b,h, tmp, tmp_1; int count = 0; scanf("%d", &n); n_square = n * n; for(a = 1; a <= n; a++){ for(b = a; b <= n; b++){ tmp = a*a+b*b; if(tmp>n_square) continue; for(h = 1; h <= n; h++){ tmp_1 = tmp+h*h; if(isPerfectSquare(tmp_1) && tmp_1<=n_square) { count++; } } } } printf("%d\n", count); return 0; } |