题意:若三边长 { 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;
}

最新文章

  1. foreach为什么要实现IEnumerable接口而不是直接用IEnumerator接口
  2. DataGridView导出到Excel的三个方法
  3. WPF的&quot;路径标记语法&quot;
  4. MAC下安装与配置MySQL
  5. .Net Core 控制台输出中文乱码
  6. South - 在 Django 中 Migrate Database
  7. javascript URI的编码
  8. cocos3 singleton
  9. Educational Codeforces Round 10 D. Nested Segments (树状数组)
  10. ACPI电源管理中的S0 S1 S2 S3 S4 S5
  11. Lucene之删除索引
  12. Java设计模式(八)观察者模式 迭代器模式
  13. tomcat-users.xml配置Manager登陆用户
  14. SQL多表关联查询
  15. (转)Spring Boot(二十):使用 spring-boot-admin 对 Spring Boot 服务进行监控
  16. Vue.js父与子组件之间传参
  17. Substr与mb_substr区别
  18. react 简单的用函数调出ui显示
  19. cf609E Minimum Spanning Tree For Each Edge (kruskal+倍增Lca)
  20. runtime.Gosched()的作用分析

热门文章

  1. ACDream - Crayon
  2. 使用 Redis及其产品定位
  3. gcc指定头文件路径及动态链接库路径
  4. &amp;#39;hibernate.dialect&amp;#39; must be set when no Connection available
  5. POJ 1166 The Clocks (暴搜)
  6. android的低内存管理器【转】
  7. oracle表类似:BIN$dJ5h8mA4Lr/gQAB/AQB0oA==$0 TABLE
  8. B1108 [POI2007]天然气管道Gaz 贪心
  9. js滚动
  10. Tomcat安全设置与优化详解(非原创)