当前位置:网站首页>由于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
边栏推荐
- The space between the left and right of the movie ticket seats is empty and cannot be selected
- How to deploy a website with only a server and no domain name?
- .Net Core 限流控制-AspNetCoreRateLimit
- Blazor University (12)组件 — 组件生命周期
- The shell monitors the depth of the IBM MQ queue and scans it three times in 10s. When the depth value exceeds 5 for more than two times, the queue name and depth value are output.
- HLS / chisel uses CORDIC hyperbolic system to realize square root calculation
- Processes and threads
- Gavl021, gavl281, AC220V to 5v200ma small volume non isolated chip scheme
- Thoughts on the 2022 national network security competition of the national secondary vocational group (only one idea for myself) - network security competition questions (8)
- MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator
猜你喜欢

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

Judge whether there is a leap year in the given year

Vs code setting line feed

Cherno_ Game engine series tutorial (5): 101~

HLS / chisel practice CORDIC high performance computing complex square root

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

Summary of interface automation interview questions for software testing

Xamarin效果第二十一篇之GIS中可扩展浮动操作按钮

Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system

Dynamic sequence table + OJ
随机推荐
Response processing of openfeign
MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
.NET7之MiniAPI(特别篇):.NET7 Preview3
基于.NetCore开发博客项目 StarBlog - (2) 环境准备和创建项目
C# 读写二进制文件
Microservices (distributed architecture)
Realize QQ login with PHP
Summary of software test interview questions
Development notes of raspberry pie (12): start Advantech industrial control raspberry pie uno-220 Kit (I): introduction and operation of the system
树莓派开发笔记(十二):入手研华ADVANTECH工控树莓派UNO-220套件(一):介绍和运行系统
Laravel's own paging query
How to count the number of all files in a directory under win10 system
c#语法糖模式匹配【switch 表达式】
如果通过 C# 实现对象的深复制 ?
Niuke white moon race 6 [solution]
JS relearning
Use of MySQL command line client and common commands
Small companies don't make formal offers
Kubernetes - Introduction to actual combat
Opencv combines multiple pictures into video