Write a program that finds all triples of consecutive positive three-digit integers each of which is
the sum of two squares.
Hint: As we are not constrained by memory space for this problem, and as we have some experience
with integers but not with floating point numbers, we might declare an array that can store an int
for all numbers n in the range [100, 999], equal to 1 in case n is the sum of two squares, and to 0
otherwise.
Do not forget to use #define to give evocative names to special values (in particular, the minimum
and maximum values that a variable can take in a loop).
solution:
#include <stdio.h>
#include <stdlib.h>
/* The largest number whose square is a 3-digit number */
#define MAX 31
int nb_of_consecutive_squares(int);
/* For all n in [100, 999], if n is found to be of the form a^2 + b^2
* then sums_of_two_squares[n] will be set to a + b * 100,
* so a and b can be easily retrieved.
* Note that there might be other decompositions of n into a sum of 2 squares;
* we just recall the first decomposition we find.
* Also note that we waste the 100 first elements of the array;
* we can afford it and this choice makes the program simpler. */
int sums_of_two_squares[1000];
int main(void) {
for (int i = 0; i <= MAX; ++i)
for (int j = i; j <= MAX; ++j) {
int n = i * i + j * j;
if (n < 100)
continue;
if (n > 1000)
break;
sums_of_two_squares[n] = i + j * 100;//这条语句只是给这 下标为n的数组提供一个值而已 没有什么实际意义 相当于做个一个记号
}
for (int n = 100; n < 1000; ++n) {
int i = nb_of_consecutive_squares(n);
if (i < 3) {
/* There is no potential triple before n + i + 1; the loop will increment n by 1. */
n += i;//这句话加快了执行速度 如果去掉不影响程序进行
continue;
}
printf("(%d, %d, %d) "
"(equal to (%d^2+%d^2, %d^2+%d^2, %d^2+%d^2)) is a solution./n",
n, n + 1, n + 2,
sums_of_two_squares[n] % 100, sums_of_two_squares[n] / 100,
sums_of_two_squares[n + 1] % 100, sums_of_two_squares[n + 1] / 100,
sums_of_two_squares[n + 2] % 100, sums_of_two_squares[n + 2] / 100);
/* We assume we could have two solutions of the form
* (n, n + 1, n + 2) and (n + 1, n + 2, n + 3)
* (but as the solution shows, this never happens...).
* We could avoid checking that sums_of_two_squares[n + 1] and
* sums_of_two_squares[n + 2] are not equal to 0, but why make the program
* more complicated for no significant gain? */
}
return EXIT_SUCCESS;
}
int nb_of_consecutive_squares(int n) {
if (sums_of_two_squares[n] == 0)//如果等于零则表明 在下标为n的数组元素中不存在值
return 0;
if (sums_of_two_squares[++n] == 0)//n的下标存在值 但是n+1的元素不存在值
return 1;
if (sums_of_two_squares[++n] == 0)//n 和 n+1的下表存在值 但是n+2的下标不存在值
return 2;
return 3;
}
附上的代码怎么都没有颜色提示 算里 就那样把