The description of the problem

Given an integer n, return the number of ways you can write n as the sum of consecutive positive integers.来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/consecutive-numbers-sum

an example

Example 1:Input: n = 5
Output: 2
Explanation: 5 = 2 + 3

The intuition for this problem

The codes for above problem

#include <iostream>
using namespace std;
class Solution {public:int consecutiveNumbersSum(int n) {int res = 0;int bound = 2*n;for (int k = 1; k * (k + 1) <= bound; ++k) {if (isKConservative(k, n)) ++res;}return res;}bool isKConservative(int k, int n) {if (k % 2 ==1 ){return n % k == 0;} else {return n % k != 0 && 2*n % k == 0;}}
};
int main()
{Solution s;cout << "res: " << s.consecutiveNumbersSum(5) << endl;return 0;
}

The second format:

#include <iostream>
using namespace std;
class Solution {public:int consecutiveNumbersSum(int n) {int res = 0;int k = 1;while (k*(k+1) <= 2*n) {bool flag = false;if (k %2 ){if (n % k == 0) flag = true;} else {if ((n % k != 0) && ((2*n) % k == 0)) flag = true;}if (flag) res++;++k;}return res;}
};
int main()
{Solution s;cout << "res: " << s.consecutiveNumbersSum(5) << endl;return 0;
}

The results

$ ./test
res: 2

829. Consecutive Numbers Sum相关推荐

  1. LeetCode 829. Consecutive Numbers Sum--笔试题--C++解法

    LeetCode 829. Consecutive Numbers Sum–笔试题–C++解法 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题 ...

  2. Divide a list of numbers into group of consecutive numbers

    //Divide a list of numbers into group of consecutive numbers but their original order should be pres ...

  3. [SQL]LeetCode180. 连续出现的数字 | Consecutive Numbers

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...

  4. Project Euler 50 Consecutive prime sum

    题意: 素数41可以写成六个连续素数的和: 41 = 2 + 3 + 5 + 7 + 11 + 13 在小于一百的素数中,41能够被写成最多的连续素数的和. 在小于一千的素数中,953能够被写成最多的 ...

  5. leetcode 180. Consecutive Numbers

    题意:投影出连续三个num值相等的元组的num 题目链接:https://leetcode.com/problems/consecutive-numbers/ select distinct l1.n ...

  6. LeetCode 所有题目总结

    文章目录 做题注意事项 题目分类 1.位运算 2.字符串题型 3.TopK 问题--最大堆/最小堆 4.链表 5.动态规划 easy Medium hard 6.贪心 7.树 8.图 9.数学题 10 ...

  7. [经验]2020届后台开发方向实习,秋招经验总结

    文章目录 [经验]2020届后台开发方向实习,秋招经验总结 实习 秋招 准备 1.算法题 2.简历 3.基础知识 4.招聘信息 5.智力题 6.心态 7.面经 [经验]2020届后台开发方向实习,秋招 ...

  8. ligo_algo_art

    算法和复杂度 复杂度 LinkedList 链表 基础模块 链表定义 常见的边界条件 翻转模块 先走几步模块 快慢指针 奇偶抽取 两个链表拉链合并 移除特定节点 排序模块 迭代和递归的基本过程 递归的 ...

  9. leetcode 题解 (500-1000题,持续更新,part 2)

    part1(1-500), part3(1000-*) 502. IPO 题意:给定k,w,profits数组和capital数组.k表示最多可完成的任务数.w是初始资本.profits是各个任务的收 ...

  10. LeetCode All in One 题目讲解汇总(持续更新中...)

    原文地址:https://www.cnblogs.com/grandyang/p/4606334.html 终于将LeetCode的大部分题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开 ...

最新文章

  1. ICML2020 | GCNII:使用初始残差和恒等映射解决过度平滑
  2. jQuery 2.0.3 源码分析 Deferred(最细的实现剖析,带图)
  3. AspNetPager分页控件
  4. leetcode 1143. 最长公共子序列
  5. MyEclipse+Tomcat+MAVEN+Git项目完整环境搭建
  6. approach for doing things
  7. JUST技术:提升基于GPS轨迹的路网推测精确度
  8. 判断系统是大端还是小段
  9. python 内置模块 subprocess
  10. OpenFlow网络中处理正常流量的工作流程
  11. PyTorch 深度学习:33分钟快速入门——VGG
  12. 【Loss】深度学习的多个loss如何平衡?
  13. 微信小程序——组件(二)
  14. opencv——convertTo
  15. 实验一:常用网络命令操作
  16. 外圣内王适用于互联网行业吗?
  17. 实习周记----第三周
  18. 【GamePlay】Unity手机屏幕UI适配问题
  19. 031--python--打印机票页面
  20. Java 源码 - Socket 源码及面试题

热门文章

  1. 西南大学学前教育计算机答案,西南大学网络教育本科《学前教育心理学》作业题答案完整版.doc...
  2. 李荣融警示央企至少要做好过两年从紧的准备
  3. 解决java.io.FileNotFoundException: /storage/emulated/0/formats/
  4. brian goetz_Brian Goetz引入了新的OpenJDK项目“ Valhalla”
  5. 【C语言】猴子选大王问题
  6. 计算机课怎么保存文件,在线录制的课程如何让保存到本地?方法想不到
  7. OpenSSL单向与双向认证通信
  8. 2023年互联网行业研究报告
  9. 游戏《孢子》的思考 —— Metaball的逻辑,实现及对应思考
  10. 一分钟教会你制作培训机构招生H5宣传页