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);

    }

このアプローチの利点は、何もバッファリングする必要がなく、動的メモリの割り当てが関係していないため、非常に単純ですが、もちろん計算は 2 回行われ、実際には受け入れられない可能性があります。\nまた、Basile が既に指摘したように、ほとんどの場合、printf の最後に配置する方がはるかに簡単であることに注意してください。

最初の代替手段はsprintf、結果文字列を、連結された結果文字列の最大長を含むのに十分な大きさであることが保証されている char[] 変数に strcat することです。そうすれば、計算を 1 回だけ実行できますが、N が大きくなると、そのメモリ構造は巨大な比率に成長する可能性があります。単純ではありますが、このアプローチは N の値がかなり小さい場合にのみ実際に実行可能です。

3 番目の方法は、個々の結果文字列をリンク リストに格納し、結果が見つかるたびにノードを割り当てることです。それらを印刷するには、リンクされたリストをたどって、すべてのノードを印刷するだけです。これは、リンクされたリストを実装するためのかなりの追加コードを犠牲にして、以前のソリューションの欠点を回避する最も効率的で洗練されたソリューションです。

于 2012-10-15T00:00:49.187 に答える
0

ラインを使わない

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

この行で、値を文字列型の変数に格納します。そして、この2行の後に

    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 をさらに 1 つ追加します。

于 2012-10-14T23:44:38.580 に答える