<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 ...
随机推荐
- bzoj1019: [SHOI2008]汉诺塔(动态规划)
1019: [SHOI2008]汉诺塔 题目:传送门 简要题意: 和经典的汉诺塔问题区别不大,但是题目规定了一个移动时的优先级: 如果当前要从A柱子移动,但是A到C的优先级比A到B的优先级大的话,那就 ...
- Java TreeMap 介绍和使用
转自:https://www.cnblogs.com/skywang12345/p/3310928.html 概要 这一章,我们对TreeMap进行学习. 我们先对TreeMap有个整体认识,然后再学 ...
- 制作可以SSH的Docker容器
以 Ubuntu 16.04为例: Docker里的root密码是随机的, 用passwd来设置新的密码 安装完SSH_SERVER后, 默认是不能用root登录的. vi /etc/ssh/sshd ...
- MyBatis数据持久化(一)准备工作
MyBatis简介 mybatis的前生是ibatis,它是一款非常优秀的java持久层框架,所有sql语句写在配置文件中,和另外一款比较知名的orm框架hibernate比起来显得更加小巧灵活,也是 ...
- (转载)打开一个本地apk进行安装
1 2 3 4 5 6 Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); File file = new File ...
- jQuery学习(一)——jQuery入门
1.jQuery基础 Jquery它是一个库(框架),要想使用它,必须先引入! jquery-1.8.3.js:一般用于学习阶段. jquery-1.8.3.min.js:用于项目使用阶段 官网下载后 ...
- 优动漫PAINT-紫藤花画法
本教程分享一篇使用优动漫PAINT绘制一树梦幻的紫藤萝花教程,原文转载自优动漫官网. 小清新紫藤萝教程,就到这里啦!有兴趣的可以尝试画一画哦,软件下载:www.dongmansoft.com/xiaz ...
- Vim常用命令及配置方案
Vim常用命令及配置方案 几句话 很久之前就接触到vim,初学那阵觉得vim很酷炫,但确实对新手不是很友好.我也就简单看了下基本操作就上手了,但又不是长期在vim下工作,这就导致了每一次重新使用v ...
- 视图层 view
视图层是 Django 处理请求的核心代码层,我们大多数 Python 代码都集中在这一层面.它对外接收用户请求,对内调度模型层和模版层,统合数据库和前端,最后根据业务逻辑,将处理好的数据,与前端结合 ...
- IPv6理论知识详解
1. IPv6地址表示 IPv6地址可以表示为128位由0.1组成的字符串,为了便于计算机理解,将128位的二进制字符串表示为32位的十六进制字符串,为了便于理解,人们将其划分为8组,组与组之间用 : ...