一、题目描述

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. 具体解释XML解析(一)—解析接口浅析

    在DRP项目中接触到了一个XML解析工具DOM4J,它作为解析工具的据说性能很优秀.可是刚刚接触解析工具不久,而且也没有使用过其它的解析工具.因此对于DOM4J的性能没有直接的感受(没有參照物).只是 ...

  2. 个人andriod实习小作品,个人联网笔记本

    个人联网笔记本 个人信息:就读于燕大本科软件project专业 眼下大四; 本人博客:google搜索"cqs_2012"就可以; 个人爱好:酷爱数据结构和算法,希望将来从事算法工 ...

  3. hdoj--1010--Tempter of the Bone(搜索+奇偶剪枝)

    Tempter of the Bone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

  4. ora-01157怎么解决

    在数据库startup时,出现以下两个错误:ora-01157:cannot identify/lock data file 8 -see DBWR trace fileora-01110:data ...

  5. MEF基本概念

    基本概念: Managed Extensibility Framework 或 MEF 是一个用于创建可扩展的轻型应用程序的库,在.NET 4.0发布 Container:容器,使用Compositi ...

  6. 使用JS方法使页面滚动到指定元素+优化+API介绍(动画)

    前言 当页面最上部有顶部菜单是,使用锚点跳转的方法很容易挡住想要呈现的内容(如下图技能两个字被挡住了一半),为避免出现这样的问题,故滚动到指定元素使用用JS的方法来实现. 目录 使用的API简介 初版 ...

  7. jQuery学习(五)——使用JQ完成复选框的全选和全不选

    1.在系统后台进行人员管理时,进行批量删除,使用jq完成全选和全不选 步骤分析: 第一步:引入jquery文件 第二步:书写页面加载函数 第三步:为上面的复选框绑定单击事件 第四步:将下面所有的复选框 ...

  8. servlet中Session的用法

    ## (1)什么是Session? 服务器端为了保存用户的状态而创建的一个特殊的对象(即session对象).          当浏览器第一次访问服务器时,服务器会创建session对象(该    ...

  9. confluence6.0.3安装文档

    一.Atlassian Confluence 6.0.3安装文档包含内容 1.wiki的安装步骤: 2.旧系统迁移中碰到的无法编辑和问题和解决方案: 3.wiki源码安装包.连接mysql用的jar包 ...

  10. POJ 3093 Margaritas on the River Walk(背包)

    题意 n个有体积的物品,问选取一些物品,且不能再继续选有多少方法? n<=1000 题解 以前的考试题.当时是A了,但发现是数据水,POJ上WA了. 把体积从小到大排序枚举没选的物品中体积最小的 ...