`
水木清华77
  • 浏览: 36268 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Problem9

阅读更多
package com.shui.mu.yao.io.algorithm;

import java.util.Arrays;

/**
 * 
 * @author shuimuqinghua77 @date 2011-11-3下午03:31:30
 */
/**
 * A Pythagorean triplet is a set of three natural numbers, a b c, for which, a2
 * + b2 = c2 For example, 32 + 42 = 9 + 16 = 25 = 52. There exists exactly one
 * Pythagorean triplet for which a + b + c = 1000. Find the product abc.
 */
public class Problem9 {
	public static int[] PythagoreanTriplet(int seed) {
		
		for (int i = 1; i < seed/2; i++)
			for (int j = i + 1; j < seed/2; j++)
				for (int k = j + 1; k < /** 2边之和大于第三边 */
				i + j && k < seed; k++) {
					if (k - i > j)
						continue;
					if (threeSqrt(i, j, k, seed))
						return new int[] { i, j, k };

				}
		return new int[3];
	}

	private static boolean threeSqrt(int x, int y, int z, int seed) {
		if (x + y + z == seed && x * x + y * y == z * z)
			return true;
		else
			return false;
	}

	public static void main(String[] args) {
		long start=System.currentTimeMillis();
		int[] triplet = PythagoreanTriplet(1000);
		System.out.println(Arrays.toString(triplet));
		System.out.println(triplet[0] * triplet[1] * triplet[2]);
		long end=System.currentTimeMillis();
		System.out.println(end-start);
		start=System.currentTimeMillis();
		triplet=MathMethod(1000);
		System.out.println(Arrays.toString(triplet));
		System.out.println(triplet[0] * triplet[1] * triplet[2]);
		end=System.currentTimeMillis();
		System.out.println(end-start);
}

	private static int[] MathMethod(int seed) {
		int a=0;
		for(int b=1;b<seed/2;b++){
			a=(seed*b-seed/2*seed)/(b-seed);
			if(a*(b-seed)==(seed*b-seed/2*seed)&&a<b)
				return new int[]{a,b,seed-a-b};
		}
		return new int[3];
	}

}
分享到:
评论

相关推荐

    numpy-numpy使用示例之problem9.zip

    numpy numpy_numpy使用示例之problem9

    Week1Problem9

    很抱歉,但根据您给出的信息,标题"Week1Problem9"和描述中并没有提供任何具体的IT知识点。标签为空,也无法提供额外的上下文信息。压缩包子文件的名称"Week1Problem9-master"通常可能是一个GitHub项目的结构,暗示...

    week1_problem9:创世纪MBD提交的第1周第9个问题,由唯一ID 2005546 Gauri M Kulkarni

    标题中的"week1_problem9"表明这是一个学习课程或者项目的一部分,可能是一个每周编程挑战或作业。唯一ID "2005546 Gauri M Kulkarni" 指明了这个问题的提交者,这可能是学生的名字和学号,用于追踪和评估个人的工作...

    爱立信CSR数据采集规范

    9. USSD related problem 7 10. Announcement problem 7 11. Echo canceller (ECP) problem 8 12. DTI problem 8 13. Group Switch problem 8 14. IN problem 9 15. ONE WAY SPEECH 9 16. CALL WAITING 9 17. ...

    Computer-Based.Problem.Solving.Process

    Chapter 9. Computer Process Manipulation by Programs Chapter 10. Memory Management System Chapter 11. I/O Device Management System Chapter 12. Computation Activity and Its Management Tools Part 4 ...

    Problem Solving with C++, 10th Global Edition

    Problem Solving with C++, Global Edition by Walter Savitch (author) (Author) Pages:1117 出版社: Pearson Education Limited; 10th edition edition (November 20, 2017) Language: English ISBN-10: ...

    Wicked Problem

    ### Wicked Problem与Wicked Environmental Problem #### 一、引言 "Wicked Problem"(棘手问题)这一概念最初由霍恩(Horst Rittel)和韦伯(Melvin Webber)于1973年提出,指的是那些复杂且难以解决的问题。这类...

    node-problem-detector-0.8.7.tar

    node-problem-detector 镜像包 v0.8.7 版本

    MCM 2012 problem A B C 论文

    MCM 2012年 problem A problem B problem C 枪手论文

    MySQL数据库考试试题.docx

    9. 获取最后一句 SELECT 查询到的总行数:FOUND_ROWS() 函数(Problem 9) FOUND_ROWS() 函数用于获取最后一句 SELECT 查询到的总行数。 10. 视图上的限制操作:不能定义新的表(Problem 10) 视图是一个基于表的...

    Problem Arrangement zoj 3777

    Problem Arrangement zoj 3777

    problem

    标题 "problem" 提供的信息较少,但从描述中的 "NULL 博文链接:https://eric0000.iteye.com/blog/322311" 可以推测,这可能是一个关于解决某个问题或者技术讨论的博客文章链接。由于没有具体的博文内容,我们无法...

    Problem Solving with C++(9th) 无水印pdf

    Problem Solving with C++(9th) 英文无水印pdf 第9版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请...

    算法设计taxi problem

    算法设计里关于taxi problem的C语言代码

    台大机器学习作业六1

    **Problem 9**:提到的正确率和错误预测可能与分类算法的性能评估相关,如二分类问题中的真正例、假正例、真负例和假负例的计算。 **Problem 10**:讨论了分类问题中决策边界的问题,涉及到超平面的选择及其影响...

    Problem_C_Data.zip

    "Problem_C_Data.zip" 是一个压缩包文件,包含2020年美国数学建模竞赛(简称美赛)C题的题目及相应的原始数据。美赛是一项国际性的数学建模竞赛,每年吸引众多学生参与,旨在提升参赛者的数学、数据分析和解决实际...

    fundamental of electric circuits

    - **电力传输系统**(Problem 9.93): 分析了电力传输系统的结构与优化策略。 - **Colpitts振荡器设计**(Problem 10.94): 探讨了振荡器的设计方法。 - **立体声音响电路**(Problem 13.85): 分析了音响电路的设计与优化...

    Problem 1002

    HDOJ Problem 1001 C++版

    master page search problem

    在这个特定的问题“master page search problem”中,我们可能遇到的是在使用Master Page时搜索功能出现的问题。ViewData是ASP.NET MVC框架中的一个特性,用于在控制器和视图之间传递数据,而JavaScript则是客户端...

    euler project.r.zip_R Euler project_project

    9. **Problem 9: Pythagorean triplet** - 知识点:勾股定理,循环结构,条件判断。 - 解决方法:遍历所有可能的三元组,检查是否满足勾股定理且三边平方和等于1000。 10. **Problem 10: 1到1000000的素数之和** ...

Global site tag (gtag.js) - Google Analytics