欧拉计划 第三十九题

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p ≤ 1000, is the number of solutions maximised?

如果p是具有整数长度边的直角三角形的周长{ abc },则对于p = 120 ,恰好有三个解。

{20,48,52},{24,45,51},{30,40,50}

对于其中的价值p ≤1000,解决方案的数量最多的是?

思路

1.素勾股数:如3,4,5 ,它们的整倍数也是素勾股数;

2.a,b,c之间两两互质;

3.a,b必为一奇一偶;

4.任何素勾股数均可以表示成如下形式,其中n<m,且gcd(n,m) = 1;

a = 2 * m * n;

b = m * m - n * n;

c = m * m + n * n;

5.把求出的个数放入keep数组中。

#include <stdio.h>

#define max 1000

int keep[max + 5] = {0};

int gcd(int a, int b){
	if(!b) return a;
	return gcd(b, a % b);
}

int add(int a, int b, int c){
	int ans = 0,p = a + b + c;
	for(int i = p; i <= max; i += p){
		keep[i] += 1;
		ans += 1;
	}
	return ans;
}

int main(){
	int a,b,c;
	for(int i = 1; i < 32; i++){
		for(int j = i + 1; j < 32; j++){
			if(gcd(i, j) != 1) continue;
			a = j * j - i * i;
			b = 2 * i * j;
			c = i * i + j * j;
			if(a + b + c <= max){
				add(a , b , c);
			}
		}
	}
	int sum = 0;
	for(int i = 1; i <= max; i++){
		if(keep[i] > sum) sum = i;
	}
	printf("%d\n", sum);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38362049/article/details/81002362