原题:

/*
 * 原题:
 * A Pythagorean triplet is a set of three natural numbers, a  b  c, for which,
 *
 * a^2 + b^2 = c^2
 *
 * 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^2 + b^2 = c^2
 *
 * 那请寻找这样一个毕达哥拉斯三角,满足:a+b+c=1000。并计算他们的乘积。
 */

解题思路:

 

1 /// <summary>   
2 /// 解题思路:   
3 ///     完全按照字面意思解题,唯一的问题就是运算次数上。   
4 /// </summary>   

 

代码:

代码
 1 using System;   
 2   
 3 class A_B_C   
 4 {   
 5     internal static int GetA_B_C()   
 6     {   
 7         bool flag = false;   
 8         int Result = 0;   
 9         for (int a = 1; a < 500; a++// 三边和等于1000,单边最大不会超过500。   
10         {   
11             int a_2 = a * a;   
12             for (int b = 1; b < 500; b++)   
13             {   
14                 int b_2 = b * b;   
15                 for (int c = 1; c < 500; c++)   
16                 {   
17                     int c_2 = c * c;   
18                     if ((a + b + c) == 1000)   
19                     {   
20                         if (a_2 + b_2 == c_2)   
21                         {   
22                             //Console.WriteLine(string.Format("The Result is {0}", a * b * c));   
23                             Result = a * b * c;   
24                             flag = true;   
25                             break;   
26                         }   
27                     }   
28                 }   
29   
30                 if (flag)   
31                     break;   
32             }   
33   
34             if (flag)   
35                 break;   
36         }   
37   
38         return Result;   
39     }   
40 
41 
42 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xbxbxb007/archive/2009/09/06/4525256.aspx

 

 

posted on 2010-05-18 10:48  干田  阅读(250)  评论(0编辑  收藏  举报