如何在另一行之前打印这一行?

时间:2012-10-14 23:39:01

标签: c loops printf

#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)内的每个Pythagorean三元组。  假设我输入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).

再次欢呼和谢谢!

2 个答案:

答案 0 :(得分:0)

不要使用

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

在此行中,将值存储在string类型的变量中。在这两行之后

    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 :(得分: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);

    }

这种方法的优点是不需要缓冲任何东西,没有涉及动态内存的分配,所以它很简单,但当然计算完成了两次,而且在现实生活中可能是不可接受的。另请注意,在大多数情况下,将\n放在printf的末尾会更容易,正如Basile已经指出的那样。

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

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