博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《剑指offer》第六十四题(求1+2+…+n)
阅读量:5020 次
发布时间:2019-06-12

本文共 3206 字,大约阅读时间需要 10 分钟。

// 面试题64:求1+2+…+n// 题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case// 等关键字及条件判断语句(A?B:C)。#include 
// ====================方法一====================//使用构造函数class Temp{public: Temp() { ++N; Sum += N; } static void Reset() { N = 0; Sum = 0; } static unsigned int GetSum() { return Sum; }private: static unsigned int N; static unsigned int Sum;//静态成员,所有实例共享};unsigned int Temp::N = 0;unsigned int Temp::Sum = 0;unsigned int Sum_Solution1(unsigned int n){ Temp::Reset(); Temp *a = new Temp[n];//建立n次,实现sum加和 delete[]a; a = NULL; return Temp::GetSum();}// ====================方法二====================//使用虚函数class A;A* Array[2];class A{public: virtual unsigned int Sum(unsigned int n) { return 0; }};class B : public A{public: virtual unsigned int Sum(unsigned int n) { return Array[!!n]->Sum(n - 1) + n; }};int Sum_Solution2(int n){ A a; B b; Array[0] = &a; Array[1] = &b; int value = Array[1]->Sum(n);//当n大于0时,总是执行B类中的sum,直到n=0,!!n=0,然后变成了Array[0]->Sum(n),即A类,注意Array设成全局变量 return value;}// ====================方法三====================//使用函数指针typedef unsigned int(*fun)(unsigned int);unsigned int Solution3_Teminator(unsigned int n){ return 0;}unsigned int Sum_Solution3(unsigned int n){ static fun f[2] = { Solution3_Teminator, Sum_Solution3 };//静态成员,第一次调用时候建立 return n + f[!!n](n - 1);}// ====================方法四====================//使用模版类型template
struct Sum_Solution4{ enum Value { N = Sum_Solution4
::N + n };//Sum_Solution4
::N就是我们要的值};template <> struct Sum_Solution4<1>{ enum Value { N = 1 };};template <> struct Sum_Solution4<0>{ enum Value { N = 0 };};// ====================测试代码====================void Test(int n, int expected){ printf("Test for %d begins:\n", n); if (Sum_Solution1(n) == expected) printf("Solution1 passed.\n"); else printf("Solution1 failed.\n"); if (Sum_Solution2(n) == expected) printf("Solution2 passed.\n"); else printf("Solution2 failed.\n"); if (Sum_Solution3(n) == expected) printf("Solution3 passed.\n"); else printf("Solution3 failed.\n");}void Test1(){ const unsigned int number = 1; int expected = 1; Test(number, expected); if (Sum_Solution4
::N == expected) printf("Solution4 passed.\n"); else printf("Solution4 failed.\n");}void Test2(){ const unsigned int number = 5; int expected = 15; Test(number, expected); if (Sum_Solution4
::N == expected) printf("Solution4 passed.\n"); else printf("Solution4 failed.\n");}void Test3(){ const unsigned int number = 10; int expected = 55; Test(number, expected); if (Sum_Solution4
::N == expected) printf("Solution4 passed.\n"); else printf("Solution4 failed.\n");}void Test4(){ const unsigned int number = 0; int expected = 0; Test(number, expected); if (Sum_Solution4
::N == expected) printf("Solution4 passed.\n"); else printf("Solution4 failed.\n");}int main(int argc, char* argv[]){ Test1(); Test2(); Test3(); Test4(); system("pause"); return 0;}

 

转载于:https://www.cnblogs.com/CJT-blog/p/10551775.html

你可能感兴趣的文章
【AHOI2013复仇】从一道题来看DFS及其优化的一般步骤和数组分层问题【转】
查看>>
less 分页显示文件内容
查看>>
如何对数据按某列进行分层处理
查看>>
[Qt] this application failed to start because it could not find or load the Qt platform plugin
查看>>
Git Submodule管理项目子模块
查看>>
学会和同事相处的30原则
查看>>
NOJ——1568走走走走走啊走(超级入门DP)
查看>>
文件操作
查看>>
Python:GUI之tkinter学习笔记3事件绑定(转载自https://www.cnblogs.com/progor/p/8505599.html)...
查看>>
jquery基本选择器
查看>>
hdu 1010 dfs搜索
查看>>
搭建wamp环境,数据库基础知识
查看>>
android中DatePicker和TimePicker的使用
查看>>
SpringMVC源码剖析(四)- DispatcherServlet请求转发的实现
查看>>
Android中获取应用程序(包)的大小-----PackageManager的使用(二)
查看>>
Codeforces Gym 100513M M. Variable Shadowing 暴力
查看>>
浅谈 Mybatis中的 ${ } 和 #{ }的区别
查看>>
CNN 笔记
查看>>
版本更新
查看>>
SQL 单引号转义
查看>>