当前位置:网站首页>由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。
由于3²+4²=5²,所以称‘3,4,5‘为勾股数,求n(包括n)以内所有勾股数数组。
2022-04-23 03:02:00 【Roc-xb】
由于3²+4²=5²,所以称'3,4,5'为勾股数,求n(包括n)以内所有勾股数数组。
比如:10以内的勾股数组:['3,4,5','6,7,8']
目录
一、题目分析
我们都学过勾股定理,知道勾股定理需要满足a²+b²=c²,我们可以把等式两边拆开来处理,首先
遍历一次1~n,存储c的平方结果集,然后通过双层for循环计算a²+b²,最后判断a²+b²的和是否在c的平方结果集当中,如果在,就说明,a,b,c满足勾股数关系。
二、程序代码
package com;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
int n = 10;
List<Integer> zList = new ArrayList<>();
for (int i = 1; i <= n; i++) {
zList.add(i * i);
}
List<String> result = new ArrayList<>();
for (int x = 1; x < n; x++) {
for (int y = x; y < n; y++) {
int z = x * x + y * y;
if (zList.contains(z)) {
result.add(String.format("'%d,%d,%d'", x, y, (int) Math.sqrt(z)));
}
}
}
System.out.println(result);
}
}
三、运行结果

版权声明
本文为[Roc-xb]所创,转载请带上原文链接,感谢
https://yang-roc.blog.csdn.net/article/details/124355810
边栏推荐
- Judge whether there is a leap year in the given year
- Winsock programming interface experiment: Ping
- Publish to NPM?
- JS learning notes
- The difference between encodeuri and encodeuricomponent
- Assembly learning Chapter III of assembly language (Third Edition) written by Wang Shuang
- Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮
- Blazor University (12)组件 — 组件生命周期
- 腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
- tf. keras. layers. Density function
猜你喜欢

Huawei machine test question -- deformation of hj53 Yang Hui triangle

TP5 inherits base and uses the variables in base

Kubernetes study notes

Encapsulate components such as pull-down menu based on ele

Cherno_ Game engine series tutorial (5): 101~

Response processing of openfeign

Dynamic sequence table + OJ

AOT和单文件发布对程序性能的影响

ASP.NET 6 中间件系列 - 自定义中间件类

【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持
随机推荐
Chapter IV project cost management of information system project manager summary
It turns out that PID was born in the struggle between Lao wangtou and Lao sky
Restart redis
基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目
微软是如何解决 PC 端程序多开问题的
Kubernetes - Introduction to actual combat
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
MYSQL05_ Ordr by sorting, limit grouping, group by grouping
Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (7)
对.NET未来的一点感悟
Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
tf. keras. layers. Timedistributed function
Service avalanche effect
如果通过 C# 实现对象的深复制 ?
HLS / chisel uses CORDIC hyperbolic system to realize square root calculation
Openfeign service call
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
Shell script learning notes - regular expressions
Winsock programming interface experiment: Ping