1 题目描述

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

2 思路和方法

  (1)递归,不能使用if等条件判断语句,可以使用&&逻辑运算符的短路特性实现。当n=0时,不进行后一个判断的计算,作为递归终止。

  (2)利用sizeof(a)计算bool数组的字节数,bool类型在C++中占一个字节。bool a = [n][n+1]; 因一共有n*(n+1)个1,下三角或者上三角,第一行:[1]和为1;第二行:[1][1] 和为2;第三行:[1][1][1]和为3,……。bool类型的数据a,sizeof(a)=n*(n+1),所以1+2+3+...+n=sizeof(a)=n*(n+1)/2,或者是sizeof(a)>>1。

3 C++核心代码

 #include<iostream>

 #include<vector>

 using namespace std;

 class Solution {
public:
int Sum_Solution(int n) {
int sum = n;
sum && (sum += Sum_Solution(n - ));// 利用前一个判断短路;当n=0时,不进行后一个判断的计算,作为递归终止
return sum;
}
}; int main()
{
Solution a; int res = a.Sum_Solution();
cout << "result = " << res << endl;
system("pause");
return ;
}
 class Solution {
public:
int Sum_Solution(int n) {
bool a[n][n+];
return sizeof(a)>>;
}
};

参考资料

https://blog.csdn.net/feng_zhiyu/article/details/82112248

剑指offer47:位运算+递归。求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。的更多相关文章

  1. 求1+2+…+n,要求不能使用乘除法、for、while、if、else、s witch、case 等关键字以及条件判断语句(A?B:C)和不用循环/goto/递归输出1~100的10种写法

    来源:据说是某一年某个公司的面试题 题目:求1+2+…+n, 要求不能使用乘除法.for.while.if.else.s witch.case 等关键字以及条件判断语句(A?B:C) 分析:这题本来很 ...

  2. C语言奇思妙想:求1+2+…+n,要求不能使用乘除法、for、while、if、else、s witch、case 等关键字以及条件判断语句(A?B:C)

    来源:据说是某一年某个公司的面试题 题目:求1+2+…+n, 要求不能使用乘除法.for.while.if.else.s witch.case 等关键字以及条件判断语句(A?B:C) 分析:这题本来很 ...

  3. 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)

    代码如下: public int Sum_Solution(int n) { int temp = n; boolean b = (temp>0)&&(temp += Sum_S ...

  4. 求1+2+3+...+n的值,要求不能使用乘除法,for、while、if、else、switch、case、等关键字及条件判断语句(JAVA)

    采用递归和三目表达式注意红色字体一定不能写成n-- 1 package com.hunag; public class Sum { static int sum; public static int ...

  5. 题目: 求1+2+...+n,要求不使用乘除发、for、while、if、else、switch、case、等关键字以及条件判断语句(A?B:C)

    #include <iostream> using namespace std; int add_(int a,int b){ return 0; } int Add(int i,bool ...

  6. 剑指Offer - 九度1506 - 求1+2+3+...+n

    剑指Offer - 九度1506 - 求1+2+3+...+n2013-11-29 19:22 题目描述: 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switc ...

  7. 【剑指Offer】47、求1+2+3+4+···+n

      题目描述:   求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C).   解题思路:   本题本身没有太多 ...

  8. 剑指offer(47)求1+2+3+...+n

    题目描述 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 题目分析 不能用乘除也就不能用公示了,并且不能 ...

  9. 剑指offer四十七之求1+2+3+...+n

    一.题目 求1+2+3+...+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字及条件判断语句(A?B:C). 二.思路 1.需利用逻辑与的短路特性实现递归终 ...

随机推荐

  1. input输入框限制只能输入数字

    js: function onlyNumber(event){     var keyCode = event.keyCode;     if((keyCode<48&&keyC ...

  2. JavaWeb_(SpringMVC框架)SpringMVC入门

    Spring MVC又叫SpringWebMVC是一个轻量级的基于请求响应的表现层框架.它是Spring框架的一部分.SpringMVC与Struts2都可以替代原始Servlet技术. Spring ...

  3. git reset 版本回退操作

    1 git回退命令 git reset --hard GIT_HEAD   GIT_HEAD是你具体要回退的分支: 如图:   注:  查询GIT_HEAD可以通过两个命令:git log 获取未删除 ...

  4. Vue 的基本认识

    1.1.1.  官网 1) 英文官网: https://vuejs.org/ 2) 中文官网: https://cn.vuejs.org/ 1.1.2.  介绍描述 1) 渐进式 JavaScript ...

  5. APP相关测试工具

    名称 描述   性能检测工具 用于对插件CPU.内存.闪退进行测试   接口测试工具 用于对插件本版本内的接口进行上线前的结构检测 自动比对差异   monkey测试工具 对主软件进行稳定性测试   ...

  6. csp-s模拟99

    考前10天了... 昨天晚上真的不清醒,什么也码不对,心态爆炸. T1调了一个多小时没出来,T2因为少了一出q.pop()没A掉,T3随便写了几个sort竟然A了.十分懵逼. 最后20分钟想调T1,结 ...

  7. Linux 操作memcache命令行

    telnet 127.0.0.1 11211 连接 memcache stats 查看 memcache 状态 状态说明: pid memcache服务器的进程ID uptime 服务器已经运行的秒数 ...

  8. PHP学习之观察者模式

    <?php //观察者模式涉及到两个类 //男人类 和女朋友类 //男人类对象小明, 女朋友类对象小花.小丽 class Man { //用了存放观察者 protected $observers ...

  9. --thunder-lock is available since uWSGI 1.4.6 but never got documentation (of any kind)

    --thunder-lock is available since uWSGI 1.4.6 but never got documentation (of any kind) Serializing ...

  10. uWSGI Apache 处理 惊群效应的方式 现代的内核

    Serializing accept(), AKA Thundering Herd, AKA the Zeeg Problem — uWSGI 2.0 documentationhttps://uws ...