829. Consecutive Numbers Sum

2023-11-10 05:40
文章标签 sum consecutive numbers 829

本文主要是介绍829. Consecutive Numbers Sum,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/380681

相关文章

最大流=最小割=最小点权覆盖集=sum-最大点权独立集

二分图最小点覆盖和最大独立集都可以转化为最大匹配求解。 在这个基础上,把每个点赋予一个非负的权值,这两个问题就转化为:二分图最小点权覆盖和二分图最大点权独立集。   二分图最小点权覆盖     从x或者y集合中选取一些点,使这些点覆盖所有的边,并且选出来的点的权值尽可能小。 建模:     原二分图中的边(u,v)替换为容量为INF的有向边(u,v),设立源点s和汇点t

计蒜客 Half-consecutive Numbers 暴力打表找规律

The numbers 11, 33, 66, 1010, 1515, 2121, 2828, 3636, 4545 and t_i=\frac{1}{2}i(i+1)t​i​​=​2​​1​​i(i+1), are called half-consecutive. For given NN, find the smallest rr which is no smaller than NN

如何导入sun.misc.BASE64Encoder和sum.misc.BASE64Decoder

右击项目名--->Build Path--->Configure Build Path...--->java Build Path--->Access rules:1 rule defined,added to all librar...   --->Edit --->Add...

高精度打表-Factoring Large Numbers

求斐波那契数,不打表的话会超时,打表的话普通的高精度开不出来那么大的数组,不如一个int存8位,特殊处理一下,具体看代码 #include<stdio.h>#include<string.h>#define MAX_SIZE 5005#define LEN 150#define to 100000000/*一个int存8位*/int num[MAX_SIZE][LEN];void

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt

leetCode#448. Find All Numbers Disappeared in an Array

Description Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this

18. 4 Sum

题目: 解答: 与之前的三数之和的解法类似,也是先排序,然后不断剔除不可能的条件,最后两个参数,通过两头求和计算得出。 代码: class Solution {public:vector<vector<int>> fourSum(vector<int>& nums, int target) {vector<vector<int>> result;int len = nums.size

apt-get update更新源时,出现“Hash Sum mismatch”问题

转载自:apt-get update更新源时,出现“Hash Sum mismatch”问题 当使用apt-get update更新源时,出现下面“Hash Sum mismatch”的报错,具体如下: root@localhost:~# apt-get update ...... ...... W: Failed to fetch http://us.archive.ubuntu.com/ub

[LeetCode] 485. Max Consecutive Ones

题: 题目 Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits or the last three digits are consec

[LeetCode] 303. Range Sum Query - Immutable

题:https://leetcode.com/problems/range-sum-query-immutable/description/ 题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums