当前位置:网站首页>[Niuke] find 1+2+3+... +n
[Niuke] find 1+2+3+... +n
2022-07-28 04:11:00 【Patrick star`】
seek 1+2+3+...+n_ Niuke Tiba _ Cattle from (nowcoder.com)
subject :
seek 1+2+3+...+n, It is required that multiplication and division shall not be used 、for、while、if、else、switch、case Wait for keywords and conditional statements (A?B:C).
Data range : 0<n≤200
Advanced : Spatial complexity O(1) , Time complexity O(n)
Ideas :
make the best of C++ characteristic , The construction object constructor will be called automatically , Let the summation be done in the constructor
Code :
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;边栏推荐
- What technical capabilities should a qualified software testing engineer have?
- Fourier series
- 【无标题】
- idea2022更改本地仓库,配置阿里云中央仓库
- Citrix virtual desktop tcp/udp transmission protocol switching
- Leetcode 0141. circular linked list - three solutions
- Machine learning 07: Bayesian learning
- C language: realize the exchange of two numbers without creating temporary variables
- H265/hevc noun explanation -- CTU, CTB, Cu, CB, Tu, PU, TB, Pb, LCU, slice, tile, chroma, luma, I frame, B frame, P frame
- We must do these seven things well before leaving, and it's troublesome to do one less thing.
猜你喜欢

ESP8266 WIFI 模块和手机通信

CV2. Threshold(), CV2. Findcontours(), CV2. Findcontours image contour processing

月薪28K学员 自动化测试经验分享

numeric_ Limits the range and related attributes of each data type learned

Super easy to use PC end long screenshot tool

The simulation test disconnects the server from the public network

JDBC usage

简单、好用的性能测试工具推荐

Go结构体

un7.27:redis数据库常用命令。
随机推荐
Which stock exchange has the lowest commission? Is it safe to open an account on your mobile phone
Move notice!
When import is introduced, sometimes there are braces, sometimes there are no braces. How should we understand this?
Monotonous stack -- 42. Receiving rain -- a difficult problem that big factories must know
常用的弱网测试工具
From Clickhouse to Snowflake: MPP query layer
setup和hold timing分析不满足是解决方法
Fourier series
cookie与Session
Dynamic planning - 1049. Weight of the last stone II
security异常处理机制
C language: realize the exchange of two numbers without creating temporary variables
Dynamic programming - 416. Segmentation and subsets
Kotlin——函数
Read Plato farm's eplato and the reason for its high premium
Kingbasees Security Guide for Jincang database -- 5.1. database transmission security
Kingbasees Security Guide for Jincang database -- 4 data access protection
2022.7.13-----leetcode.735
test case management tool
.net upload files through boundary