马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
#include <math.h>
#define NUM 128
int main()
{
int num[NUM][NUM];
int cube1, cube2, right, count;
int i, j, k;
char q;
printf("请输入一个整数:");
scanf("%d", &count);
cube1 = count * count * count;
right = count * (count * (count - 1) + 1) + count * (count -1);
if (cube1 == right)
{
printf("经验证,3 ~ %d之间的所有整数均符合尼科彻斯定理!\n\n", count);
}
getchar();
printf("是否打印所有式子(y/n):");
scanf("%c", &q);
if (q == 'y')
{
for (i = 0; i < count - 2; i++) //3~count之间所有整数
{
k = i + 3;
cube2 = k * k * k;
printf("%d^%d == %d == ", k, k, cube2);
for (j = 0; j < 3 + i && j < count; j++)
{
num[i][j] = k * (k - 1) + 1 + 2 * j;
printf(j? "+ %d " : "%d ", num[i][j]);
}
printf("\n");
}
}
else
{
printf("\n");
}
return 0;
}
我还以为老师的答案错了,原来老师的尼科彻斯定理跟百度的不一样,晕!!!
#include <stdio.h>
#include <stdbool.h>
int main()
{
int i, j, n, 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; j += 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:
sum = 0;
}
// 检查
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;
}
|