剑指OFFER题6------按牛客网通过率排序
时间:2018.10.16.2036
作者:Waitt
题目
求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
时间限制:1秒 空间限制:32768K 热度指数:128539
解答
1+2+3+…+n的公式结果为:(n2+n)/2;
由于题目要求不可使用一些语句,则考虑利用math.h中的 pow() 函数以及位左移>> 符号计算。
class Solution {
public:
int Sum_Solution(int n) {
int b=pow(n,2);
return ((b+n)>>1);
}
};