首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
求1+2+3+...+n
[编程题]求1+2+3+...+n
热度指数:376118
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 64M,其他语言128M
算法知识视频讲解
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
数据范围:
进阶: 空间复杂度
,时间复杂度
示例1
输入
5
输出
15
示例2
输入
1
输出
1
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(2531)
分享
提交结果有问题?
1087个回答
138篇题解
开通博客
牛客题解官
发表于 2020-06-01 16:31:55
精华题解
题目的主要信息: 计算1+2+3+...+n1+2+3+...+n1+2+3+...+n 不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C) 举一反三: 学习完本题的思路你可以解决如下题目: JZ65. 不用加减乘除做加法 JZ15.
展开全文
漫漫云天自翱翔
发表于 2021-06-22 10:18:27
精华题解
这是一题考虑思维广度的题目:1、不能使用判断语句,可以从逻辑运算符考虑2、利用类型强转,函数指针3、模板元编程4、放在C++类中考虑6、将1+2+3+...+n的结果开辟为动态内存,内存计算题解一:递归+逻辑运算符前置知识:位运算|、&与逻辑运算符&&、||的区别。1、&am
展开全文
鸠摩罗什
发表于 2021-07-01 22:30:28
精华题解
描述 求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。 示例1输入:5返回值:15 方法一:递归求解核心思
展开全文
蒙牛麦片
发表于 2021-06-24 09:30:14
精华题解
JZ47 求1+2+3+...+n 题意分析: 求连加到n的和。 示例输入:5返回:1+2+3+4+5=15 题解一(高斯公式): 。 int Sum_Solution(int n) {// return n * (n + 1) / 2; } 值得注意的是,该解法不满足题意要求 题解二(循环
展开全文
FYZ~
发表于 2019-08-09 19:35:20
题目:求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。思路:这是一个等差数列,sum=(a1+an)n/2=>(1+n)n/2=>(n+n^2)/2;Math.pow(a,b)表示a^b;右移一
展开全文
法拉利201903231900848
发表于 2019-07-30 17:11:02
class Solution { public: int Sum_Solution(int n) { int sum=n; &n
展开全文
凉风起天末
发表于 2020-02-02 22:42:03
O(n)伤不起:模拟二进制运算,绝对不涉及乘法,复杂度为O(logN) 2进制乘法原理与10进制类似,但是2进制更加简单,因为2进制非0即1,所以乘数m的一个二进制位与被乘数n相乘的结果要么是0要么是n本身,只要在实际计算过程中根据m的某个进制位所在的位置对n进行移位就可以了。 当然,这里讨论的都是
展开全文
Ironxin
发表于 2020-03-04 21:08:27
题目:求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。先给自己普及大佬们口中的短路求值是什么。作为"&&"和"||"操作符的操作数表达式,这些表达式在进行求值时,只要最终的结果已经可以确定是
展开全文
Jackson_888
发表于 2019-12-30 23:25:15
一行代码简便解法,直接递归短路代替if语句,当n为0或者负数时,自动忽略之后的递归 # -*- coding:utf-8 -*- class Solution: def Sum_Solution(self, n): # write code here retu
展开全文
扣得皮
发表于 2020-01-07 20:15:30
当n=0时,sum=0,&&后面的就不会执行了,直接返回sum=0 public class Solution { public int Sum_Solution(int n) { int sum=n; //n=1时直接返回1
展开全文
李东蔚
发表于 2021-09-25 14:56:13
import java.util.*; 应该说看到这种只能加减,不能乘除,也不能循环,不能使用判断等等这些条件就知道了 肯定要用位运算。 左移一位相当于乘以2,右移一位相当于除以2,这是基本的常识。记不起来可以举一个0010的例子移动一下1 然后把(首项+末项)*项数/2进一步化简,即为(n+n²)
展开全文
好想找工作
发表于 2020-06-16 01:24:18
解法一:或 public class Solution { public int Sum_Solution(int n) { boolean flag=(n==1)||((n+=Sum_Solution(n-1))>0); return n; }
展开全文
乐意999
发表于 2022-05-25 16:24:49
">#include<stdlib.h> class add{ public: add(){ i+=j; j++; } static int i,j; }; int add:: i=0; int add:: j=1; cl
展开全文
橙子爱吃桃子
发表于 2020-05-28 00:45:31
C++简单代码/符合题意/3行: class Solution { public: int Sum_Solution(int n) { n && (n += Sum_Solution(n - 1)); //A&&B 1.A为true,则计算并返
展开全文
问题信息
基础数学
难度:
1087条回答
2531收藏
132019浏览
热门推荐
通过挑战的用户
查看代码
Elastic...
2023-02-25 22:19:40
郁闷的马里奥风度翩翩
2023-02-20 19:38:23
Dice_L
2022-12-16 10:17:06
幻影20181...
2022-11-11 10:30:59
干脆面la
2022-10-18 10:36:06
相关试题
车站建造问题
基础数学
评论
(40)
线段树编号问题
基础数学
评论
(2)
牛牛的超市
动态规划
基础数学
评论
(5)
编译方法中,动态存储分配的含义是:()
编译和体系结构
评论
(2)
来自
乐视2017秋招开发工程...
闪速存储器能提供高性能、低功耗、字...
编程基础
评论
(1)
求1+2+3+...+n
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class Solution { public int Sum_Solution(int n) { } }
class Solution { public: int Sum_Solution(int n) { } };
# -*- coding:utf-8 -*- class Solution: def Sum_Solution(self, n): # write code here
class Solution { public int Sum_Solution(int n) { // write code here } }
function Sum_Solution(n) { // write code here } module.exports = { Sum_Solution : Sum_Solution };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param n int整型 # @return int整型 # class Solution: def Sum_Solution(self , n: int) -> int: # write code here
package main /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param n int整型 * @return int整型 */ func Sum_Solution( n int ) int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param n int整型 * @return int整型 */ int Sum_Solution(int n ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # @param n int整型 # @return int整型 # class Solution def Sum_Solution(n) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param n int整型 * @return int整型 */ def Sum_Solution(n: Int): Int = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param n int整型 * @return int整型 */ fun Sum_Solution(n: Int): Int { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param n int整型 * @return int整型 */ public int Sum_Solution (int n) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param n int整型 * @return int整型 */ export function Sum_Solution(n: number): number { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param n int整型 * @return int整型 */ func Sum_Solution ( _ n: Int) -> Int { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param n int整型 * @return int整型 */ pub fn Sum_Solution(&self, n: i32) -> i32 { // write code here } }
5
15
1
1