46. 求 1+2+ … +n。

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

a. 利用构造函数求解

#include <iostream>
using namespace std; class Sum{
public:
Sum() {++n; sum += n;}
static void reset() {sum = n = 0;}
static int getSum(){ return sum;}
private:
static int n;
static int sum;
};
int Sum::n = 0;
int Sum::sum = 0; int solution(int n)
{
Sum::reset();
Sum *p = new Sum[n];
delete[] p;
return Sum::getSum();
} int main()
{
cout << solution(100) << endl;
cout << solution(1000) << endl;
cout << solution(100) << endl;
return 0;
}
b. 利用虚函数函数指针求解
#include <iostream>
using namespace std;
class Base;
class Base* A[2];
class Base{
public:
virtual int sum(int n) { return 0; }
};
class Dirived: public Base{
public:
int sum(int n)
{
return A[!!n]->sum(n-1) + n;
}
};
int main()
{
A[0] = new Base;
A[1] = new Dirived; cout << A[1]->sum(100) << endl;
cout << A[1]->sum(1000) << endl;
cout << A[1]->sum(100) << endl;
return 0;
}
利用函数指针
#include <iostream>
using namespace std; typedef int (*F)(int n);
F A[2];
int fun1(int n)
{
return 0;
}
int fun2(int n)
{
return A[!!n](n-1) + n;
} int main()
{
A[0] = fun1;
A[1] = fun2;
cout << A[1](100) << endl;
cout << A[1](1000) << endl;
cout << A[1](100) << endl;
return 0;
}
c. 利用模板类型和枚举类型求解(编译器完成工作)
#include <iostream>
using namespace std; template<size_t N>class A{
public:
enum { N = A<N-1>::N + N};
}; template<>class A<1>{
public:
enum { N = 1};
}; int main()
{
const int n = 100;
cout << A<n>::N << endl;
cout << A<500>::N << endl;
/* cout << A<1000>::T.N << endl;*/ // not support too long like 1000
return 0;
}

47. 不用 +、-、*、/ 做加法

GO: 奇妙的数学 -> 3.不用加减乘除求两个整数的和

48. 不能被继承的类

有缺陷的方法1:

class A{
public:
static A* getInstance() { return new A; }
static void deleteInstance(A *rhs) { delete[] rhs;}
private:
A(){}
~A(){}
};

缺点:使用麻烦,且只能得到位于堆上的实例。

方法2:利用虚拟继承。

template<typename T>class Help
{
friend T;
private:
Help(){}
~Help(){}
};
class A : virtual public Help<A>
{
public:
A(){}
~A(){}
};

如上,类 A 可以很方便的声明对象; 同时它是虚拟继承,故若有类继承它,则直接调动 Help 构造函数,导致编译失败。

注: GCC不支持模版参数类型作为友元类型。

Chap6: question 46 - 48的更多相关文章

  1. 1Z0-050

    QUESTION 13 View the Exhibit.Examine the following command that is executed for the TRANSPORT table ...

  2. Sharepoint学习笔记—习题系列--70-576习题解析 -(Q45-Q48)

    Question 45 You are designing a branding strategy for a customer with a new SharePoint 2010 server f ...

  3. 你真的懂了R中的stem函数是如何绘制茎叶图的么?

    本文原创,转载请注明出处,本人Q1273314690(交流学习)   哭晕 你真的学会了stem()函数了吗? stem()函数的使用方法是: stem(x, scale=1,width=80, at ...

  4. OCJP(1Z0-851) 模拟题分析(二)over

    Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...

  5. 雅虎公司C#笔试题及参考答案

    Question 1. (单选) 在计算机网络中,表征数据传输可靠性的指标是——21. 传输率2. 误码率3. 信息容量4. 频带利用率Question 2. (单选) 以下关于链式存储结构的叙述中哪 ...

  6. 【Java】-NO.20.Exam.1.Java.1.001- 【1z0-807】- OCEA

    1.0.0 Summary Tittle:[Java]-NO.20.Exam.1.Java.1.001-[1z0-807] Style:EBook Series:Java Since:2017-10- ...

  7. Exam E05-001 Information Storage and Management Version 3 Exam

    Emc 考试 e05-001信息存储和管理版本3考试 [总问题:171] 哪种 emc 产品提供软件定义的存储基础架构的自动监视和报告? A. viprSrmB. 斯纳普内C. 阿瓦马尔D. 快速副总 ...

  8. 结对编程——paperOne基于java web的简易四则运算出题网站

    项目成员:张金生     张政 需求分析: 1.要进行四则运算: 2.运算题目随机: 3.进行对错判断: 4.整数运算. 程序概要: 1.用JSP实现: 2.用户可选择题目数量: 3.答题页用表格列出 ...

  9. django应用的测试

    本文章默认用户使用win10系统,并且已经安装pycharm.git.django2.2.5及配套第三方库(python3.6.0及以上版本,且为anaconda环境) 前言 其实在上一期django ...

随机推荐

  1. Charles的使用

    简介 Charles是在Mac下常用的截取网络封包的工具,在做iOS开发时,我们为了调试与服务器端的网络通讯协议,常常需要截取网络封包来分析.Charles通过将自己设置成系统的网络访问代理服务器,使 ...

  2. linux shell 指令 诸如-d, -f, -e之类的判断表达式

    文件比较运算符-e filename 如果 filename存在,则为真 [ -e /var/log/syslog ]-d filename 如果 filename为目录,则为真 [ -d /tmp/ ...

  3. JS实现表格的增删改

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/T ...

  4. excel具有制作甘特图的功能

    1.Excel最大功能:数据处理.统计分析. 2.数据有效性验证: 长数字输入方法,文本前面加英文"'"(单引号)或使用文本转换. 身份证号:数据.数据有效性.文本长度. 性别:数 ...

  5. php-eclipse乱码处理

    方法一:1)设置"eclipse目录下eclipse.ini文件"2)在文件结尾添加"-Dfile.encoding=UTF-8".3)重新启动eclipse, ...

  6. js中退出语句break,continue和return 比较 (转载)

    在 break,continue和return 三个关键字中, break,continue是一起的,return 是函数返回语句,但是返回的同时也将函数停止 首先:break和continue两个一 ...

  7. gulp ---攻略一

    根据项目需要可能会出连载 项目需要现在用gulp进行js的质量检测.合并.压缩.发布,未来需要进行sass的编译.合并.压缩,html.img的压缩以及md5戳.reload等功能,暂时先测试js的质 ...

  8. ACE - Reactor模式源码剖析及具体实现(大量源码慎入)

    原文出自http://www.cnblogs.com/binchen-china,禁止转载. 在之前的文章中提到过Reactor模式和Preactor模式,现在利用ACE的Reactor来实现一个基于 ...

  9. 浅谈对ECharts的使用

    上个月的项目,其中有一个模块用的是ECharts来实现的,分别用了折线图,环形图,还有漏斗图,这几个都算比较常见的了,尤其是折线图,环形图,用的最多的就是它们了.之前也没怎么接触过ECharts,实际 ...

  10. Linux内核分析——跟踪分析Linux内核的启动过程

    万子惠 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程 实验部分 menu程序: cd LinuxKernel/ qemu -kernel linux-3.18.6/a ...