/*
1.利用构造函数求解
一次创建n个对象,将累加的算法写在构造函数中
*/
class Temp
{
public:
Temp(){ N++; Sum += N; }
static void Reset(){ N = 0; Sum = 0; }
static unsigned int Get(){ return Sum; }
private:
static unsigned int N;
static unsigned int Sum;
};
unsigned int Temp::N = 0;
unsigned int Temp::Sum = 0;
unsigned int Sum_Solution1(unsigned int n)
{
Temp::Reset();
Temp* a = new Temp[n];
delete a;
a = NULL;
return Temp::Get();
}
实现1+2+3...+n,要求不能使用乘除法、循环、条件判断
最新推荐文章于 2022-05-02 09:20:44 发布