|
15鱼币
课后作业S1E21的动动手2的这段代码,在vscode编译时,int array[count][4],无论n是多少,编译出来array[count][3]都是错的,如果改成int array[count][5]就可以了,但是在Dev C++用array[count][4]编译却没问题,是不是我的vscode哪里设置错了,求大佬解答!!!#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;
}
编程是很灵活的 #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];
int array[count + 1][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;
}
|
-
-
|