码迷,mamicode.com
首页 > 其他好文 > 详细

《剑指offer》第六十四题:求1+2+…+n

时间:2020-04-13 22:44:28      阅读:73      评论:0      收藏:0      [点我收藏+]

标签:code   构造   argv   show   offer   面试题   初始化   opened   方法   

// 面试题64:求1+2+…+n
// 题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case
// 等关键字及条件判断语句(A?B:C)。

#include <cstdio>

// ====================方法一====================
// 利用构造函数求解
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个实例, 则构造函数会被调用n次
    delete[] a;
    a = nullptr;

    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)  //充当递归函数
    {
        // !!n 非零值为true 零值为false
        return Array[!!n]->Sum(n - 1) + n;
    }
};

int Sum_Solution2(int n)
{
    A a;
    B b;
    Array[0] = &a;
    Array[1] = &b;

    //当n大于0时, 总是执行B中sum, 直到n==0
    int value = Array[1]->Sum(n);

    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 }; //两个函数

    //当n大于0时调用函数Sum_Solution3
    //当n==0时调用函数Solution3_Teminator
    return n + f[!!n](n - 1); 
}

// ====================方法四====================
// 利用模板类型求解
template <unsigned int n> struct Sum_Solution4
{
    //实现方法类似
    enum Value { N = Sum_Solution4<n - 1>::N + 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<number>::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<number>::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<number>::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<number>::N == expected)
        printf("Solution4 passed.\n");
    else
        printf("Solution4 failed.\n");
}

int main(int argc, char* argv[])
{
    Test1();
    Test2();
    Test3();
    Test4();

    return 0;
}
测试代码

分析:通过各种方法实现递归操作。

技术图片
typedef int (*fun)( int );
class Solution {
public:
    
    static int Solution3_Teminator(int n) { return 0; }
    
    static int Sum_Solution(int n) {
        
        static fun f[2] = {Solution3_Teminator, Sum_Solution};
        return n + f[!!n](n - 1);
    }
};
牛客网提交代码

 

《剑指offer》第六十四题:求1+2+…+n

标签:code   构造   argv   show   offer   面试题   初始化   opened   方法   

原文地址:https://www.cnblogs.com/ZSY-blog/p/12694308.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!