博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Project Euler Problem 9: Special Pythagorean triplet
阅读量:7223 次
发布时间:2019-06-29

本文共 979 字,大约阅读时间需要 3 分钟。

Problem 9

A Pythagorean triplet is a set of three natural numbers,a < b < c, for which,

a2 + b2 =c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for whicha + b + c = 1000.

Find the product abc.

C++:

#include 
using namespace std;int main(){ int n, c; // a + b + c = n // a^2 + b^2 = c^2 // a < b < c // a + b > c // a < n / 3 // b <= (n - a) / 2 // c = n - a - b while(cin >> n) { for(int a=1, maxa=n/3; a

C++(Truth):

#include 
using namespace std;int main(){ int s, c; while(cin >> s) { for(int a=3, maxa=(s-3)/3; a<=maxa; a++) { for(int b=a+1, maxb=(s-1-a)/2; b<=maxb; b++) { c = s - a - b; if(a + b <= c) continue; if(a * a + b * b == c * c) cout << a * b * c << endl; } } } return 0;}

转载于:https://www.cnblogs.com/tigerisland/p/7564032.html

你可能感兴趣的文章
Java串口通信详解
查看>>
Newtonsoft 自定义输出内容
查看>>
HTML图片元素(标记)
查看>>
windows server 2008 域控安装
查看>>
编写高质量代码:改善Java程序的151个建议(第1章:JAVA开发中通用的方法和准则___建议6~10)...
查看>>
Oracle查看和修改连接数(进程/会话/并发等等)
查看>>
【SpringMVC学习06】SpringMVC中的数据校验
查看>>
Laravel错误与日志处理
查看>>
微信小程序开发教程第七章:微信小程序编辑名片页面开发
查看>>
Java并发编程:Java ConcurrentModificationException异常原因和解决方法
查看>>
浅谈iOS中MVVM的架构设计
查看>>
node.js 中模块的循环调用问题详解
查看>>
ActiveReports 报表应用教程 (6)---分组报表
查看>>
OLEDB操作Excel
查看>>
struts2的json-default和struts-default的区别
查看>>
java中<> 的用法
查看>>
IIS 下配置无后缀的URL ReWrite
查看>>
对Asp.net Mvc 和 jQuery UI使用者的一些忠告
查看>>
Silverlight开发历程—动画(实现跑马灯效果)
查看>>
怎么说???
查看>>