当前位置:网站首页>【牛客】求1+2+3+...+n
【牛客】求1+2+3+...+n
2022-07-28 04:09:00 【Patrick star`】
求1+2+3+...+n_牛客题霸_牛客网 (nowcoder.com)
题目:
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
数据范围: 0<n≤200
进阶: 空间复杂度O(1) ,时间复杂度 O(n)
思路:
充分利用C++特性,构造对象构造函数会自动调用的特点,让求和在构造函数中进行完成
代码:
class sum
{
public:
sum()
{
_sum+=_i;
_i++;
}
static int get_sum()
{
return _sum;
}
private:
int static _i;
int static _sum;
};
class Solution {
public:
int Sum_Solution(int n)
{
sum arr[n];
return sum::get_sum();
}
};
int sum:: _i = 1;
int sum:: _sum = 0;边栏推荐
- 金仓数据库KingbaseES安全指南--4 数据访问保护
- Read Plato farm's eplato and the reason for its high premium
- Build an "industrial brain" and improve the park's operation, management and service capabilities with "digitalization"!
- CV2. Threshold(), CV2. Findcontours(), CV2. Findcontours image contour processing
- Which stock exchange has the lowest commission? Is it safe to open an account on your mobile phone
- [reach out to Party welfare] the easiest way to scan the H5 page in wechat
- From Clickhouse to Snowflake: MPP query layer
- 一名合格的软件测试工程师,应该具备哪些技术能力?
- H265/HEVC名词解释-- CTU,CTB,CU,CB,TU,PU,TB,PB,LCU,Slice,Tile,Chroma,Luma,I帧,B帧,P帧
- I did these three things before the interview, and the result was actually direct
猜你喜欢

Notes on writing questions in sword finger offer

ESP8266 WIFI 模块和手机通信

un7.27:redis数据库常用命令。

IPC: multi process binder Aidl user guide, including cases

Fourier series

Appnium -- app automated test tool

Simple and easy-to-use performance testing tools recommended

【无标题】

Embedded development: tips and techniques -- the best practice of defensive programming with C

月薪28K学员 自动化测试经验分享
随机推荐
Cyber Nuwa, how to make digital people?
Summary and interpretation of CONDA virtual environment
[untitled]
We must do these seven things well before leaving, and it's troublesome to do one less thing.
LeetCode 0140. 单词拆分 II
Detailed explanation of string + memory function (C language)
金仓数据库KingbaseES安全指南--4 数据访问保护
Virtual machine class loading mechanism
Common weak network testing tools
RT thread changes the print serial port (add other functions on the basis of BSP)
Dynamic programming - 509. Fibonacci number
ESP8266 WIFI 模块和手机通信
Linux - MySQL advanced (day19)
Greed 122. The best time to buy and sell stocks II
测试用例管理工具
Go结构体
虚拟机类加载机制
Move notice!
Greed - 55. Jumping game
How does MySQL ensure high availability