📜  用1和2的n乘以最小项数为k的n

📅  最后修改于: 2021-05-05 01:31:28             🧑  作者: Mango

给定两个正整数nk 。 n可以使用多个项以多种方式表示为1和2的总和。任务是找到用于使总和为n的最小项1s和2s,并且项数必须是k的倍数。如果不存在这样的数量的术语,请打印“ -1”。
例子 :

Input : n = 10, k = 2
Output : 6
10 can be represented as 2 + 2 + 2 + 2 + 1 + 1.
Number of terms used are 6 which is multiple of 2.

Input : n = 11, k = 4
Output : 8
10 can be represented as 2 + 2 + 2 + 1 + 1 + 1 + 1 + 1
Number of terms used are 8 which is multiple of 4.

观察到,当n被加1时,用来表示n为1s和2s之和的项的最大数目为n。同样,最小项数将是2s的n / 2倍和n%2乘以1s。因此,从最小项数迭代到最大项数,并检查k是否有倍数。

C++
// C++ program to find minimum multiple of k
// terms used to make sum n using 1s and 2s.
#include
using namespace std;
 
// Return minimum multiple of k terms used to
// make sum n using 1s and 2s.
int minMultipleK(int n, int k)
{
    // Minimum number of terms required to make
    // sum n using 1s and 2s.
    int min = (n / 2) + (n % 2);
 
    // Iterate from Minimum to maximum to find
    // multiple of k. Maximum number of terns is
    // n (Sum of all 1s)
    for (int i = min; i <= n; i++)
        if (i % k == 0)
            return i;
 
    return -1;
}
 
// Driven Program
int main()
{
    int n = 10, k = 2;
    cout << minMultipleK(n, k) << endl;
    return 0;
}


Java
// Java program to find minimum
// multiple of k terms used to
// make sum n using 1s and 2s.
import java.io.*;
 
class GFG
{
     
// Return minimum multiple of
// k terms used to make sum n
// using 1s and 2s.
static int minMultipleK(int n,
                        int k)
{
    // Minimum number of terms
    // required to make sum n
    // using 1s and 2s.
    int min = (n / 2) + (n % 2);
 
    // Iterate from Minimum to
    // maximum to findmultiple of k.
    // Maximum number of terms is
    // n (Sum of all 1s)
    for (int i = min; i <= n; i++)
        if (i % k == 0)
            return i;
 
    return -1;
}
 
// Driver Code
public static void main (String[] args)
{
    int n = 10, k = 2;
    System.out.println( minMultipleK(n, k));
}
}
 
// This code is contributed by anuj_67.


Python3
# Python3 program to find minimum multiple of k
# terms used to make sum n using 1s and 2s.
 
# Return minimum multiple of k terms
# used to make sum n using 1s and 2s.
def minMultipleK( n, k):
 
    # Minimum number of terms required
    # to make sum n using 1s and 2s.
    min = (n // 2) + (n % 2)
 
    # Iterate from Minimum to maximum to find
    #multiple of k. Maximum number of terns is
    # n (Sum of all 1s)
    for i in range(min, n + 1):
        if (i % k == 0):
            return i
 
    return -1
 
# Driver Code
if __name__=="__main__":
 
    n = 10
    k = 2
    print (minMultipleK(n, k))
     
# This code is contributed
# by ChitraNayal


C#
// C# program to find minimum
// multiple of k terms used to
// make sum n using 1s and 2s.
using System;
 
class GFG
{
     
// Return minimum multiple of
// k terms used to make sum n
// using 1s and 2s.
static int minMultipleK(int n,
                        int k)
{
    // Minimum number of terms
    // required to make sum n
    // using 1s and 2s.
    int min = (n / 2) + (n % 2);
 
    // Iterate from Minimum to
    // maximum to findmultiple of k.
    // Maximum number of terms is
    // n (Sum of all 1s)
    for (int i = min; i <= n; i++)
        if (i % k == 0)
            return i;
 
    return -1;
}
 
// Driver Code
public static void Main ()
{
    int n = 10, k = 2;
    Console.WriteLine( minMultipleK(n, k));
}
}
 
// This code is contributed by anuj_67.


PHP


Javascript


输出 :

6