求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

// test13.cpp : 定义控制台应用程序的入口点。
//

include "stdafx.h"

include

include

include

include

using namespace std;

class Solution {
public:
int Sum_Solution(int n) {
if (n == 1)
return 1;
else
return Sum_Solution(n - 1) + n;

}
};

int _tmain(int argc, _TCHAR* argv[])
{
Solution so;
int num;
while (cin>>num)
{
cout << "1+2+...+" <<num <<"的和是: "<< so.Sum_Solution(num) << endl;
}

}

注意:f(n)=f(n-1)+n

posted @ 2016-09-30 20:11  wdan2016  阅读(1198)  评论(0)    收藏  举报