|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
这是我的代码,但输出的64 不是1 + 3 +...+15。求助#include <stdio.h>
int main(void)
{
int count;
int cube;
int sum = 0;
printf("请输入一个整数:\n");
scanf("%d",&count);
// // 定义一个二维数组来存储数值
// int num;
// for (num = 1;num * num * num < count * count * count;num++)
// {}
int n[count][4];
for (int i = 3;i <= count;i++)
{
cube = i * i * i;
for (int j = 1;j < cube;j += 2)
{
for (int k = j;k < cube;k += 2)
{
sum += k;
if (sum == cube)
{
// 存储数值
n[i - 1][0] = cube;
n[i - 1][1] = j;
n[i - 1][2] = j + 2;
n[i - 1][3] = k;
// if (k - j <= 4)
// printf("%d = %d + %d + %d\n",cube,j,j+2,k);
// else
// printf("%d = %d + %d + ... + %d\n",cube,j,j+2,k);
goto CONTINUE;
}
if (sum > cube) // 跳出 循环 还要记得把sum归0
{
sum = 0;
break;
}
}
}
CONTINUE:
; // 空语句,继续 i 循环
}
int b;
printf("请输入想要打印的范围:\n");
scanf("%d",&b);
for (int i = 3;i <= b;i++)
{
if (i == 3)
printf("%d = %d + %d + %d\n",n[i - 1][0],n[i - 1][1],n[i - 1][2],n[i - 1][3]);
else
printf("%d = %d + %d ... +%d\n",n[i - 1][0],n[i - 1][1],n[i - 1][2],n[i - 1][3]);
}
// printf("%d %d %d",n[4][1],n[4][2],n[4][3]);
return 0;
}
已修改: #include <stdio.h>
int main()
{
int n, res, flag, flag2, flag3 = 1;
char c;
printf("输入一个整数:");
scanf("%d", &n);
int ans[256][256] = {{0}};
for(int num = 3, x = num*num*num, p = 0; num <= n; num++, p++, x = num*num*num, flag2 = 0){
for(int i = 1; i < x; i += 2){
res = flag = 0;
for(int j = i, k = 0; j < x; j += 2){
res += ans[p][k++] = j;
if(res == x){
flag = flag2 = 1;
break;
}
else if(res > x) break;
}
if(flag) break;
}
if(!flag2){
printf("经验证,3 ~ %d 之间所有的整数不符合尼科彻斯定理!\n\n", n);
flag3 = 0;
break;
}
}
if(flag3) printf("经验证,3 ~ %d 之间所有的整数符合尼科彻斯定理!\n\n", n);
printf("是否打印所有式子(y/n):");
while((c = getchar()) != '\n');
scanf("%c", &c);
switch(c){
case 'y':
for(int i, j, r = i = 0; i < n-2; i++){
for(j = 0; ans[i][j] && ans[i][j] != ans[i][j-1]; j++){}
if(j > 3) printf("%d^3 == %d == %d + %d + ... + %d", i+3, (i+3)*(i+3)*(i+3), ans[i][0], ans[i][1], ans[i][j-1]);
else printf("%d^3 == %d == %d + %d + %d", i+3, (i+3)*(i+3)*(i+3), ans[i][0], ans[i][1], ans[i][2]);
printf("\n");
}
break;
case 'n': break;
}
return 0;
}
|
|