S1E21尼科彻斯定理
#include <stdio.h>#include <math.h>
#include <string.h>
#define M 1024
int main()
{
unsigned int n, i, j = 0, sum = 0, k = 0;
unsigned int num;
char a;
printf("请输入一个整数(大于2):");
scanf("%d", &n);
num = pow(n, 3);
for (i = 1;i < (num);i += 2)
{
for (j = i;j < (num);j += 2)
{
a = j; //j存入数组a
sum += j;
if (sum == num)
{
a = '\0';/////////
goto DONE;
}
else if (sum > num)
{
for (k = 0;k < M;k++) //将数组a元素清零
{
a= 0;
}
a = '\0';
k = 0;
sum = 0;
break;
}
else
{
;
}
}
}
DONE:printf("%d = %d + %d + ... + %d\n", num, a, a, a);
for (j = 0;j < strlen(a);j++)
{
printf("%d ", a);
}
return 0;
}
我的程序可以计算10以内的数,当n > 10时会出现图示负数以及结果不准确的情况,求解答。 很简答,因为你定义的a数组是char类型,该类型只有8个bit,故而能够存储数的大小范围在 -128~127。
故代码第11行可以改为:
int a; 本帖最后由 jhq999 于 2021-9-23 13:01 编辑
int main(void)
{
int m=0,a=0,b=0;
scanf("%d",&m);
a=m*m;
if (a%2)
{
a-=2;
}
else
a--;
a-=(m/2-1)*2;
for (int i = 0; i < m-1; i++,a+=2)
{
printf("%d+",a);
b+=a;
}
b+=a;
printf("%d=%d",a,b);
return 0;
}
#include <stdio.h>
int main(void)
{
/*验证3--20 之间的尼科彻斯定理*/
for (int i=3;i<=20;i++)
{
int first=i*(i-1)+1;
int sum=0;
for (int j=0;j<i;j++)
{
sum+=j*2+first;
}
if (sum==i*i*i)
{
printf("%d^3=%d =",i,i*i*i);
for (int j=0;j<i;j++)
{
if(j==i-1){
printf("%d",j*2+first);
}
elseprintf("%d+",j*2+first);
}
printf(" success\n");
}
else
{
printf("failed\n");
}
}
return 0;
}
/*
PS D:\我> ./wp4
3^3=27 =7+9+11 success
4^3=64 =13+15+17+19 success
5^3=125 =21+23+25+27+29 success
6^3=216 =31+33+35+37+39+41 success
7^3=343 =43+45+47+49+51+53+55 success
8^3=512 =57+59+61+63+65+67+69+71 success
9^3=729 =73+75+77+79+81+83+85+87+89 success
10^3=1000 =91+93+95+97+99+101+103+105+107+109 success
11^3=1331 =111+113+115+117+119+121+123+125+127+129+131 success
12^3=1728 =133+135+137+139+141+143+145+147+149+151+153+155 success
13^3=2197 =157+159+161+163+165+167+169+171+173+175+177+179+181 success
14^3=2744 =183+185+187+189+191+193+195+197+199+201+203+205+207+209 success
15^3=3375 =211+213+215+217+219+221+223+225+227+229+231+233+235+237+239 success
16^3=4096 =241+243+245+247+249+251+253+255+257+259+261+263+265+267+269+271 success
17^3=4913 =273+275+277+279+281+283+285+287+289+291+293+295+297+299+301+303+305 success
18^3=5832 =307+309+311+313+315+317+319+321+323+325+327+329+331+333+335+337+339+341 success
19^3=6859 =343+345+347+349+351+353+355+357+359+361+363+365+367+369+371+373+375+377+379 success
20^3=8000 =381+383+385+387+389+391+393+395+397+399+401+403+405+407+409+411+413+415+417+419 success
*/ newu 发表于 2021-9-23 12:34
很简答,因为你定义的a数组是char类型,该类型只有8个bit,故而能够存储数的大小范围在 -128~127。
故代码 ...
for (j = 0;j < strlen(a);j++)
{
printf("%d ", a);
}
本来我定义的也是int,但是我想用strlen()函数求数组a里字符的个数,就改成了char。请问我在不对a[]进行计数的情况下,有更好的方法打印数组a[]的每个值么 雨中漫步~ 发表于 2021-9-23 14:26
本来我定义的也是int,但是我想用strlen()函数求数组a里字符的个数,就改成了char。请问我在不对a[]进 ...
也简单。那你就把int数组全部初始化为0,然后输出的时候,写个循环判断,如果值不是零的话就输出。
如下代码:
定义数组改为:
int a = {0};
循环输出改为:
for (j=0; a!=0; j++)
{
printf("%d ", a);
} #include <stdio.h>
int main(){
int num, x;
scanf("%d", &num);
x = num*num-num+1;
int res = x;
printf("%d = %d = %d", num, num*num*num, x);
while (res != (num*num*num)){
x += 2;
res += x;
printf("+%d", x);
}
return 0;
}
11 = 1331 = 111+113+115+117+119+121+123+125+127+129+131
17 = 4913 = 273+275+277+279+281+283+285+287+289+291+293+295+297+299+301+303+305 newu 发表于 2021-9-23 14:48
也简单。那你就把int数组全部初始化为0,然后输出的时候,写个循环判断,如果值不是零的话就输出。
如 ...
妙啊,感谢指导
页:
[1]