当前位置:网站首页>由于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
边栏推荐
- Plug in for vscode
- .Net Core 限流控制-AspNetCoreRateLimit
- 腾讯视频涨价:一年多赚74亿!关注我领取腾讯VIP会员,周卡低至7元
- LNMP MySQL allows remote access
- 一套关于 内存对齐 的C#面试题,做错的人很多!
- C# 11 对 ref 和 struct 的改进
- 《信息系統項目管理師總結》第六章 項目人力資源管理
- Distributed system services
- Chapter VII project communication management of information system project manager summary
- Gavl021, gavl281, AC220V to 5v200ma small volume non isolated chip scheme
猜你喜欢
Openfeign timeout setting
Er and eer models
Niuke white moon race 6 [solution]
最通俗易懂的依赖注入之服务容器与作用域
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
C# 读写二进制文件
Configuring Apache Web services for servers such as Tianyi cloud
FileNotFoundError: [Errno 2] No such file or directory
Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮
Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)
随机推荐
Golden nine silver ten interview season, you are welcome to take away the interview questions (with detailed answer analysis)
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
Basic SQL (VIII) data update operation practice
Openfeign details show
tf. keras. layers. Conv? D function
Q-Learning & Sarsa
Redis data server / database / cache (2022)
Blazor University (12)组件 — 组件生命周期
Notes sur le développement de la tarte aux framboises (XII): commencer à étudier la suite UNO - 220 de la tarte aux framboises de contrôle industriel advantech (i): Introduction et fonctionnement du s
FileNotFoundError: [Errno 2] No such file or directory
Résumé du gestionnaire de projet du système d'information Chapitre VI gestion des ressources humaines du projet
最通俗易懂的依赖注入之服务容器与作用域
Xamarin效果第二十二篇之录音效果
tf. keras. layers. Inputlayer function
《信息系统项目管理师总结》第四章 项目成本管理
使用两种方法来解决“最大回文数乘积”问题
TP5 where query one field is not equal to multiple values
L2-006 樹的遍曆(中後序確定二叉樹&層序遍曆)
ASP.NET 6 中间件系列 - 执行顺序
TP5 inherits base and uses the variables in base