Chap6: question 46 - 48
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. 不用 +、-、*、/ 做加法
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的更多相关文章
- 1Z0-050
QUESTION 13 View the Exhibit.Examine the following command that is executed for the TRANSPORT table ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q45-Q48)
Question 45 You are designing a branding strategy for a customer with a new SharePoint 2010 server f ...
- 你真的懂了R中的stem函数是如何绘制茎叶图的么?
本文原创,转载请注明出处,本人Q1273314690(交流学习) 哭晕 你真的学会了stem()函数了吗? stem()函数的使用方法是: stem(x, scale=1,width=80, at ...
- OCJP(1Z0-851) 模拟题分析(二)over
Exam : 1Z0-851 Java Standard Edition 6 Programmer Certified Professional Exam 以下分析全都是我自己分析或者参考网上的,定有 ...
- 雅虎公司C#笔试题及参考答案
Question 1. (单选) 在计算机网络中,表征数据传输可靠性的指标是——21. 传输率2. 误码率3. 信息容量4. 频带利用率Question 2. (单选) 以下关于链式存储结构的叙述中哪 ...
- 【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- ...
- Exam E05-001 Information Storage and Management Version 3 Exam
Emc 考试 e05-001信息存储和管理版本3考试 [总问题:171] 哪种 emc 产品提供软件定义的存储基础架构的自动监视和报告? A. viprSrmB. 斯纳普内C. 阿瓦马尔D. 快速副总 ...
- 结对编程——paperOne基于java web的简易四则运算出题网站
项目成员:张金生 张政 需求分析: 1.要进行四则运算: 2.运算题目随机: 3.进行对错判断: 4.整数运算. 程序概要: 1.用JSP实现: 2.用户可选择题目数量: 3.答题页用表格列出 ...
- django应用的测试
本文章默认用户使用win10系统,并且已经安装pycharm.git.django2.2.5及配套第三方库(python3.6.0及以上版本,且为anaconda环境) 前言 其实在上一期django ...
随机推荐
- IT公司100题-26-左旋转字符串
问题描述: 给定字符串和左旋的字符数,写程序实现字符串的左旋操作.例如对于字符串”12345678″, 左旋转4个字符后,变成”56781234″.要求时间复杂度为O(n),空间复杂度O(1). ...
- Linux shell的&&和||
Linux shell的&&和|| shell 在执行某个命令的时候,会返回一个返回值,该返回值保存在 shell 变量 $? 中.当 $? == 0 时,表示执行成功:当 $? ...
- DOM扩展之Selectors API
jQuery的核心就是通过CSS选择符查询DOM文档取得元素的引用,从而抛开了getElementById()和getElementsByTagName(). Selectors API致力于让浏览器 ...
- UE4 在C++ 动态生成几何、BSP体、Brush ---- Mesh_Generation
截至UE4 4.10 runtime 无法生成BSP类 ,只能通过自定义的Mesh的Vertex 进行绘制 ( Google 考证,能改UE4源码的请忽略 ) 可用到的 UE4 集成的Render ...
- wndows程序设计之书籍知识与代码摘录-封装一个类似printf的messagebox
//----------------------------------------- //本程序展示了如何实现MessageBoxPrintf函数 //本函数能像printf那样格式化输出 //摘录 ...
- D - Half of and a Half 大数
D - Half of and a Half Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I ...
- Ubuntu 14.04下安装Hadoop2.4.0 (单机模式)
转自 http://www.linuxidc.com/Linux/2015-01/112370.htm 一.在Ubuntu下创建Hadoop组和hadoop用户 增加hadoop用户组,同时在该组里增 ...
- NSDictionary读取数据类型异常问题.
起因:做网络交互时,经常会使用JSON作为数据的承载体,本来是件好事,但是用多了,发现iOS侧偶尔会出现异常,几经比较发现是服务器给的数据有问题,该给INT的给按照STR给了,服务器能做动态更新,可客 ...
- vim显示行数
在根目录下,新建.vimrc文件,添加以下内容 set number
- 队列的链式实现(C语言)
/* Queue.h */ #ifndef QUEUE_H_INCLUDED #define QUEUE_H_INCLUDED #include <stdio.h> #include &l ...