题意:若三边长 { 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. SIGPROF和SIGALRM使用总结

    这几天,被公司的两个模块的程序好好的搞了一下,开始以为是SHELL的问题,仔细研究了以下,原来没有想象的那么复杂!!!       关键在使用的信号SIGALRM 上,两个进程都用可SIGALRM 信 ...

  2. 【ACM】hdu_zs2_1005_Problem E _201308030747

    Problem E Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)Total Subm ...

  3. 事务 ACID

    A C I D A:ATOMICITY REDO C:CONSTENCY UNDO I :ISOLATION LOCK D :Durable redo & undo

  4. Spring框架自学之路——简易入门

    目录 目录 介绍 Spring中的IoC操作 IoC入门案例 Spring的bean管理配置文件 Bean实例化的方式 Bean标签的常用属性 属性注入 使用有参构造函数注入属性 使用set方法注入属 ...

  5. [Java]对字符串中的每一个单词个数进行统计

    这是来自一道电面的题. 单词统计非常easy想到用Map来统计,于是想到了用HashMap. 可是我却没有想到用split来切割单词,想着用遍历字符的方式来推断空格.人家面试官就说了,假设单词之间不止 ...

  6. 系统报 “client没有所需的特权” 的解决方法

    今在对服务端代码进行单元測试的时候.突然报出例如以下错误: client没有所需的特权 后经网上查找,相同的问题都是属于对C盘读写的问题.回忆自己的项目也须要对C盘进行创建文件夹和读写文件.故尝试运行 ...

  7. Spring——概览

    Spring是什么? Spring是帮助开发者简化开发工作的工具. Spring的出现就是为了简化人们的复杂的开发.能够在不论什么Java应用中使用,使用了主要的JavaBean取代EJB. Spri ...

  8. luogu1896 [SCOI2005]互不侵犯 状压DP

    题目大意 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上左下右上右下八个方向上附近的各一个格子,共8个格子.( 1 <=N <=9, 0 ...

  9. beisen

    #include <stdio.h> #include <pthread.h> #include <windows.h> #define N 100 #define ...

  10. tensorflow利用预训练模型进行目标检测(四):检测中的精度问题以及evaluation

    一.tensorflow提供的evaluation Inference and evaluation on the Open Images dataset:https://github.com/ten ...