到目前为止,该程序会打印所有三元组并告诉您输入的数字是否没有三元组。在列出所有三元组后,我需要它再次打印具有最高 C 值的三元组。 此项目的示例 I/O
#include <stdio.h>
void main()
{
int a = 0, b = 0, c = 0, n;
int counter = 0; // counter for # of triples
printf("Please Enter a Positive Integer: \n"); //asks for number
scanf_s("%d", &n); //gets number
for (c = 0; c < n; c++) //for loops counting up to the number
{
for (b = 0; b < c; b++)
{
for (a = 0; a < b; a++)
{
if (a * a + b * b == c * c) //pythag to check if correct
{
printf("%d: \t%d %d %d\n", ++counter, a, b, c); //prints triples in an orderd list
}
else if (n < 6) // if less than 6 - no triples
{
printf("There is no pythagorean triple in this range");
}
}
}
}
getch(); //stops program from closing until you press a keys
}
如果我在 N 中输入 15,它将打印:
3 4 5
6 8 10
5 12 13
所以它打印的最后一个三元组总是具有最高的 C (5 12 13) 值,但我需要打印一个声明,说明特定的三元组具有最高值。