• 大小: 308KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-19
  • 语言: 其他
  • 标签: 面试题  

资源简介

本书从写代码的规范性、完整性、鲁棒性方面,全面展示一道简单面试题,需要考虑的因素,是基于测试编程面试指南的范本,值得推荐………… 《剑指Offer——名企面试官精讲典型编程题》附带的源代码,内含解决50余道编程面试题的完整源代码(含单元测试代码)。打开InterviewQuestions.sln需要Visual Studio 2008或者更新的版本。项目名称前面的数字和《剑指Offer——名企面试官精讲典型编程题》中面试题的编号对应。 将该书的源代码上传到这里,希望对大家有用…………

资源截图

代码片段和文件信息

// Accumulate.cpp : Defines the entry point for the console application.
//

// 《剑指Offer——名企面试官精讲典型编程题》代码
// 著作权所有者:何海涛

#include “stdafx.h“

// ====================方法一====================
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();
}

// ====================方法二====================
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);

    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};
};

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“);
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2011-10-24 11:38  InterviewQuestions\Accumulate\
     文件        3414  2011-09-30 15:25  InterviewQuestions\Accumulate\Accumulate.cpp
     文件        4528  2011-06-08 17:26  InterviewQuestions\Accumulate\Accumulate.vcproj
     文件        1417  2011-10-24 11:50  InterviewQuestions\Accumulate\Accumulate.vcproj.HAIHE-WIN7.Harry He.user
     文件        1320  2011-04-19 07:36  InterviewQuestions\Accumulate\ReadMe.txt
     文件         297  2011-04-19 07:36  InterviewQuestions\Accumulate\stdafx.cpp
     文件         320  2011-04-19 07:36  InterviewQuestions\Accumulate\stdafx.h
     文件         765  2011-04-19 07:36  InterviewQuestions\Accumulate\targetver.h
     目录           0  2011-10-24 11:38  InterviewQuestions\AddTwoNumbers\
     文件         932  2011-09-30 15:25  InterviewQuestions\AddTwoNumbers\AddTwoNumbers.cpp
     文件        4537  2011-06-08 17:26  InterviewQuestions\AddTwoNumbers\AddTwoNumbers.vcproj
     文件        1417  2011-10-24 11:50  InterviewQuestions\AddTwoNumbers\AddTwoNumbers.vcproj.HAIHE-WIN7.Harry He.user
     文件        1338  2011-04-18 16:07  InterviewQuestions\AddTwoNumbers\ReadMe.txt
     文件         300  2011-04-18 16:07  InterviewQuestions\AddTwoNumbers\stdafx.cpp
     文件         320  2011-04-18 16:07  InterviewQuestions\AddTwoNumbers\stdafx.h
     文件         765  2011-04-18 16:07  InterviewQuestions\AddTwoNumbers\targetver.h
     目录           0  2011-10-24 11:38  InterviewQuestions\AssignmentOperator\
     文件        2455  2011-09-30 15:25  InterviewQuestions\AssignmentOperator\AssignmentOperator.cpp
     文件        4552  2011-06-08 17:40  InterviewQuestions\AssignmentOperator\AssignmentOperator.vcproj
     文件        1417  2011-10-24 11:50  InterviewQuestions\AssignmentOperator\AssignmentOperator.vcproj.HAIHE-WIN7.Harry He.user
     文件        1368  2011-06-08 17:31  InterviewQuestions\AssignmentOperator\ReadMe.txt
     文件         305  2011-06-08 17:31  InterviewQuestions\AssignmentOperator\stdafx.cpp
     文件         320  2011-06-08 17:31  InterviewQuestions\AssignmentOperator\stdafx.h
     文件         765  2011-06-08 17:31  InterviewQuestions\AssignmentOperator\targetver.h
     目录           0  2011-10-24 11:38  InterviewQuestions\BalancedBinaryTree\
     文件        6081  2011-09-30 15:25  InterviewQuestions\BalancedBinaryTree\BalancedBinaryTree.cpp
     文件        4554  2011-06-08 17:26  InterviewQuestions\BalancedBinaryTree\BalancedBinaryTree.vcproj
     文件        1417  2011-10-24 11:50  InterviewQuestions\BalancedBinaryTree\BalancedBinaryTree.vcproj.HAIHE-WIN7.Harry He.user
     文件        1368  2011-05-27 11:17  InterviewQuestions\BalancedBinaryTree\ReadMe.txt
     文件         305  2011-05-27 11:17  InterviewQuestions\BalancedBinaryTree\stdafx.cpp
     文件         320  2011-05-27 11:17  InterviewQuestions\BalancedBinaryTree\stdafx.h
............此处省略419个文件信息

评论

共有 条评论