Project Euler 39 Integer right triangles( 素勾股数 )
题意:若三边长 { 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( 素勾股数 )的更多相关文章
- 2018中国大学生程序设计竞赛 - 网络选拔赛 4 - Find Integer 【费马大定理+构造勾股数】
Find Integer Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- 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 ...
- 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 ...
- Project Euler 94:Almost equilateral triangles 几乎等边的三角形
Almost equilateral triangles It is easily proved that no equilateral triangle exists with integral l ...
- (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 ...
- hackerrank Project Euler #210: Obtuse Angled Triangles
传送门 做出一个好几个星期屯下来的题目的感觉就是一个字: 爽! 上图的黄点部分就是我们需要求的点 两边的部分很好算 求圆的地方有一个优化,由于圆心是整数点,我们可以把圆分为下面几个部分,阴影部分最难算 ...
- 笔试题-求小于等于N的数中有多少组素勾股数
题目描述: 一组勾股数满足:a2+b2=c2: 素勾股数:a,b,c彼此互质. 输入正整数N: 输出小于等于N的数中有多少组勾股数. 例: 输入:10 输出:1 思路:我是直接暴力破解的…… 首先找出 ...
- Project Euler 9
题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...
- 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 ...
随机推荐
- 1013MySQL监控利器-Innotop
转自 http://www.cnblogs.com/ivictor/p/5101506.html 安装过程中 可以使用 YUM INSTALL INNOTOP进行直接安装 Innotop是一款十分强大 ...
- 0419如何利用关系角度看待SQL
转自http://www.open-open.com/solution/view/1389339225820 十步完全理解SQL 1. SQL 是一种声明式语言 首先要把这个概念记在脑中:“声明” ...
- 【ACM】hdu_zs2_1004_Problem D _201308030856
Problem D Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)Total Subm ...
- C#使用PowerShell 操作Exchange
先介绍一篇文章来参考一下 点开文章 该文章的最后使用了SSL来保证账户在连接服务器的时候不发生账户认证错误,但是我经过测试发现这个是不可行的,有一种更为简单的方法 首先要对服务器进行winrm设置 就 ...
- owin--Authentication
下面的这个从httpcontext取出来的GetOwinContext的Authentication认证有很多关于cookie和session的方法,好像不实用owin也能取出来 private IA ...
- codevs——T1043 方格取数
http://codevs.cn/problem/1043/ 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 De ...
- 不错的题目-n个数连接得到的最大值
这道题目还是很不错的 <[字符串排序]n个数连接得到最小或最大的多位整数> 题目 描述:设有n个正整数,将它们依次连成在一排,组成一个多位数,现在要求可能组成的多位数中最大的多位数是什么? ...
- HDU 4518
整理一下思路,明天再写... 这道题,其实就是求包含大于10的斐波那切数字的第K(K是斐波那契数)个数.注意到斐波那契数的爆炸性增长,所以在范围 内的符合要求的F数并不多吧.比如求第K个F数,那么,前 ...
- PHP扩展开发-测验成功
原文:http://kimi.it/496.html http://blog.csdn.net/u011957758/article/details/72234075 ---------------- ...
- Nginx 源码安装和调优
常见web架构: LAMP =Linux+Apache+Mysql+PHP LNMP =Linux+Nginx+Mysql+PHP nginx概述: 知道:1 不知道:2 Nginx (&q ...