题目描述:

求 1+2+...+n,

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

分析:

首先想到的是写递归函数,但是遇到一个问题,递归函数总需要一个出口,不然会无穷递归下去。

出口一半是 if() return. 题目又要求不能使用 if 语句。

什么语句有类似与 if 的选择功能呢??

解法 1: || 运算符号

A || B 有如下性质: 当 A 是 true 时, B 语句不再运行。。这里就包含了 选择的功能。

// Method 2: recursive function
int Sum(int n)
{
int ret = 0;
n == 0 || (ret = Sum(n-1));
return n + ret;
}

解法 2: 模板元编程 TMP

很酷的名字吧。。这是一个非常酷的技术,能够把计算的时间从运行期提前到编译期。

参考 Effective C++。

其实思想就是 利用函数模板的特化(specialization),来模拟 选择语句。

// Method 1: TMP, reference: Effective C++
template<unsigned n>
struct MySum{
enum { value = MySum<n-1>::value + n };
}; // use template specialization to mimic if expression
template<>
struct MySum<0>{
enum { value = 0 };
};

MySum<10>::value 就是我们要的结果。。

解法 3:

采用C++ 类中的 static 变量,已经构造函数在对象构造时会被自动调用的性质。

// Method 3: static variable of class
struct MyClass{
MyClass(){
sum += ++ n;
}
static int n;
static int sum;
};
// static members should be defined outside class body
int MyClass::n = 0;
int MyClass::sum = 0;
  MyClass a[10];
cout << MyClass::sum << endl;

Interview----求 1+2+...+n, 不能用乘除法、for、while if、else、switch、case 等关键字以及条件判断语句 (A?B:C)的更多相关文章

  1. 求1+2+…+n,要求不能使用乘除法、for、while、if、else、s witch、case 等关键字以及条件判断语句(A?B:C)和不用循环/goto/递归输出1~100的10种写法

    来源:据说是某一年某个公司的面试题 题目:求1+2+…+n, 要求不能使用乘除法.for.while.if.else.s witch.case 等关键字以及条件判断语句(A?B:C) 分析:这题本来很 ...

  2. C语言奇思妙想:求1+2+…+n,要求不能使用乘除法、for、while、if、else、s witch、case 等关键字以及条件判断语句(A?B:C)

    来源:据说是某一年某个公司的面试题 题目:求1+2+…+n, 要求不能使用乘除法.for.while.if.else.s witch.case 等关键字以及条件判断语句(A?B:C) 分析:这题本来很 ...

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

    1 题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 2 思路和方法 (1)递归,不能使用if等 ...

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

    代码如下: public int Sum_Solution(int n) { int temp = n; boolean b = (temp>0)&&(temp += Sum_S ...

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

    #include <iostream> using namespace std; int add_(int a,int b){ return 0; } int Add(int i,bool ...

  6. 求1+2+3+...+n的值,要求不能使用乘除法,for、while、if、else、switch、case、等关键字及条件判断语句(JAVA)

    采用递归和三目表达式注意红色字体一定不能写成n-- 1 package com.hunag; public class Sum { static int sum; public static int ...

  7. Divide two numbers,两数相除求商,不能用乘法,除法,取模运算

    问题描述:求商,不能用乘法,除法,取模运算. 算法思路:不能用除法,那只能用减法,但是用减法,超时.可以用位移运算,每次除数左移,相当于2倍. public class DividTwoInteger ...

  8. (剑指Offer)面试题46:求1+2+3+....+n

    题目: 求1+2+3+...+n,要求不能使用乘除法,for,while,if,else,switch,case等关键字及条件判断语句(a?b:c). 思路: 1.构造函数 在类中定义静态成员变量N和 ...

  9. 题目:求1+2+…+n,

    题目:求1+2+-+n, 要求不能使用乘除法.for.while.if.else.switch.case等关键字 以及条件判断语句(A?B:C). java 实现 public class sum { ...

  10. 求1+2+……+n(位运算)

    求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 我发现网上的做法都很神,各种理由编译的巧妙办法,就能间接 ...

随机推荐

  1. 工作日志 jquery slideDown slideUp

    jquery里面使用 slideDown 和  slideUp会有一个像素的偏差

  2. HTML5自学笔记[ 20 ]canvas绘图实例之绘制倒影

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  3. C/C++ 关于生成静态库(lib)/动态库(dll)文件如何使用(基于windows基础篇)

    1. 首先,如何制作一个静态库(lib)? 额, 对于静态库,我们知道,里头是不应该有Main函数,它只是一个配合文件.之所以称之为lib静态库,其实就是指,我们需要用到lib里头的函数时,我们才会去 ...

  4. hdu---(1054)Strategic Game(最小覆盖边)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. javaSE基础之基本细节注解

    1.  对于多行注释而言,不能进行嵌套注释.....! /* dada /* d adasdas */ */ 只是不被允许的.... 2.对于记事本编程......如果竹类是公有类,则必须保证类名和为 ...

  6. Combination Sum II [LeetCode]

    Problem description: http://oj.leetcode.com/problems/combination-sum-ii/ Basic idea: use recursive a ...

  7. JSCore的基本使用

    一.简单介绍 JSCore全称为JavaScriptCore,是苹果公司在iOS中加入的一个新的framework.该framework为OC与JS代码相互操作的提供了极大的便利.该工程默认是没有导入 ...

  8. java.lang.IllegalArgumentException: addChild: Child name '/SSHE' is not unique

    错误信息: Caused by: java.lang.IllegalArgumentException: addChild:  Child name '/SSHE' is not unique     ...

  9. 5月21日 CSS样式表加阴影

    HTML <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3 ...

  10. jQuery学习小结1-CSS操作+事件

    一.DOM对象和jQuery 对象互换 1.jQuery对象 就是通过jQuery包装DOM对象后产生的对象.jQuery对象是jQuery独有的,其可以使用jQuery里的方法.比如: $(&quo ...