程序员面试题精选100题(08)-求1+2+...+n[C/C++/C#] - 何海涛的日志 - 网易博客
题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。
Use constructor:
class Temp
{
public:
Temp() { ++ N; Sum += N; }
static void Reset() { N = 0; Sum = 0; }
static int GetSum() { return Sum; }
private:
static int N;
static int Sum;
};
int Temp::N = 0;
int Temp::Sum = 0;
int solution1_Sum(int n)
{
Temp::Reset();
Temp *a = new Temp[n];
delete []a;
a = 0;
return Temp::GetSum();
}
return Temp::GetSum();}
Read full article from 程序员面试题精选100题(08)-求1+2+...+n[C/C++/C#] - 何海涛的日志 - 网易博客
题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。
Use constructor:
class Temp
{
public:
Temp() { ++ N; Sum += N; }
static void Reset() { N = 0; Sum = 0; }
static int GetSum() { return Sum; }
private:
static int N;
static int Sum;
};
int Temp::N = 0;
int Temp::Sum = 0;
int solution1_Sum(int n)
{
Temp::Reset();
Temp *a = new Temp[n];
delete []a;
a = 0;
return Temp::GetSum();
}
return Temp::GetSum();}
- static int find(int n){
- int re=0;
- boolean whatever=false;
- int a=-1;
- whatever=(n!=0)&&(a==(re=find(n-1)));
- return re+n;
- }