#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).
再次欢呼和感谢!