📌  相关文章
📜  找出K个连续的整数,使它们的和为N

📅  最后修改于: 2021-04-28 13:45:29             🧑  作者: Mango

给定两个整数NK ,任务是找到K个连续的整数,使得它们的和为N。
注意:如果没有这样的K个整数,则打印-1。
例子:

天真的方法:一个简单的解决方案是运行一个从i = 0到N –(K – 1)的循环,以检查是否从i开始的K个连续整数的总和为N。
高效的方法:想法是使用算术级数来解决此问题,其中具有相同差的K个算术级数项之和可以定义如下:

  1. K项总和– \frac{Number of Terms}{2} * (first Term + Last Term)
    => N = \frac{a_1 + a_K}{2} * K
  2. 进一步求解方程以获得可能的第一项
    => a_1 + a_K = \frac{2*N}{K}
  3. 这里的K是第K项,可以写成1 + K – 1
    => 2*a_1 + K - 1 = \frac{2*N}{K}
    => a_1 = \frac{\frac{2*N}{K} + 1 - K}{2}
  4. 最后,检查计算出的第一个项是整数,如果是,则存在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