0
#include <stdio.h>
#include <math.h>

int main(void)
{
///-------------------------------------------------------------------------------------------------------------------------------///
/// Initializes necessary variables. Description of each variable provided.
        int a, b, c; // Sides of triangle
        int N; // User-defined integer, where c<N
        int k=0; // Counter necessary for 'if loop'
        int thinA=0, thinB=0, thinC=0; // Memory for sides of 'thinnest' triangle
        double totalAngle = 180; // Sum of interior angles in a triangle
///-------------------------------------------------------------------------------------------------------------------------------///
/// Introduction
        printf("This program prints out all Pythagorean triples as (a,b,c) when given a positive integer, N, where c<N. \n\nThis program will also print out the number of triples and the 'thinnest' \n triangle in this range.\n\n");
///-------------------------------------------------------------------------------------------------------------------------------///
/// Requests user input for variable N. The program will then find all pythagorean triples that have side lengths less than N.
        printf("Enter a positive integer: ");
        scanf("%d", &N);
///-------------------------------------------------------------------------------------------------------------------------------///
/// Initilizes computing of side lengths, using several 'if' loops embedded within one another
        // Side A
        for (a=1; a<N; a++)
        {
            // Side B
            for (b=1; b<N; b++)
            {
                // Side C
                for(c=1; c<N; c++)
                {
                    // Validation of a right angle triangle. Also validates that side A is less than side B so no triangle is listed twice
                    if (a*a + b*b == c*c && a < b)
                    {
                        // Prints out listed side lengths of every acceptable triangle. Also increments counter for proper print statements at end
                        printf("\n(%d %d %d)", a, b, c);
                        k++;
///-------------------------------------------------------------------------------------------------------------------------------///
/// Determination of thinnest triangle
                        if (atan(a*1.0/b) < totalAngle)
                        {
                            totalAngle = atan(a*1.0/b);
                            thinA = a;
                            thinB = b;
                            thinC = c;
                        }
                    }
                }
            }
        }
///-------------------------------------------------------------------------------------------------------------------------------///
/// Results
        // If the counter incremented (that is, a triangle was found to exist where c<N), then it will print the amount of triangles found.
        // If not, it will state that no triangles were found.
        if (k > 0)
        {
            printf("\n\nThere are %d Pythagorean triples in this range.\n", k);
            printf("\nThe thinnest right-angle triangle is formed by (%d %d %d).\n\n", thinA, thinB, thinC);

        }
        else
            printf("\nThere are no pythagorean triples.\n\n");
///-------------------------------------------------------------------------------------------------------------------------------///
/// END OF SCRIPT
///-------------------------------------------------------------------------------------------------------------------------------///
    return 0;
}

晚上所有。我的代码接受用户定义的 int 变量 N 并输出范围 (0,N) 内的每个毕达哥拉斯三元组。假设我输入 N 作为 12,将打印以下内容:

Enter a positive integer: 12
(3 4 5) 
(6 8 10)
There are 2 Pythagorean triples in this range.
The thinnest right-angle triangle is formed by (3 4 5).

需要做哪些调整才能使打印顺序变成这样?

Enter a positive integer: 12 
There are 2 Pythagorean triples in this range.
(3 4 5)
(6 8 10)
The thinnest right-angle triangle is formed by (3 4 5).

再次欢呼和感谢!

4

2 回答 2

0

我看到了几种可能性,哪一种最好取决于 - 通常 - 取决于程序的其他方面:

到目前为止最简单的实现是首先计算 k,打印出数字,然后重做循环以打印出结果,像这样

    // pass 1: determine k
    for (a=1; a<N; a++)
    {
        // Side B
        for (b=1; b<N; b++)
        {
            // Side C
            for(c=1; c<N; c++)
            {
                // Validation of a right angle triangle. Also validates that side A is less than side B so no triangle is listed twice
                if (a*a + b*b == c*c && a < b)
                {
                    k++;
                }
            }
        }
   }
   if (k > 0) {
       printf("There are %d Pythagorean triples in this range.\n", k);
   } else {
       printf("There are no pythagorean triples.\n\n");
       // we're done
       return 0;
   }
   // pass 2 - print out the triples found and the thinnest
    for (a=1; a<N; a++)
    {
        // Side B
        for (b=1; b<N; b++)
        {
            // Side C
            for(c=1; c<N; c++)
            {
                // Validation of a right angle triangle. Also validates that side A is less than side B so no triangle is listed twice
                if (a*a + b*b == c*c && a < b)
                {
                    printf("(%d %d %d)\n", a, b, c);
                    if (atan(a*1.0/b) < totalAngle)
                    {
                        totalAngle = atan(a*1.0/b);
                        thinA = a;
                        thinB = b;
                        thinC = c;
                    }
                }
            }
        }
    }
    if (k > 0)
    {
        printf("The thinnest right-angle triangle is formed by (%d %d %d).\n\n", thinA, thinB, thinC);

    }

这种方法的优点是不需要缓冲任何东西,不涉及动态内存的分配,因此非常简单,但当然计算会进行两次,在现实生活中可能无法接受。另请注意,在大多数情况下,将 at 放在 printf 的末尾要容易\n得多,正如 Basile 已经指出的那样。

第一种选择是sprintf将结果字符串 strcat 到一个 char[] 变量中,该变量保证足够大以包含连接结果字符串的最大长度。这样你只执行一次计算,但随着 N 的增长,内存结构可能会增长到很大的比例。虽然简单,但这种方法仅适用于相当小的 N 值。

第三种选择是将各个结果字符串存储在链表中,每次找到结果时分配一个节点。将它们打印出来只是遍历链表,打印出每个节点。这是最有效和最优雅的解决方案,它避免了以前解决方案的缺点,但代价是需要相当多的额外代码来实现链表。

于 2012-10-15T00:00:49.187 回答
0

不要使用线路

    printf("\n(%d %d %d)", a, b, c);

在这一行中,将值存储在字符串类型的变量中。在这两条线之后

    printf("\n\nThere are %d Pythagorean triples in this range.\n", k);
    printf("\nThe thinnest right-angle triangle is formed by (%d %d %d).\n\n", thinA, thinB, thinC);

添加更多 printf 来打印创建的字符串变量。

于 2012-10-14T23:44:38.580 回答