<Sicily>Pythagorean Proposition
一、题目描述
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的更多相关文章
- sicily 中缀表达式转后缀表达式
题目描述 将中缀表达式(infix expression)转换为后缀表达式(postfix expression).假设中缀表达式中的操作数均以单个英文字母表示,且其中只包含左括号'(',右括号‘)’ ...
- sicily 1934. 移动小球
Description 你有一些小球,从左到右依次编号为1,2,3,...,n. 你可以执行两种指令(1或者2).其中, 1 X Y表示把小球X移动到小球Y的左边, 2 X Y表示把小球X移动到小球Y ...
- 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 ...
- Codeforces Round #368 (Div. 2) C. Pythagorean Triples(数学)
Pythagorean Triples 题目链接: http://codeforces.com/contest/707/problem/C Description Katya studies in a ...
- 大数求模 sicily 1020
Search
- Sicily 1510欢迎提出优化方案
这道题我觉得是除1000(A-B)外最简单的题了……不过还是提出一个小问题:在本机用gcc编译的时候我没包括string.h头文件,通过编译,为什么在sicily上却编译失败? 1510. Mispe ...
- Special Pythagorean triplet
这个比较简单,慢慢进入状态. A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2 + b2 = ...
- (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 ...
- projecteuler---->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 ...
随机推荐
- 关于 折半查找 while 条件 < , <=
int bin_search(int a[],int len,int key) { int low=0; int high=len-1; while(low<=high) //若为low< ...
- 【iOS开发-54】案例学习:通过UIScrollView的缩放图片功能练习代理模式的详细实现
案例:(在模拟器中按住option键,点击鼠标就会出现缩放的手势) (1)在ViewController.m中: --缩放东西是UIScrollView除了滚动之外的还有一个功能,所以须要缩放的东西应 ...
- GitBlit中出现 error: remote unpack failed: error Missing tree
clu@WASYGSHA01-1020 MINGW64 /d/ChuckLu/Git/Edenred/LISA_5.0.0.0 (local)$ git push origin preaction:p ...
- m-orchastration system
m-orchastration system 1.bootstrap上面有很多前台的页面代码可以用 2.H-ui里面的案例我可以去看看,这个网站也有后台框架 H-ui案例 - H-ui前端框架官方网站 ...
- ThinkPHP5.0框架开发--第10章 TP5.0验证器
ThinkPHP5.0框架开发--第10章 TP5.0验证器 第10章 TP5.0验证器 ======================================= 今日学习 1.验证器 1) 控 ...
- Java TreeMap 介绍和使用
转自:https://www.cnblogs.com/skywang12345/p/3310928.html 概要 这一章,我们对TreeMap进行学习. 我们先对TreeMap有个整体认识,然后再学 ...
- [雅礼NOIP2018集训 day1]
现在才来填坑,之后还要陆续补其他几天的,可能前几天真的太颓了 T1: 题目大意:给定一个长度为n的序列,m次询问每次询问给出l,r,询问区间l到r的元素在模k意义下的最大值 数据范围当然是你暴力写不过 ...
- 将hexo的评论系统由gitment改为Valine
title: 将hexo的评论系统由gitment改为Valine toc: false date: 2018-09-13 15:10:56 categories: methods tags: hex ...
- zookeeper客户端命令行操作
一.命令行 (1)使用zookeeper安装bin目录下的./zkCli.sh连接到zookeeper服务器上,基本语法如下: ./zkCli.sh -timeout 0 -r -server ip: ...
- PSSecurityException之PowerShell权限设置
Windows下PowerShell默认的权限级别是Restricted,不允许执行PS脚本(即.ps1文件).如果在Restricted权限级别下运行,会得到错误信息: .\XXXX.ps1 : F ...