当前位置:网站首页>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;
}
};
边栏推荐
- LeetCode 31下一个排列、LeetCode 64最小路径和、LeetCode 62不同路径、LeetCode 78子集、LeetCode 33搜索旋转排序数组(修改二分法)
- Pytest -- plug-in writing
- Thread data sharing and security -threadlocal
- 【读书笔记】《文案变现》——写出有效文案的四个黄金步骤
- 打包iso文件的话,怎样使用hybrid格式输出?isohybrid:command not found
- Leetcode 1482 guess, how about this question?
- 【日常训练】1175. 质数排列
- Listener listener
- 实战 ELK 优雅管理服务器日志
- About the application of MySQL
猜你喜欢

So easy 将程序部署到服务器

最好用的信任关系自动化脚本(shell)

Ctfshow blasting WP

The best learning method in the world: Feynman learning method

Design practice of current limiting components

Chapitre 03 Bar _ Gestion des utilisateurs et des droits

Data exchange JSON

Introduction to EtherCAT

Learning notes for introduction to C language multithreaded programming

The value of the second servo encoder is linked to the NC virtual axis of Beifu PLC for display
随机推荐
Ultimate dolls 2.0 | encapsulation of cloud native delivery
Elk elegant management server log
Force buckle - sum of two numbers
Feature Pyramid Networks for Object Detection论文理解
Druid监控统计数据源
Redis分布式锁的8大坑
Pyramid Scene Parsing Network【PSPNet】论文阅读
5、【WebGIS实战】软件操作篇——服务发布及权限管理
GCC usage, makefile summary
Leetcode 1818 absolute value, sorting, dichotomy, maximum value
md5sum操作
监听器 Listener
后台系统页面左边菜单按钮和右边内容的处理,后台系统页面出现双滚动
pytorch训练深度学习网络设置cuda指定的GPU可见
完全背包问题
go实现命令行的工具cli
多元线性回归
几行事务代码,让我赔了16万
LeetCode 144二叉树的前序遍历、LeetCode 114二叉树展开为链表
Pathmeasure implements loading animation