有关(Nicomachus)定理哪个编程高手知道编?

来源:百度知道 编辑:UC知道 时间:2024/09/28 09:23:07
就是一个数的立方等于几个连续奇数的和如:
1^3=1
2^3=3+5
3^3=7+9+11...
不要是死循环!

//---------------------------------------------------------------------------

#include <stdio.h>

int lf(const int a,int n)
{
int i=n,s=0;
while (s<a)
{
s+=n;
n+=2;
}

if (s==a){
while (s)
{
printf("%d\t",i);
s-=i;
i+=2;
}
return 1;
}
return 0;
}
int main(void)
{
int i,a;
scanf("%d",&a);/*输入底数*/
a=a*a*a;
for (i=1; i<a; i+=2) {
if (lf(a,i)) break;/*输出结果*/
}
return 0;
}
//---------------------------------------------------------------------------