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

使用函数指针

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//使用函数指针 递归加法
typedef (*fun)(int);
int func1(int n) {
return 0;
}
int func2(int n) {
fun f[2] = {func1, func2};
return n + f[(n != 0)](n - 1);
}
void function1() {
cout << func2(10) << endl;
}
//----->fun2 end

使用静态变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
//----->fun2 start
//使用静态变量
class test {
static int N;
static int sum;
public :
test() {
sum += ++N;
}
大专栏  递归加法(day1)lass="function">static void reset() {
N = sum = 0;
}
static int getSum() {
return sum;
}
};
int test::N = 0;
int test::sum = 0;
void function2() {
test::reset();
test *p = new test[10];
cout << test::getSum() << endl;
delete[]p;
}
//--->fun2 end

使用虚函数的编译多态性

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//----->fun3 start
//使用虚函数的编译多态性
class A {
public:
virtual int sum(int n) { return 0; };
};
class B : public A {
public:
int sum(int n) {
A a;
B b;
A *p[2] = {&a, &b};
return n + p[(n - 1 != 0)]->sum(n - 1);
}
};
void function3() {
B b;
cout << b.sum(10) << endl;
}
//----->fun3 end

源码github

递归加法(day1)的更多相关文章

  1. JavaScript 概览 更新时间2014-0414-0837

    一些概念 DOM(文档对象模型)是HTML和XML的应用程序接口(API).DOM Level1规划文档结构:DOM Level2扩展了对鼠标和用户界面事件等的支持:DOM Level3支持了XML1 ...

  2. n阶贝塞尔曲线绘制(C/C#)

    原文:n阶贝塞尔曲线绘制(C/C#) 贝塞尔是很经典的东西,轮子应该有很多的.求n阶贝塞尔曲线用到了 德卡斯特里奥算法(De Casteljau's Algorithm) 需要拷贝代码请直接使用本文最 ...

  3. day1 diff命令递归比较目录下的文件

    一.diff实战 (1)递归比较文件夹下所有的文件及目录的不同 diff --brief -Nr dir1/ dir2/                               Reference ...

  4. hdu1865 1sting (递归+大数加法)

    1sting Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  5. 五一DAY1数论学习笔记

    by ruanxingzhi 整除性 如果a能把b除尽,也就是没有余数,则我们称a整除b,亦称b被a整除.(不是除以,是整除!!) 记作:\(a|b\) |这个竖杠就是整除符号 整除的性质 自反性 对 ...

  6. 52. 不用+、-、×、÷做加法[add two numbers without arithmetic]

    [本文链接] http://www.cnblogs.com/hellogiser/p/add-two-numbers-without-arithmetic.html [题目] 写一个函数,求两个整数的 ...

  7. scala 学习笔记(01) 函数定义、分支、循环、异常处理、递归

    package yjmyzz import scala.io.StdIn object ScalaApp { def main(args: Array[String]) { println(" ...

  8. lecture7-序列模型及递归神经网络RNN

    Hinton 第七课 .这里先说下RNN有recurrent neural network 和 recursive neural network两种,是不一样的,前者指的是一种人工神经网络,后者指的是 ...

  9. lecture7-序列模型及递归神经网络RNN(转载)

    Hinton 第七课 .这里先说下RNN有recurrent neural network 和 recursive neural network两种,是不一样的,前者指的是一种人工神经网络,后者指的是 ...

随机推荐

  1. POJ 3273 Monthly Expense二分查找[最小化最大值问题]

    POJ 3273 Monthly Expense二分查找(最大值最小化问题) 题目:Monthly Expense Description Farmer John is an astounding a ...

  2. jacascript 滚动scroll

    滚动 scroll scrollHeight 表示元素的总高度,包括由于溢出而无法展示在网页的不可见部分: scrollWidth 表示元素的总宽度,包括由于溢出而无法展示在网页的不可见部分: 没有滚 ...

  3. linux 中的.so和.a文件

    Linux下的.so是基于Linux下的动态链接,其功能和作用类似与windows下.dll文件. 下面是关于.so的介绍: 一.引言 通常情况下,对函数库的链接是放在编译时期(compile tim ...

  4. AFNetWorking3.0.4(Weather Demo)

    参考文章:http://www.jianshu.com/p/99bcc40abb30 demo下载地址:http://pan.baidu.com/s/1eRuYDEi 密码:ma5m 使用最新AFNe ...

  5. layui子弹框调用父弹框方法

    var thisFrame = parent.window.document.getElementById("LAY_layuiStampDuty1").getElementsBy ...

  6. auctex 11.86的墓志铭

    卸载了emacs23,在Ubuntu 软件中心搜索并安装了emacs24.3.把auctex11.86也卸载了(自然,在.emacs文件里也注释掉了它的加载路径,但是我不会删去那段代码的.让注释的那段 ...

  7. 55)PHP,在html嵌套PHP写法

    样例代码:

  8. P2967 [USACO09DEC]视频游戏的麻烦Video Game Troubles

    冲刺阶段的首篇题解! 题目链接:P2967 [USACO09DEC]视频游戏的麻烦Video Game Troubles: 题目概述: 总共N个游戏平台,金额上限V元,给出每个游戏平台的价钱和其上游戏 ...

  9. python中读取mat文件

    mat数据格式是Matlab的数据存储的标准格式 在python中可以使用scipy.io中的函数loadmat()读取mat文件. import scipy.io as scio path = 'e ...

  10. python3多线程应用详解(第二卷:多线程到底是怎么工作的)

    现在很多人都说用多线程工作快是因为多个不同任务可以同时执行,注意我说的是不同任务,要是重复做一件事达到相同效果就是画蛇添足了,其实这是个错误的说法,线程真正的本质是无法同时执行的.现在我们来看下多线程 ...