题意:若三边长 { a , b , c } 均为整数的直角三角形周长为 p ,当 p = 120 时,恰好存在三个不同的解:{ 20 , 48 , 52 } , { 24 , 45 , 51 } , { 30 , 40 , 50 }

在所有的p ≤ 1000中,p取何值时有解的数目最多?

思路:可以构建素勾股数,每构建成功一组素勾股数就用其生成其他勾股数,最后扫描一遍取最大值即可。

素勾股数性质:


/*************************************************************************
> File Name: euler039.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年06月29日 星期四 00时19分08秒
************************************************************************/ #include <stdio.h>
#include <inttypes.h> #define MAX_RANGE 1000 int32_t many[MAX_RANGE + 10] = {0}; int32_t gcd(int32_t a , int32_t b) {
return b == 0 ? a : gcd(b , b % a);
} void addMany(int32_t a , int32_t b , int32_t c) {
int32_t p = a + b + c;
for (int32_t k = p ; k <= MAX_RANGE ; k += p) {
many[k] += 1;
}
}
int32_t main() {
int32_t a , b , c , p;
for (int32_t i = 2 ; i * i < MAX_RANGE ; i++) { // 总感觉不思考就暴力的写上上界实在是!太蠢了!
for (int32_t j = 1 ; j < i ; j++) {
if (gcd(i , j) != 1) continue;
a = 2 * i * j;
b = i * i - j * j;
c = j * j + i * i;
p = a + b + c;
if (p > MAX_RANGE) continue;
addMany(a , b , c);
}
}
int32_t maxMany = 0 , ans = 0;
for (int32_t i = 1 ; i <= MAX_RANGE ; i++) {
if (maxMany < many[i]) {
maxMany = many[i] , ans = i;
}
}
printf("%d\n",ans);
return 0;
}

Project Euler 39 Integer right triangles( 素勾股数 )的更多相关文章

  1. 2018中国大学生程序设计竞赛 - 网络选拔赛 4 - Find Integer 【费马大定理+构造勾股数】

    Find Integer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  2. Project Euler:Problem 39 Integer right triangles

    If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exact ...

  3. Project Euler 91:Right triangles with integer coordinates 格点直角三角形

    Right triangles with integer coordinates The points P (x1, y1) and Q (x2, y2) are plotted at integer ...

  4. Project Euler 94:Almost equilateral triangles 几乎等边的三角形

    Almost equilateral triangles It is easily proved that no equilateral triangle exists with integral l ...

  5. (Problem 39)Integer right triangles

    If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exact ...

  6. hackerrank Project Euler #210: Obtuse Angled Triangles

    传送门 做出一个好几个星期屯下来的题目的感觉就是一个字: 爽! 上图的黄点部分就是我们需要求的点 两边的部分很好算 求圆的地方有一个优化,由于圆心是整数点,我们可以把圆分为下面几个部分,阴影部分最难算 ...

  7. 笔试题-求小于等于N的数中有多少组素勾股数

    题目描述: 一组勾股数满足:a2+b2=c2: 素勾股数:a,b,c彼此互质. 输入正整数N: 输出小于等于N的数中有多少组勾股数. 例: 输入:10 输出:1 思路:我是直接暴力破解的…… 首先找出 ...

  8. Project Euler 9

    题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...

  9. Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.

    In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...

随机推荐

  1. @Service注解的作用

    @Service注解作用    1. 其getBean的默认名称是类名(头字母小写),可以@Service(“xxxx”)这样来指定, 2.其定义的bean默认是单例的,可以使用@Service(“b ...

  2. Spring MVC-Hello World示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_hello_world_example.htm 说明:示例基于Spring MVC ...

  3. Java枚举类型使用示例

    Java枚举类型使用示例 学习了:https://www.cnblogs.com/zhaoyanjun/p/5659811.html http://blog.csdn.net/qq_27093465/ ...

  4. HDU1846(巴什博奕)

    Brave Game Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  5. 关于PHP浮点数之 intval((0.1+0.7)*10) 为什么是7

    PHP是一种弱类型语言, 这样的特性, 必然要求有无缝透明的隐式类型转换, PHP内部使用zval来保存任意类型的数值, zval的结构如下(5.2为例): struct _zval_struct { ...

  6. 初探boost之noncopyable学习笔记

    noncopyable 功能 同意程序轻松实现一个不可复制的类. 需包括头文件 #include<boost/noncopyable.hpp>     或 #include<boos ...

  7. 64位只有一种调用约定stdcall

    procedure TForm2.Button1Click(Sender: TObject); function EnumWindowsProc(Ahwnd: hwnd; AlParam: lPara ...

  8. ubantu下NiFi单节点安装部署

    第一步,首先下载安装包:http://nifi.apache.org/download.html,博主下载的是1.4.0版本,直接下载的是编译后的文件. 第二步:将压缩包上传到服务器相应目录下,并且解 ...

  9. Gerapy 使用详解

    https://blog.csdn.net/fengltxx/article/details/79894839

  10. swiper套路

    swiper插件 quick start 基本结构 <div class="swiper-container"> <div class="swiper- ...