《剑指offer》第六十四题(求1+2+…+n)
// 面试题64:求1+2+…+n
// 题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case
// 等关键字及条件判断语句(A?B:C)。 #include <iostream> // ====================方法一====================
//使用构造函数
class Temp
{
public:
Temp() { ++N; Sum += N; } static void Reset() { N = ; Sum = ; }
static unsigned int GetSum() { return Sum; } private:
static unsigned int N;
static unsigned int Sum;//静态成员,所有实例共享
}; unsigned int Temp::N = ;
unsigned int Temp::Sum = ; unsigned int Sum_Solution1(unsigned int n)
{
Temp::Reset(); Temp *a = new Temp[n];//建立n次,实现sum加和
delete[]a;
a = NULL; return Temp::GetSum();
} // ====================方法二====================
//使用虚函数
class A;
A* Array[]; class A
{
public:
virtual unsigned int Sum(unsigned int n)
{
return ;
}
}; class B : public A
{
public:
virtual unsigned int Sum(unsigned int n)
{
return Array[!!n]->Sum(n - ) + n;
}
}; int Sum_Solution2(int n)
{
A a;
B b;
Array[] = &a;
Array[] = &b; int value = Array[]->Sum(n);//当n大于0时,总是执行B类中的sum,直到n=0,!!n=0,然后变成了Array[0]->Sum(n),即A类,注意Array设成全局变量 return value;
} // ====================方法三====================
//使用函数指针
typedef unsigned int(*fun)(unsigned int); unsigned int Solution3_Teminator(unsigned int n)
{
return ;
} unsigned int Sum_Solution3(unsigned int n)
{
static fun f[] = { Solution3_Teminator, Sum_Solution3 };//静态成员,第一次调用时候建立
return n + f[!!n](n - );
} // ====================方法四====================
//使用模版类型
template <unsigned int n> struct Sum_Solution4
{
enum Value { N = Sum_Solution4<n - >::N + n };//Sum_Solution4<number>::N就是我们要的值
}; template <> struct Sum_Solution4<>
{
enum Value { N = };
}; template <> struct Sum_Solution4<>
{
enum Value { N = };
}; // ====================测试代码====================
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 = ;
int expected = ;
Test(number, expected);
if (Sum_Solution4<number>::N == expected)
printf("Solution4 passed.\n");
else
printf("Solution4 failed.\n");
} void Test2()
{
const unsigned int number = ;
int expected = ;
Test(number, expected);
if (Sum_Solution4<number>::N == expected)
printf("Solution4 passed.\n");
else
printf("Solution4 failed.\n");
} void Test3()
{
const unsigned int number = ;
int expected = ;
Test(number, expected);
if (Sum_Solution4<number>::N == expected)
printf("Solution4 passed.\n");
else
printf("Solution4 failed.\n");
} void Test4()
{
const unsigned int number = ;
int expected = ;
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();
system("pause");
return ;
}
《剑指offer》第六十四题(求1+2+…+n)的更多相关文章
- 《剑指offer》第十四题(剪绳子)
// 面试题:剪绳子 // 题目:给你一根长度为n绳子,请把绳子剪成m段(m.n都是整数,n>1并且m≥1). // 每段的绳子的长度记为k[0].k[1].…….k[m].k[0]*k[1]* ...
- 《剑指offer》第二十四题(反转链表)
// 面试题24:反转链表 // 题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的 // 头结点. #include <iostream> #include &quo ...
- 剑指Offer(三十四):第一个只出现一次的字符
剑指Offer(三十四):第一个只出现一次的字符 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net ...
- 剑指Offer(二十四):二叉树中和为某一值的路径
剑指Offer(二十四):二叉树中和为某一值的路径 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...
- 《剑指offer》第十九题(正则表达式匹配)
// 面试题19:正则表达式匹配 // 题目:请实现一个函数用来匹配包含'.'和'*'的正则表达式.模式中的字符'.' // 表示任意一个字符,而'*'表示它前面的字符可以出现任意次(含0次).在本题 ...
- 《剑指offer》第二十九题(顺时针打印矩阵)
// 面试题29:顺时针打印矩阵 // 题目:输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字. #include <iostream> void PrintMatrixInC ...
- 《剑指offer》第二十八题(对称的二叉树)
// 面试题28:对称的二叉树 // 题目:请实现一个函数,用来判断一棵二叉树是不是对称的.如果一棵二叉树和 // 它的镜像一样,那么它是对称的. #include <iostream> ...
- 《剑指offer》第二十五题(合并两个排序的链表)
// 面试题25:合并两个排序的链表 // 题目:输入两个递增排序的链表,合并这两个链表并使新链表中的结点仍然是按 // 照递增排序的.例如输入图3.11中的链表1和链表2,则合并之后的升序链表如链 ...
- 《剑指offer》第二十二题(链表中倒数第k个结点)
// 面试题22:链表中倒数第k个结点 // 题目:输入一个链表,输出该链表中倒数第k个结点.为了符合大多数人的习惯, // 本题从1开始计数,即链表的尾结点是倒数第1个结点.例如一个链表有6个结点, ...
- 《剑指offer》第十八题(删除链表中重复的结点)
// 面试题18(二):删除链表中重复的结点 // 题目:在一个排序的链表中,如何删除重复的结点?例如,在图3.4(a)中重复 // 结点被删除之后,链表如图3.4(b)所示. #include &l ...
随机推荐
- USB设备被识别流程
源: USB设备被识别流程
- Tencent QQ现在就是一个十八层地狱下面的大恶魔-删除右键里的"通过QQ发送到"
都是流氓软件, 有人推荐装什么管家什么助手来清除, 那就是请走一个流氓又引进另外一个流氓. 下面的注册表项直接手工删除 32位系统: windows Registry Editor Version 5 ...
- JS截取字符串中数字
今天项目中需要在一个字符串中截取一个数字,然后数字参与运算.搜了一下,有好多好用的方式截取字符串. 1,使用parseInt() var str ="4500元"; var num ...
- 【题解】Luogu SP3267 DQUERY - D-query
原题传送门 这题和Luogu P1972 [SDOI2009]HH的项链很像,只是数据大小有些差别,题解 我博客里对莫队的介绍 我们在排序询问时,普通是这样qaq inline bool cmp(re ...
- ldap集成jira
jira默认支持ldap,通过管理员登录jira 点击 User Management --> User Directories --> Add Directory. 进行ldap配置: ...
- vue2.0子组件修改父组件props数据的值
从vue1.0升级至2.0之后 prop的.sync被去除 因此直接在子组件修改父组件的值是会报错的如下: 目的是为了阻止子组件影响父组件的数据那么在vue2.0之后 如何在子组件修改父组件props ...
- Codeforces 868C Qualification Rounds - 位运算
Snark and Philip are preparing the problemset for the upcoming pre-qualification round for semi-quar ...
- java基础篇之理解synchronized的用法
Java语言的关键字,当它用来修饰一个方法或者一个代码块的时候,能够保证在同一时刻最多只有一个线程执行该段代码. 一.当两个并发线程访问同一个对象object中的这个synchronized(this ...
- Android Studio报错view is not constrained
在活动上面创建了两个按钮,在Design上看上去是两个按钮分开的,run一下,按钮就重合在一起了,而且一直报错,这个时候再去看一下Design,两个按钮重在一块,只显示一个按钮.如下图: button ...
- 如何开启Intel HAXM功能
1. 启用BIOS中的Intel(R) Virtualization Technology选项 2.设置成功后,在控制台中输入sc query intelhaxm.出现下图即为成功 3. 启动andr ...