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. Android 源码下载

    一直想尝试android源码的编译,这两天正好海思代码的编译也需要ubuntu环境,于是安装了ubuntu 12.04,安装时选了语言为中文,因此下面很多状态及错误报告都是中文了,另外分配了4G sw ...

  2. wp8.1 Study16:网络之 使用Azure移动服务及利用Azure推送通知服务

    一.WP8.1有关网络的API WP8.1与其它平台的对比如下图: 二.Azure移动服务 前提: Azure移动服务可以让使用者的数据存放在云空间,从而方便使用者的App在不同平台上的数据共享. 1 ...

  3. 读取配置文件 PropertyPlaceholderConfigurer 的配置与使用

    public class SpringPropertyConfigurer extends PropertyPlaceholderConfigurer { private static Map< ...

  4. java 泛型详解

    public class Base<T>{    Class classType=T.class;    public Base(){     //想在这里 得到T的类对象 赋值给 cla ...

  5. SharePoint开发——利用CSOM逐级获取O365中SharePoint网站的List内容

    博客地址:http://blog.csdn.net/FoxDave 本文介绍如何利用SharePoint客户端对象模型(.NET)逐级获取Office 365网站中List的内容,仅仅是示例,没有 ...

  6. sqoop的export的说明

    1.通用参数说明

  7. JS的(function($){})(query)

    function(arg){...} 这就定义了一个匿名函数,参数为arg 而调用函数时,是在函数后面写上括号和实参的,由于操作符的优先级,函数本身也需要用括号,即:(function(arg){.. ...

  8. poj2387 spfa求最短路

    //Accepted 4688 KB 63 ms #include <cstdio> #include <cstring> #include <iostream> ...

  9. Android RecyclerView单击、长按事件标准实现:基于OnItemTouchListener + GestureDetector

     Android RecyclerView单击.长按事件:基于OnItemTouchListener + GestureDetector标准实现 Android RecyclerView虽然拥有L ...

  10. 数据库最大连接池max pool size

    本文导读:Max Pool Size如果未设置则默认为100,理论最大值为32767.最大连接数是连接池能申请的最大连接数,如果数据库连接请求超过此数,后面的数据库连接请求将被加入到等待队列中,这会影 ...