(Problem 39)Integer right triangles

If p is the perimeter of a right angle triangle with integral length sides, {a,b,c}, there are exactly three solutions for p = 120.

{20,48,52}, {24,45,51}, {30,40,50}

For which value of p ≤ 1000, is the number of solutions maximised?

题目大意:

如果p是一个直角三角形的周长,三角形的三边长{a,b,c}都是整数。对于p = 120一共有三组解:

{20,48,52}, {24,45,51}, {30,40,50}

对于1000以下的p中,哪一个能够产生最多的解?

#include <stdio.h>

int count(int p)
{
    int a, b, c, n, p1, p2;
    n = 0;
    p1 = p / 3;
    p2 = p / 2;
    for(a = 1; a < p1; a++) {
        for(b = p2 - a; b < p2; b++) {
            c = p - a - b;
            if(a * a + b * b == c * c) {
                n++;
            }
        }
    }    
    return n;
}

int main()
{
    int i, max, t, k;
    max = 3;
    for(i = 120; i < 1000; i++) {
        t = count(i);
        if(t > max) {
            max = t;
            k = i;
        }
    }
    printf("%d\n",k);
    return 0;
}
Answer:
840
posted @ 2014-01-07 02:39  acutus  阅读(458)  评论(0编辑  收藏  举报
TOP