本帖最后由 lizhiyong_11 于 2017-8-4 15:27 编辑
#include <stdio.h>
#include <stdbool.h>
int main()
{
int n, i, j, count, cubed, sum = 0;
bool result = true;
char answer;
printf("请输入一个整数:");
scanf("%d",&count);
int array[count][4];
for (n = 3; n <= count; n++)
{
array[n][0] = 0;
}
for (n =3; n <= count; n++)
{
cubed = n * n * n;
for(i = 1; i < cubed; i += 2)
{
for (j = i; j < cubed; i +=2)
{sum += j;
if (sum == cubed)
{
array[n][0] = cubed;
array[n][1] = i;
array[n][2] = i +2;
array[n][3] = j;
goto FINDIT;
}
if (sum > cubed)
{
sum = 0;
break;
}
}
}
FINDIT:
;
}
for (n = 3; n <= count; n++)
{
if (array[n][0] == 0)
{
result = false;
break;
}
}
if (result)
{
printf("经验证,3~%d 之间的所有整数均符合尼克斯定理!\n\n",count);
printf("是否打印所有等试(y/n):");
getchar();
scanf("%c",&answer);
}
else
{
printf("验证失败:整数%d无法找到对应的连续奇数!\n");
}
if (answer == 'y')
{
for (n = 3; n <= count; n++)
{
if (array[n][3] - array[n][1] > 4)
{
printf("%d^3 == %d == %d + %d +...+%d\n", n, array[n][0], array[n][1], array[n][2], array[n][3]);
}
else
{
printf("%d^3 == %d == %d + %d +%d\n", n, array[n][0], array[n][1], array[n][2], array[n][3]);
}
}
}
return 0;
}
一点一点从我的虚拟机上copy下来的,可以通过编译,但是有问题,请看下一楼的输出结果 |