#include <iostream> using namespace std; class Plus { public: Plus() { ++i, sum += i; } //去掉下面的reset函数试试 static void reset() { i = 0; sum = 0; } //去掉下面的static关键字试试 static int getSum() { return sum; } private: static int i; static int sum; }; int Plus::i = 0; int Plus::sum = 0; int cacSum(int n) { //只有static函数才能如此调用 Plus::reset(); Plus *test = new Plus[n]; delete[] test; return Plus::getSum(); } int main() { int i = 100; int result = cacSum(i); cout << "result = " << result << endl; system("pause"); return 0; }
int getSum(int n) { int result=0; (n>0)&&(result=getSum(n-1)+n); return result; }
1+2+3+...+n不能用while、for、if else等实现,布布扣,bubuko.com
1+2+3+...+n不能用while、for、if else等实现
原文地址:http://blog.csdn.net/u010367506/article/details/28092891