求1+2+3+...+n,要求不能使用乘除法,for,while,if,else,switch,case等关键字以及条件判断语句
来源:互联网 发布:linux ulimit 编辑:程序博客网 时间:2024/05/29 16:56
方法一:利用构造函数和静态数据成员#include <iostream>using namespace std;class Temp{public:Temp(){++N;Sum+=N;}static void Reset(){N=0;Sum=0;}static int GetSum(){return Sum;}private:static int N;static int Sum;};int Temp::N=0;int Temp::Sum=0;int solution_Sum(int n){Temp::Reset();Temp *a=new Temp[n];delete []a;a=0;return Temp::GetSum();}int main(){cout<<solution_Sum(100)<<endl;return 0;}方法二:利用虚函数#include <iostream>using namespace std;class A;A* Array[2];class A{public:virtual int Sum(int n){return 0;}};class B:public A{public:virtual int Sum(int n){return Array[!!n]->Sum(n-1)+n;}};int solution2_Sum(int n){A a;B b;Array[0]=&a;Array[1]=&b;int value=Array[1]->Sum(n);return value;}int main(){cout<<solution2_Sum(100)<<endl;return 0;}利用函数指针#include <iostream>using namespace std;typedef int (*fun)(int);int solution_f1(int i){return 0;}int solution_f2(int i){fun f[2]={solution_f1, solution_f2};return i+f[!!i](i-1);}void main(){cout<<solution_f2(100)<<endl;} 三。利用&&的短路特性#include <stdio.h>#include <stdlib.h>#include <string.h>int add_fun(int n, int &sum){n && add_fun(n-1, sum);return (sum+=n);}int main(){int sum=0;int n=100;printf("1+2+3+...+n=%d\n",add_fun(n, sum));return 0;}
0 0
- 求1+2+3+...+n,要求不能使用乘除法,for,while,if,else,switch,case等关键字以及条件判断语句
- 求1+2+3+...+n,要求不能使用乘除法,for,while,if,else,switch,case等关键字以及条件判断语句
- 求1+2+3+...+n,要求不能使用乘除法,for,while,if,else,switch,case等关键字以及条件判断语句
- 求1+2+3+...+n,要求不能使用乘除法,for,while,if,else,switch,case等关键字以及条件判断语句
- 题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。
- 求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case 等关键字以及条件判断语句
- 求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)
- 求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)
- 题目:求1+2+…+n, 要求不能使用乘除法、for、while、if、else、switch、case 等关键字以及条件判断语句
- 题目:求1+2+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A?B:C)。
- 求1+2+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字以及条件判断语句(A? B:C)
- 求1+2+…+n, 要求不能使用乘除法、for、while、if、else、switch、case等关键字 以及条件判断语句(A?B:C)
- 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
- 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。Java实现
- 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)
- 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
- 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)
- 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
- 关于动态网页的POST和GET的选择问题
- iOS:hidesBottomBarWhenPushed的正确用法
- JAVA 学习文章汇集
- HDU 2086
- XP系统罕见操作技巧
- 求1+2+3+...+n,要求不能使用乘除法,for,while,if,else,switch,case等关键字以及条件判断语句
- 2014巴西世界杯尘埃落定
- 深入浅出桥梁模式
- deque的使用
- 2014大众点评Hackathon参赛感想
- Spring mvc 中使用 kaptcha 验证码
- 应用开发博客收集
- C_C++变量命名规则
- 关于磁盘管理小结【收集中】