zoukankan      html  css  js  c++  java
  • projecteuler Problem 9 Special Pythagorean triplet

    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.

    译文:一个毕达哥拉斯三元数组是由三个自然数组成,a<b<c,形如

    举个例子,32 + 42 = 9 + 16 = 25 = 52.

    现在存在一个毕达哥拉斯三元数组,它满足 a + b + c = 1000.毕达哥拉斯三元数组的数值乘积。


    第一次code:

     1 public class Main
     2 { 
     3     public static void main(String[] args) 
     4     { 
     5         System.out.println(run(1000));
     6     }
     7     public static String run(int n)
     8     {
     9         String a="";
    10         for(int i=1;i<n;i++)
    11         {
    12             for(int j=0;j<i;j++)
    13             {
    14                 for(int s=0;s<j;s++)
    15                 {
    16                     if(s*s+j*j==i*i)
    17                     {
    18                          if(s+j+i == 1000)
    19                          {
    20                              a =String.valueOf(s*j*i);
    21                          }
    22                     }
    23                 }
    24             }
    25         }
    26         return a;
    27     }
    28 }

    时间效率:280毫秒。

  • 相关阅读:
    为上次写的框架加了一个辅助功能
    复制文件夹下所有文件
    进制之间的相互转换
    c# 修改appConfig文件节点
    GUID
    太悲哀了
    poj2411 Mondriaan's Dream
    poj3311 Hie with the Pie
    HDU3001 Travelling
    luogu p2622关灯问题II
  • 原文地址:https://www.cnblogs.com/niithub/p/5818394.html
Copyright © 2011-2022 走看看