当前位置:网站首页>Leetcode:829. 连续整数求和
Leetcode:829. 连续整数求和
2022-07-01 03:16:00 【Re:fused】
题目:829. 连续整数求和
题意:
给定一个正整数 n,返回 连续正整数满足所有数字之和为 n 的组数 。
题解:
连续就想到了等差为1的等差数列,所以有公式 m a 1 + ( m − 1 ) m / 2 = m ( m a 1 + ( m − 1 ) / 2 ) = n ma_1+(m-1)m/2=m(ma_1+(m-1)/2) = n ma1+(m−1)m/2=m(ma1+(m−1)/2)=n,可以看出m必须是n的银子,并且m-1为偶数才可以,才能确保 a 1 a_1 a1为整数。
代码:
class Solution {
public:
int consecutiveNumbersSum(int n) {
int half = sqrt(n);
int cn = 0;
for(int i = 1; i <= half; i++){
if(i == half && i*i == n){
if((i-1)%2 == 0)cn++;
}
else{
if(n % i == 0){
if((i -1)%2 == 0)cn++;
int other = n / i;
if((other-1)%2 == 0)cn++;
}
}
}
return cn;
}
};
边栏推荐
- 5、【WebGIS实战】软件操作篇——服务发布及权限管理
- LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
- Basic concepts of database
- 限流组件设计实战
- Overview of EtherCAT principle
- Data exchange JSON
- 【日常训练】1175. 质数排列
- 打包iso文件的话,怎样使用hybrid格式输出?isohybrid:command not found
- About the application of MySQL
- torch.histc
猜你喜欢

EDLines: A real-time line segment detector with a false detection control翻译

Depth first traversal of C implementation Diagram -- non recursive code

监听器 Listener

8 pits of redis distributed lock

Listener listener

Hal library setting STM32 interrupt

Filter

Data exchange JSON

Ctfshow blasting WP

Research on target recognition and tracking based on 3D laser point cloud
随机推荐
10、Scanner.next() 无法读取空格/indexOf -1
C language EXECL function
pytest-fixture
LeetCode 128最长连续序列(哈希set)
Research on target recognition and tracking based on 3D laser point cloud
Redis 教程
【读书笔记】《文案变现》——写出有效文案的四个黄金步骤
IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
Common interview questions for performance test
Home online shopping project
shell脚本使用两个横杠接收外部参数
家居网购项目
Analyze datahub, a new generation metadata platform of 4.7K star
JS daily development tips (continuous update)
Design practice of current limiting components
数据库中COMMENT关键字的使用
pytorch nn.AdaptiveAvgPool2d(1)
How to use hybrid format to output ISO files? isohybrid:command not found
Druid监控统计数据源
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理