面试题50:求1+2+...+n

题目:

求1+2+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

思路:

或许你想到了n(n+1)/2,可是不能用乘除法啊,坑~

然后你想到了递归,可是不能用if终止递归,坑~

思路一:用构造函数求解

不能循环n次,但是我们可以创建n个实例,这样相同的代码就会被执行n次。

#include <iostream>      
#include <vector>   
#include <queue>  
#include <string>      
#include <stack>      
#include <algorithm>    
#include <hash_set>  //for hashtable  
#include <hash_map>  
#include <unordered_map>  
#include <set>  
#include <ctime>  
using namespace std;


class Temp{
public:
	Temp(){ ++N; sum += N; }
	static void Reset(){ N = 0; sum = 0; }
	static int GetSum(){ return sum; }
private:
	static unsigned int N;
	static unsigned int sum;
};
unsigned int Temp::N = 0;
unsigned int Temp::sum = 0;

unsigned int Sum(unsigned int n)
{
	Temp *temp = new Temp[n];
	delete[]temp;
	return Temp::GetSum();
}

int main()
{
	cout << Sum(100) << endl;
	return 0;
}

注意:每个static数据成员可以看成是类的一个对象,而不与该类定义的对象有任何关系!


思路二:利用虚函数求解

对n连续做两次反运算,那么非0的n转为true,0转为false。

#include <iostream>      
#include <vector>   
#include <queue>  
#include <string>      
#include <stack>      
#include <algorithm>    
#include <hash_set>  //for hashtable  
#include <hash_map>  
#include <unordered_map>  
#include <set>  
#include <ctime>  
using namespace std;

class A;
A* array[2];  //class中要用到
class A{
public:
	virtual unsigned int GetSum(unsigned int n)
	{
		return 0;
	}
};

class B :public A{
public:
	virtual unsigned int GetSum(unsigned int n)
	{
		return array[!!n]->GetSum(n - 1) + n;
	}
};

unsigned int Sum(unsigned int n)
{
	A a;
	B b;
	array[0] = &a;
	array[1] = &b;
	return array[1]->GetSum(n);
}

int main()
{
	cout << Sum(100) << endl;
	return 0;
}
这种思路是用虚函数来实现函数的选择。


思路三:利用函数指针求解

#include <iostream>        
#include <vector>     
#include <queue>    
#include <string>        
#include <stack>        
#include <algorithm>      
#include <hash_set>  //for hashtable    
#include <hash_map>    
#include <unordered_map>    
#include <set>    
#include <ctime>    
using namespace std;

typedef unsigned int(*fun)(unsigned int);
unsigned int Sum(unsigned int n);
unsigned int SumTeminator(unsigned int n);

fun f[2] = { SumTeminator, Sum }; //函数数组初始化 


unsigned int SumTeminator(unsigned int n)
{
	return 0;
}

unsigned int Sum(unsigned int n)
{
	return n + f[!!n](n - 1);
}

int main()
{
	cout << Sum(100) << endl;
	return 0;
}



思路四:利用模板类型求解

可以让编译器帮助完成类似于递归的计算。

#include <iostream>      
#include <vector>   
#include <queue>  
#include <string>      
#include <stack>      
#include <algorithm>    
#include <hash_set>  //for hashtable  
#include <hash_map>  
#include <unordered_map>  
#include <set>  
#include <ctime>  
using namespace std;

template<unsigned int n> struct Sum
{
	enum Value{ N = Sum<n - 1>::N + n };
};
template<> struct Sum<1>  //特化
{
	enum Value{ N = 1 };
};


int main()
{
	
	cout << Sum<100>::N << endl;
	return 0;
}
cout << Sum<100>::Value::N << endl; //这样调用也可以



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值