给定两个整数N和K ,任务是找到K个连续的整数,使得它们的和为N。
注意:如果没有这样的K个整数,则打印-1。
例子:
Input: N = 15, K = 5
Output: 1 2 3 4 5
Explanation:
N can be represented as sum of 5 consecutive integers as follows –
=> N => 1 + 2 + 3 + 4 + 5 = 15
Input: N = 33, K = 6
Output: 3 4 5 6 7 8
Explanation:
N can be represented as sum of 6 consecutive integers as follows –
=> N => 3 + 4 + 5 + 6 + 7 + 8 = 33
天真的方法:一个简单的解决方案是运行一个从i = 0到N –(K – 1)的循环,以检查是否从i开始的K个连续整数的总和为N。
高效的方法:想法是使用算术级数来解决此问题,其中具有相同差的K个算术级数项之和可以定义如下:
- K项总和–
=> - 进一步求解方程以获得可能的第一项
=> - 这里的K是第K个项,可以写成1 + K – 1
=>
=> - 最后,检查计算出的第一个项是整数,如果是,则存在K个连续数,如果N,则其和。
下面是上述方法的实现:
C++
// C++ implementation to check if
// a number can be expressed as
// sum of K consecutive integer
#include
using namespace std;
// Function to check if a number can be
// expressed as the sum of k consecutive
void checksum(int n, int k)
{
// Finding the first
// term of AP
float first_term = ((2 * n) / k
+ (1 - k))
/ 2.0;
// Checking if first
// term is an integer
if (first_term - int(first_term) == 0) {
// Loop to print the K
// consecutive integers
for (int i = first_term;
i <= first_term + k - 1; i++) {
cout << i << " ";
}
}
else
cout << "-1";
}
// Driver Code
int main()
{
int n = 33, k = 6;
checksum(n, k);
return 0;
}
Java
// Java implementation to check if
// a number can be expressed as
// sum of K consecutive integer
class GFG{
// Function to check if a number can be
// expressed as the sum of k consecutive
static void checksum(int n, int k)
{
// Finding the first
// term of AP
float first_term = (float) (((2 * n) / k +
(1 - k)) / 2.0);
// Checking if first
// term is an integer
if (first_term - (int)(first_term) == 0)
{
// Loop to print the K
// consecutive integers
for(int i = (int)first_term;
i <= first_term + k - 1; i++)
{
System.out.print(i + " ");
}
}
else
System.out.print("-1");
}
// Driver Code
public static void main(String[] args)
{
int n = 33, k = 6;
checksum(n, k);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation to check
# if a number can be expressed as
# sum of K consecutive integer
# Function to check if a number can be
# expressed as the sum of k consecutive
def checksum(n, k):
# Finding the first
# term of AP
first_term = ((2 * n) / k + (1 - k)) / 2.0
# Checking if first
# term is an integer
if (first_term - int(first_term) == 0):
# Loop to print the K
# consecutive integers
for i in range(int(first_term),
int(first_term) + k):
print(i, end = ' ')
else:
print('-1')
# Driver Code
if __name__=='__main__':
(n, k) = (33, 6)
checksum(n, k)
# This code is contributed by rutvik_56
C#
// C# implementation to check if
// a number can be expressed as
// sum of K consecutive integer
using System;
class GFG{
// Function to check if a number can be
// expressed as the sum of k consecutive
static void checksum(int n, int k)
{
// Finding the first
// term of AP
float first_term = (float)(((2 * n) / k +
(1 - k)) / 2.0);
// Checking if first
// term is an integer
if (first_term - (int)(first_term) == 0)
{
// Loop to print the K
// consecutive integers
for(int i = (int)first_term;
i <= first_term + k - 1; i++)
{
Console.Write(i + " ");
}
}
else
Console.Write("-1");
}
// Driver Code
public static void Main(String[] args)
{
int n = 33, k = 6;
checksum(n, k);
}
}
// This code is contributed by sapnasingh4991
Javascript
输出:
3 4 5 6 7 8