浅谈《剑指offer》原题:不使用条件、循环语句求1+2+……+n
来源:互联网 发布:excel怎么导入数据 编辑:程序博客网 时间:2024/05/18 01:08
转载自:浅谈《剑指offer》原题:求1+2+……+n
如侵犯您的版权,请联系:windeal12@qq.com
《剑指offer》上的一道原题,求1+2+……+n,要求不能使用乘除法,for、while、if、else、switch、case等关键字以及条件判断语句(a?b:c)。
第一次看到这道题大约有一年的时间了,在霸笔网易的时候,当时我就晕了。。。心想这是神马东西,后来发现这是原题!!然后后悔自己没看过书了。。。
《剑指offer》上给出了不错的解法,但是这里有个解法更巧妙,虽然技术含量不高,但是可以参考,这就是《程序员面试笔试宝典》中所给出的答案。
解法一:利用宏定义求解
假设n=1000。相信看到答案的你们都会笑了。
- #include <stdio.h>
- #define L sum+=(++n);
- #define K L;L;L;L;L;L;L;L;L;L;
- #define J K;K;K;K;K;K;K;K;K;K;
- #define H J;J;J;J;J;J;J;J;J;J;
- int main()
- {
- int sum = 0;
- int n = 0;
- H;
- printf("%d\n", sum);
- return 0;
- }
解法二:利用构造函数
实际上就是利用类里面的静态成员变量,然后通过构造函数去调用。其实对于c++掌握熟练的人来说,也可以很轻松的明白。
- #include <stdio.h>
- #include <iostream>
- using namespace std;
- 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];
- delete []a;
- a = NULL;
- return Temp::GetSum();
- }
- int main()
- {
- printf("%d\n", Sum_Solution1(1000));
- return 0;
- }
解法三:利用虚函数求解
这也利用了多态的性质,特别巧妙。
- #include <stdio.h>
- #include <iostream>
- using namespace std;
- 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_Solutiion2(int n)
- {
- A a;
- B b;
- Array[0] = &a;
- Array[1] = &b;
- int value = Array[1]->Sum(n);
- return value;
- }
- int main()
- {
- printf("%d\n", Sum_Solutiion2(1000));
- return 0;
- }
解法四:利用函数指针求解
在纯C语言的编程环境中,我们不能使用虚函数,这时候函数指针就可以达到一样的效果了!
- #include <stdio.h>
- typedef unsigned int (*fun)(unsigned int);
- unsigned int Sum_Solutiion3_Teminator(unsigned int n)
- {
- return 0;
- }
- unsigned int Sum_Solutiion3(unsigned int n)
- {
- static fun f[2] = {Sum_Solutiion3_Teminator, Sum_Solutiion3};
- return n + f[!!n](n-1);
- }
- int main()
- {
- printf("%d\n", Sum_Solutiion3(1000));
- return 0;
- }
解法五:利用模板类型来求解
本质都是多态。可惜不是所有编译器都支持,VC++6.0就不支持。。
- #include <stdio.h>
- #include <iostream>
- template <unsigned int n>struct Sum_Solutiion4
- {
- enum Value{N = Sum_Solutiion4<N-1>::N + n};
- };
- template <> struct Sum_Solutiion4<1>
- {
- enum Value{N = 1};
- };
- int main()
- {
- printf("%d\n", Sum_Solutiion4<1000>::N);
- return 0;
- }
感觉这道题这些方法都能理解的话,说明c++水平已经不错了,我当时第一次看见都是云里雾里的!
0 0
- 浅谈《剑指offer》原题:不使用条件、循环语句求1+2+……+n
- 浅谈《剑指offer》原题:求1+2+……+n
- 《剑指offer》上的一道原题,求1+2+……+n,要求不能使用乘除法,for、while、if、else、switch、case等关键字以及条件判断语句
- 求1+2……+n,不能用循环和乘除和条件判断语句
- 剑指offer 面试题46 求 1+2+3+...+n (不使用乘除以及条件判断语句求前 n 项正整数的和)
- 剑指offer 面试题46:求1+2+3+...+n(不能使用乘除法、循环语句及条件判断语句) 题解
- 剑指offer 求1+2+3+...+n,要求不能使用乘除法循环语句等
- 剑指Offer——不使用循环,判断,选择语句实现1+2+3....+n
- 剑指offer面试题46求1+2+..+n不能用乘除法和选择循环,多种发散思维方法
- 剑指offer---不使用循环条件判断等实现求和
- 求1+2+…+n, 要求不能使用乘除法、for、while、if、else、switch、case和条件语句
- 求1+2+…+n, 要求不能使用乘除法、for、while、if、else、switch、case和条件语句
- 剑指offer 46---实现1+2+3...+n,要求不能使用乘除法、循环、条件判断、选择相关的关键字
- 剑指offer:求1+2+3+……n
- 剑指offer(2)--求1+2+…+n
- 剑指offer-面试题46:求1+2+…+n
- 剑指offer-求1+2+3+…+n
- 每天学习一算法系列(12) (求1+2+…+n,不能使用乘除法,for、while、if 、else、switch、case 等关键字以及条件判断语句)
- 是打过电话费赶紧干活空间看
- HttpClient 4.3.* 上传带中文文件名文件文件名乱码问题的解决 - kcher
- 第十个负担和法规及和个看
- 但是规划发过几个好看
- html5设置头像为圆形的
- 浅谈《剑指offer》原题:不使用条件、循环语句求1+2+……+n
- UBOOT之源码分析(X4412)——main_loop函数分析
- canvas 的save()和restore()方法
- 把ul里面的li值包含在div里面的解决方法
- Web of Knowledge, Web of Science, SCIE 三者之间的关系
- 【LeetCode题目记录-10】已排序的数组去除重复元素
- Android Intent 调用其他应用 setComponent
- GDB使用
- 验证码 verifycode 留存可用