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

来源:互联网 发布:网络接口转换器 编辑:程序博客网 时间:2024/04/29 22:08

题目:求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;}


 

0 0