迅雷后端笔试 素勾股数

 /*迅雷后端笔试 素勾股数*/
#include<stdio.h>
#include<math.h>

int gcd(int x, int y)
{
return (y>0) ? gcd(y, x%y) : x;
}

int main()
{
int N, n, m, num = 0;
scanf_s("%d", &N);
for (int n = 1; n <= sqrt(N - 1); n += 2)
{
for (int m = n + 2; n*n + m * m <= 2 * N; m += 2)
{
if (gcd(n, m) == 1)
num++;
}
}
printf("%d", num);
return 0;
}

#笔试题目##迅雷#
全部评论
我用的穷举法,还没敲完。。
1 回复 分享
发布于 2018-09-12 20:57
大佬啊...第二题道编程题完全没看懂啊
点赞 回复 分享
发布于 2018-09-12 20:41
贴个Java的 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int N = scanner.nextInt(); int res = 0; for(int i = 1; i<= N -1;i++) { for(int j = i ; j<= N-1;j++) { if(!huZhi(i,j)) { continue; } for(int k = j; k <=N; k++) { if(k*k - i*i - j*j > 0) { break; } else if(k*k - i*i - j*j < 0) { continue; } if(!huZhi(i, k) || !huZhi(j, k)) { break; } res++; } } } System.out.println(res); } public static boolean huZhi(int a, int b) { while(a!=0) { int temp = b; b = a; a = temp%a; } if(b == 1) { return true; } else { return false; } } }
点赞 回复 分享
发布于 2018-09-12 20:49
// 不知道对不对 最后没时间改了 就A了43% 这是交卷之后改的 import java.util.Scanner; public class Main {     public static void main(String[] args) {         // TODO Auto-generated method stub         Scanner sc = new Scanner(System.in);         int N = sc.nextInt();         int ans = 0;         for (int a = 1; a <= N; a++) {             for (int b = 1; a >= b && b <= N; b++) {                 for (int c = 1; c <= N; c++) {                     if ((a * a + b * b == c * c) && (c <= N)) {                         if (zhishu(a, b) && zhishu(a, c) && zhishu(b, c)) {                             ans++;                         }                     }                 }             }         }         System.out.println(ans);     }     public static boolean zhishu(int a, int b) {         if (a < b) {             int tmp = a;             a = b;             b = tmp;         }         int c;         while ((c = a % b) != 0) {             a = b;             b = c;         }         return b == 1;     } }
点赞 回复 分享
发布于 2018-09-12 20:59
赛码的gcc版本太低了,居然没有***函数,虽然自己写一个非递归版本也不过两分钟的事,但还是差评😒
点赞 回复 分享
发布于 2018-09-12 21:03

相关推荐

评论
2
收藏
分享
牛客网
牛客企业服务