个人答案:
#include <iostream>
using namespace std;
int sum(int n);
int main()
{
int num;
cout << "请输入n的值: ";
cin >> num;
cout << "结果: " << sum(num) << endl;
system("pause");
return 0;
}
int sum(int n)
{
return n == 1 ? 1 : n * n + sum(n - 1);
}
结果:
参考答案:
#include <iostream>
using namespace std;
int main()
{void convert(int n);
int number;
cout<<"input an integer:";
cin>>number;
cout<<"output:"<<endl;
if (number<0)
{cout<<"-";
number=-number;
}
convert(number);
cout<<endl;
return 0;}
void convert(int n)
{int i;
char c;
if ((i=n/10)!=0)
convert(i);
c=n%10+'0';
cout<<" "<<c;
}