Project Euler 39 Integer right triangles( 素勾股数 )
2024-10-19 22:42:05
题意:若三边长 { 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;
}
最新文章
- foreach为什么要实现IEnumerable接口而不是直接用IEnumerator接口
- DataGridView导出到Excel的三个方法
- WPF的";路径标记语法";
- MAC下安装与配置MySQL
- .Net Core 控制台输出中文乱码
- South - 在 Django 中 Migrate Database
- javascript URI的编码
- cocos3 singleton
- Educational Codeforces Round 10 D. Nested Segments (树状数组)
- ACPI电源管理中的S0 S1 S2 S3 S4 S5
- Lucene之删除索引
- Java设计模式(八)观察者模式 迭代器模式
- tomcat-users.xml配置Manager登陆用户
- SQL多表关联查询
- (转)Spring Boot(二十):使用 spring-boot-admin 对 Spring Boot 服务进行监控
- Vue.js父与子组件之间传参
- Substr与mb_substr区别
- react 简单的用函数调出ui显示
- cf609E Minimum Spanning Tree For Each Edge (kruskal+倍增Lca)
- runtime.Gosched()的作用分析
热门文章
- ACDream - Crayon
- 使用 Redis及其产品定位
- gcc指定头文件路径及动态链接库路径
- &;#39;hibernate.dialect&;#39; must be set when no Connection available
- POJ 1166 The Clocks (暴搜)
- android的低内存管理器【转】
- oracle表类似:BIN$dJ5h8mA4Lr/gQAB/AQB0oA==$0 TABLE
- B1108 [POI2007]天然气管道Gaz 贪心
- js滚动
- Tomcat安全设置与优化详解(非原创)