您的位置:首页 > 编程语言 > C语言/C++

限制条件下求1+2+...+n C++实现

2014-02-24 16:55 267 查看
题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。

//============================================================================
// Name        : CountWithoutLoop.cpp
// Author      : Lee
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
using namespace std;

class Lee{
public:
virtual int compute (int n){
return 0;
}
};
class LeeCom:public Lee{
public:
int compute(int n){
Lee * lee[2]={new Lee(),new LeeCom()};
return n+lee[!!(n-1)]->compute(n-1);
}
};
int main() {
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
LeeCom lee;
cout<<lee.compute(100);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: