zoukankan      html  css  js  c++  java
  • ※剑指offer系列37:求1+2+3……+n

    2019-07-23

    11:44:01

    从这个题开始,我有些题目标题前面会加一个※号,以表示此题目比较重要,要多复习其中的知识点。

    这个题就1到n的加法,不能用乘数法、循环语句和条件语句。第一个方法就是把想要的计算放进构造函数,注意这里算累计和所以要一直更新值,因此一定要用静态成员函数。

     1 class Solution {
     2 public:
     3     Solution()
     4     {
     5         n++;
     6         sum += n;
     7     }
     8     static void reset()
     9     {
    10         n = 0;
    11         sum = 0;
    12     }
    13     static unsigned int sumfind()
    14     {
    15         return sum;
    16     }
    17     unsigned int Sum_Solution(int n) {
    18         Solution::reset();
    19         Solution *a = new Solution[n];
    20         delete[]a;
    21         a = NULL;
    22         return Solution::sumfind();
    23     }
    24 private:
    25     static unsigned int n;
    26     static unsigned int sum;
    27 
    28 };
    29 unsigned int Solution::n = 0;//静态数据成员必须初始化
    30 unsigned int Solution::sum = 0;

    第二种方法:用虚函数。其实用的思想还是递归,只不过以虚函数的形式呈现出来。

     1 #include<iostream>
     2 using namespace std;
     3 class Solution {
     4 public:
     5     virtual int Sum_Solution(int n) {
     6         return 0;
     7     }
     8 };
     9 Solution *arr[2];
    10 class Solution2: public Solution
    11  {
    12 public:
    13     virtual int Sum_Solution(int n) {
    14         return  arr[!!n]->Sum_Solution(n-1)+n;//f(n)=f(n-1)+n
    15     }
    16 };
    17 int main()
    18 {
    19     Solution a;
    20     Solution2 b;
    21     arr[0] = &a;
    22     arr[1] = &b;
    23     int val = arr[1]->Sum_Solution(5) ;
    24     cout << val<<endl;
    25     return 0;
    26 }

    第三种解法:使用函数指针的方法让指针选择相应的函数,还是递归的思路

     1 #include<iostream>
     2 using namespace std;
     3 typedef unsigned int (*fun)(unsigned int);//声明一个:返回值为指向unsigned int类型的指针类型,参数为unsigned int类型,的函数
     4 unsigned int so1(unsigned int n)//相当于递归终止条件
     5 {
     6     return 0;
     7 }
     8 unsigned int so2(unsigned int n)//相当于递归循环函数
     9 {
    10     static fun f[2] = { so1,so2 };//声明函数数组f,f的每一个元素是一个函数
    11     return f[!!n](n - 1) + n;
    12 }
    13 
    14 class Solution {
    15 public:
    16     unsigned int Sum_Solution(unsigned int n) {
    17         return so2(n);
    18     }
    19 };
    20 int main()
    21 {
    22     Solution so;
    23     cout << so.Sum_Solution(4) << endl;
    24     return 0;
    25 }

    第四个:使用模板的方法

     1 #include<iostream>
     2 using namespace std;
     3 template<unsigned int n> struct sum4
     4 {
     5     enum val{N= sum4<n-1>::N+n};
     6 };
     7 template<> struct sum4<1>
     8 {
     9     enum val { N =1 };
    10 };
    11 int main()
    12 {
    13     cout << sum4<100>::N << endl;
    14     return 0;
    15 }

    这段代码没写注释因为模板我也不是很熟悉。这一个题虽然看起来不难,不过涉及到的这些知识点我确实以前掌握的不熟悉。

  • 相关阅读:
    js在html中的加载执行顺序
    外部JS的阻塞下载
    mysql 中文字段排序
    PHP 多维数组排序 array_multisort()
    最简单的Linux下apache+mysql+php安装
    [TJOI2008] 彩灯 (线性基)
    [洛谷P2257] YY的GCD (莫比乌斯反演)
    [SDOI2015] 约数个数和 (莫比乌斯反演)
    [POI2007] ZAP-Queries (莫比乌斯反演)
    [NOI2003] 文本编辑器 (splay)
  • 原文地址:https://www.cnblogs.com/neverland0718/p/11230403.html
Copyright © 2011-2022 走看看