JZ-C-46
Posted 回看欧洲
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JZ-C-46相关的知识,希望对你有一定的参考价值。
剑指offer第四十六题:求1+2+3+...+n:要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)
1 //============================================================================ 2 // Name : JZ-C-46.cpp 3 // Author : Laughing_Lz 4 // Version : 5 // Copyright : All Right Reserved 6 // Description : 求1+2+3+...+n:要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C) 7 //============================================================================ 8 9 #include <iostream> 10 #include <stdio.h> 11 using namespace std; 12 // ====================方法一:利用构造函数求解==================== 13 class Temp { 14 public: 15 Temp() { 16 ++N; 17 Sum += N; 18 } 19 20 static void Reset() { //注意这里是静态方法 21 N = 0; 22 Sum = 0; 23 } 24 static unsigned int GetSum() { //注意这里是静态方法 25 return Sum; 26 } 27 28 private: 29 static unsigned int N; //注意这里是静态变量 30 static unsigned int Sum; //注意这里是静态变量 31 }; 32 33 unsigned int Temp::N = 0; 34 unsigned int Temp::Sum = 0; 35 36 unsigned int Sum_Solution1(unsigned int n) { 37 Temp::Reset(); 38 39 Temp *a = new Temp[n]; //这里创建的是对象数组 40 delete[] a; 41 a = NULL; 42 43 return Temp::GetSum(); 44 } 45 46 // ====================方法二:利用虚函数求解==================== 47 /** 48 * 一个函数充当递归函数的角色,另一个函数处理终止递归的情况,因此使用布尔变量,若n不为0,!!n为1;若n为0,!!n为0; 49 */ 50 class A; 51 A* Array[2]; 52 53 class A { 54 public: 55 virtual unsigned int Sum(unsigned int n) { 56 return 0; 57 } 58 }; 59 60 class B: public A { 61 public: 62 virtual unsigned int Sum(unsigned int n) { 63 return Array[!!n]->Sum(n - 1) + n; 64 } 65 }; 66 67 int Sum_Solution2(int n) { 68 A a; 69 B b; 70 Array[0] = &a; //递归结束,调用的为父类Sum方法 71 Array[1] = &b; 72 73 int value = Array[1]->Sum(n); 74 75 return value; 76 } 77 78 // ====================方法三:利用函数指针求解==================== 79 typedef unsigned int (*fun)(unsigned int); //函数指针定义 80 81 unsigned int Solution3_Teminator(unsigned int n) { 82 return 0; 83 } 84 85 unsigned int Sum_Solution3(unsigned int n) { 86 static fun f[2] = { Solution3_Teminator, Sum_Solution3 }; 87 return n + f[!!n](n - 1); 88 } 89 90 // ====================方法四:利用模板类型求解==================== 91 template<unsigned int n> struct Sum_Solution4 { 92 enum Value {//enum:枚举类型 93 N = Sum_Solution4<n - 1>::N + n 94 }; 95 }; 96 97 template<> struct Sum_Solution4<1> { 98 enum Value { 99 N = 1 100 }; 101 }; 102 103 template<> struct Sum_Solution4<0> { 104 enum Value { 105 N = 0 106 }; 107 }; 108 109 // ====================测试代码==================== 110 void Test(int n, int expected) { 111 printf("Test for %d begins:\n", n); 112 113 if (Sum_Solution1(n) == expected) 114 printf("Solution1 passed.\n"); 115 else 116 printf("Solution1 failed.\n"); 117 118 if (Sum_Solution2(n) == expected) 119 printf("Solution2 passed.\n"); 120 else 121 printf("Solution2 failed.\n"); 122 123 if (Sum_Solution3(n) == expected) 124 printf("Solution3 passed.\n"); 125 else 126 printf("Solution3 failed.\n"); 127 } 128 129 void Test1() { 130 const unsigned int number = 1; 131 int expected = 1; 132 Test(number, expected); 133 if (Sum_Solution4<number>::N == expected) 134 printf("Solution4 passed.\n"); 135 else 136 printf("Solution4 failed.\n"); 137 } 138 139 void Test2() { 140 const unsigned int number = 5; 141 int expected = 15; 142 Test(number, expected); 143 if (Sum_Solution4<number>::N == expected) 144 printf("Solution4 passed.\n"); 145 else 146 printf("Solution4 failed.\n"); 147 } 148 149 void Test3() { 150 const unsigned int number = 10; 151 int expected = 55; 152 Test(number, expected); 153 if (Sum_Solution4<number>::N == expected) 154 printf("Solution4 passed.\n"); 155 else 156 printf("Solution4 failed.\n"); 157 } 158 159 void Test4() { 160 const unsigned int number = 0; 161 int expected = 0; 162 Test(number, expected); 163 if (Sum_Solution4<number>::N == expected) 164 printf("Solution4 passed.\n"); 165 else 166 printf("Solution4 failed.\n"); 167 } 168 169 int main(int argc, char** argvb) { 170 Test1(); 171 Test2(); 172 Test3(); 173 Test4(); 174 175 return 0; 176 }
以上是关于JZ-C-46的主要内容,如果未能解决你的问题,请参考以下文章