任意一个自然数m的立方均可表示为连续的奇数之和, 例如 13 = 1, 23 = 3 + 5, 33 = 7 + 9 + 11 编写程序,
2个回答
展开全部
只做了正数的,负数的添加负号就可以
#include <iostream.h>
int main(void)
{
int M;
int i=0;
cout<<"Pls input the M(end with 0)";
cin>>M;
while (M!=0)
{
cout<<M<<"\n=";
if (M%2 == 0)
{
for (i=M*M-M+1;i<=M*M+M-1;i+=2)
{
cout<<i;
if (i==M*M+M-1)
{
cout<<endl;
}
else
{
cout<<"+";
}
}
}
else
{
for (i=M*M-M/2*2;i<=M*M+M/2*2;i+=2)
{
cout<<i;
if (i==M*M+M/2*2)
{
cout<<endl;
}
else
{
cout<<"+";
}
}
}
cout<<"\nPls input the M(end with 0)";
cin>>M;
}
return 0;
}
#include <iostream.h>
int main(void)
{
int M;
int i=0;
cout<<"Pls input the M(end with 0)";
cin>>M;
while (M!=0)
{
cout<<M<<"\n=";
if (M%2 == 0)
{
for (i=M*M-M+1;i<=M*M+M-1;i+=2)
{
cout<<i;
if (i==M*M+M-1)
{
cout<<endl;
}
else
{
cout<<"+";
}
}
}
else
{
for (i=M*M-M/2*2;i<=M*M+M/2*2;i+=2)
{
cout<<i;
if (i==M*M+M/2*2)
{
cout<<endl;
}
else
{
cout<<"+";
}
}
}
cout<<"\nPls input the M(end with 0)";
cin>>M;
}
return 0;
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询