题目描述:

求 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. url重写后发布出错问题

    iis7 配置urlrewriter重写失效的问题 在IIS7下,如果使用微软的 URLRewriter 重写控件则需要在WEB.CONFIG中配置以下信息 第一个配置: <configSect ...

  2. P92认识对话框

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  3. Loadrunner基础:Loadrunner Vuser基本概念和应用

    学习示例 Loadrunner自带有WebTour的网站可以帮助初学者学习性能测试安装完Loadrunner以后进入到Program Files下的WebTour文件加,启动WebTour服务在浏览器 ...

  4. SQL Server数据库(SQL Sever语言 CRUD)

    使用SQL Sever语言进行数据库的操作 常用关键字identity 自增长primary key 主键unique 唯一键not null 非空references 外键(引用) 在使用查询操作数 ...

  5. css3内容溢出属性

    overflow是css2.0的属性,css3中新增了overflow-x和overflow-y属性. overflow-x主要是用来定义对水平方向内容溢出的剪切,而overflow-y主要是用来定义 ...

  6. CentOS 下的MySQL配置

    先贴出代码(/etc/my.cnf)如下: #The following options will be passed to all MySQL clients [client] #password ...

  7. 在vim中执行外部命令

    11.7.5  在Vim编辑器中执行Shell命令 有时需要在Vim编辑器中执行Shell命令,例如需要验证一个Shell命令是否正确,以便写入脚本中:需要在文件中引用某个Shell命令的输入等.本小 ...

  8. 如何使用JCONSOLE 监控eclipse的tomcat

    在默认情况下,使用jconsole 监控本地tomcat 是不需要任何配置的,直接连接就可以监控tomcat.   但是在eclipse 下启动是监控不了.   解决方法:   设置jvm参数:   ...

  9. SQL逻辑读变成零

    使用缓存HINT 让逻辑读变成0. create table t as select * from dba_objects; insert into t select * from t; commit ...

  10. POJ 3274 Gold Balanced Lineup 哈希,查重 难度:3

    Farmer John's N cows (1 ≤ N ≤ 100,000) share many similarities. In fact, FJ has been able to narrow ...