当前位置:网站首页>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;
}
};
边栏推荐
- So easy deploy program to server
- 不用加减乘除实现加法
- Md5sum operation
- Edge Drawing: A combined real-time edge and segment detector 翻译
- Error accessing URL 404
- 实战 ELK 优雅管理服务器日志
- Leetcode 1818 absolute value, sorting, dichotomy, maximum value
- Druid监控统计数据源
- How do I use Google Chrome 11's Upload Folder feature in my own code?
- So easy 将程序部署到服务器
猜你喜欢
Elk elegant management server log
[nine day training] content III of the problem solution of leetcode question brushing Report
FCN全卷积网络理解及代码实现(来自pytorch官方实现)
Nacos
Research on target recognition and tracking based on 3D laser point cloud
How do spark tasks of 10W workers run? (Distributed Computing)
Druid监控统计数据源
雪崩问题以及sentinel的使用
How the network is connected: Chapter 2 (Part 2) packet receiving and sending operations between IP and Ethernet
[深度学习]激活函数(Sigmoid等)、前向传播、反向传播和梯度优化;optimizer.zero_grad(), loss.backward(), optimizer.step()的作用及原理
随机推荐
Ouc2021 autumn - Software Engineering - end of term (recall version)
过滤器 Filter
pytorch中的双线性插值上采样(Bilinear Upsampling)、F.upsample_bilinear
Subnet division (10)
FCN全卷积网络理解及代码实现(来自pytorch官方实现)
排序链表(归并排序)
Valid brackets (force deduction 20)
Best used trust automation script (shell)
Keil5中如何做到 0 Error(s), 0 Warning(s).
Redis高效点赞与取消功能
文件上传下载
Golang multi graph generation gif
Golang多图生成gif
How to achieve 0 error (s) and 0 warning (s) in keil5
后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
Detailed explanation of ES6 deconstruction grammar
IPv4和IPv6、局域网和广域网、网关、公网IP和私有IP、IP地址、子网掩码、网段、网络号、主机号、网络地址、主机地址以及ip段/数字-如192.168.0.1/24是什么意思?
C#实现基于广度优先BFS求解无权图最短路径----完整程序展示
[daily training] 1175 Prime permutation
shell脚本使用两个横杠接收外部参数