一、题目描述

One day, WXYZ got a wooden stick, he wanted to split it into three sticks and make a right-angled triangle. You will be given the length of the stick, and your task is to help WXYZ to find all different right triangles that can be made.

二、输入

The first line of the input is a positive integer T. T is the number of test cases followed. Each test case contains a integer L (1<=L<=1000), representing the length of the stick.

三、输出

For each test case, output all different right-angled triangles. For each triangle, output one line containing three integers X Y Z, (1<=X<=Y<=Z, X*X+Y*Y=Z*Z). All triangles are sorted in lexicographical order.

Output a blank line after each test case.

例如:

输入:

3

40

50

60

输出:

8 15 17

10 24 26

15 20 25

四、解题思路

这道题相对比较水,主要是从1开始遍历两条直角边的平方和是否等于第三边平方。

总边长为:perimeter

假设最短那条直角边shortHEdge初始值为1;

长的那条直角边longHEdge初始值为1/2perimeter(直角边比斜边短)

斜边longLEdge=perimeter-shortHEdge-longHEdge

判断两直角边的平方和quadraticSum跟斜边平方比。

如果quadraticSum>longLEdge^2,长的直角边-1;

如果quadraticSum

五、代码

#include<iostream>

using namespace std;

int main()
{
int times;
cin >> times;
while(times--)
{
int perimeter, shortHEdge, longHEdge, longLEdge;
long long quadraticSum;
cin >> perimeter; shortHEdge = 1; //短的直角边
longHEdge = perimeter / 2; //长的直角边 while(shortHEdge <= longHEdge) //长的直角边要保持大于等于短的直角边
{
longLEdge = perimeter - longHEdge - shortHEdge; //斜边长度 quadraticSum = (shortHEdge * shortHEdge) + (longHEdge * longHEdge); //两"直角"边和
if(quadraticSum < longLEdge * longLEdge) {shortHEdge++;} //两"直角"边和小于斜边
else if(quadraticSum > longLEdge * longLEdge) {longHEdge--;} //两"直角"边和大于斜边
else {cout << shortHEdge << " " << longHEdge << " " << longLEdge << endl; shortHEdge++;} //两直角边和等于第三边
}
cout << endl;
} return 0;
}

<Sicily>Pythagorean Proposition的更多相关文章

  1. sicily 中缀表达式转后缀表达式

    题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...

  2. sicily 1934. 移动小球

    Description 你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2).其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y ...

  3. projecteuler Problem 9 Special Pythagorean triplet

    A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2 Fo ...

  4. Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)

    Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...

  5. 大数求模 sicily 1020

        Search

  6. Sicily 1510欢迎提出优化方案

    这道题我觉得是除1000(A-B)外最简单的题了……不过还是提出一个小问题:在本机用gcc编译的时候我没包括string.h头文件,通过编译,为什么在sicily上却编译失败? 1510. Mispe ...

  7. Special Pythagorean triplet

    这个比较简单,慢慢进入状态. A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2 + b2 = ...

  8. (Problem 9)Special Pythagorean triplet

    A Pythagorean triplet is a set of three natural numbers, a  b  c, for which, a2 + b2 = c2 For exampl ...

  9. projecteuler----&gt;problem=9----Special Pythagorean triplet

    title: A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2 + b2 = c2 For e ...

随机推荐

  1. [SICP] 求值规则

    在Java语言学习中,通常不太关注求值规则. (2+4*6)*(3+5+7)这样的组合式的求值规则.通常归结为优先级问题: if.for等的求值规则通常归结为语义. 函数式编程语言的Scheme,将这 ...

  2. hdu_4707

    算是水题一道吧,我也没有建树,看别人又用vector,又用bfs,dfs的,对vector不熟,所以就模拟了一下 #include<iostream> #include<string ...

  3. 线上服务CPU100%问题快速定位实战--转

    来自微信公众号 架构师之路 功能问题,通过日志,单步调试相对比较好定位. 性能问题,例如线上服务器CPU100%,如何找到相关服务,如何定位问题代码,更考验技术人的功底. 58到家架构部,运维部,58 ...

  4. [转]Adobe CC 2018 下载链接 Creative Cloud 2018 - Creative Cloud 2018 – Adobe CC 2018 Download Links

    Creative Cloud 2018 – Adobe CC 2018 Download Links – ALL Languages Adobe CC 2018Direct Downloads Win ...

  5. CSS弹性盒模型flex概念

    盒模型分为:标准w3c盒模型.IE盒模型.以及css中的伸缩盒模型. 先说CSS的伸缩盒模型:flex模型是CSS3引入的新的布局模型,是flexible box的缩写,一般称之为弹性盒模型.和CSS ...

  6. eclipse01

    http://blog.csdn.net/luman1991/article/category/6354903

  7. 1028C:Rectangles

    You are given n rectangles on a plane with coordinates of their bottom left and upper right points. ...

  8. 使用rman恢复数据小结

    恢复前提有数据备份 以 alter database open resetlogs 开机以后多要做一次全备(以前的备份失效了) 恢复参数文件: restore spfile from '/home/o ...

  9. AJAX和JSON实际应用

    实现功能:登录验证 一.因为我是在SpringMVC框架上写的,首先得添加依赖: <dependencies> <!-- 用来测试的依赖 --> <dependency& ...

  10. mysql 查询格式化时间

    select DATE_FORMAT(addtime,'$m %d %Y') from tablename 输出:01 28 2019 数据库时间格式:2019-01-28 15:01:20