📌  相关文章
📜  检查是否只能通过增加或减少给定的秒数才能回到 12'0 时钟

📅  最后修改于: 2021-09-22 10:09:10             🧑  作者: Mango

给定 N 秒。任务是检查是否有可能从 12’0 时钟开始并回到 12 只增加或减少给定的秒数。我们需要只使用所有给定的秒数,我们可以添加一个元素或减去它。
例子:

Input: a[] = {60, 60, 120} 
Output: YES 
Add the first two seconds and 
subtract the last one to get back to 0. 

Input : a[] = {10, 20, 60, 180} 
Output : NO 

简单方法:生成所有可能的组合来解决上述问题。因此生成 N 个数的幂集。检查任何人的sum%(24*60)是否等于零,如果是,则可能不。
下面是上述方法的实现:

C++
// C++ program to check if we come back to
// zero or not in a clock
#include 
using namespace std;
 
// Function to check all combinations
bool checkCombinations(int a[], int n)
{
 
    // Generate all power sets
    int pow_set_size = pow(2, n);
    int counter, j;
 
    // Check for every combination
    for (counter = 0; counter < pow_set_size; counter++) {
 
        // Store sum for all combiantions
        int sum = 0;
        for (j = 0; j < n; j++) {
 
            /* Check if jth bit in the counter is set
             If set then print jth element from set */
            if (counter & (1 << j))
                sum += a[j]; // if set then consider as '+'
            else
                sum -= a[j]; // else consider as '-'
        }
 
        // If we can get back to 0
        if (sum % (24 * 60) == 0)
            return true;
    }
    return false;
}
// Driver Code
int main()
{
    int a[] = { 60, 60, 120 };
    int n = sizeof(a) / sizeof(a[0]);
 
    if (checkCombinations(a, n))
        cout << "YES";
    else
        cout << "NO";
    return 0;
}


Java
// Java program to check if we come
// back to zero or not in a clock
import java.lang.Math;
 
class GfG
{
 
    // Function to check all combinations
    static boolean checkCombinations(int a[], int n)
    {
        // Generate all power sets
        int pow_set_size = (int)Math.pow(2, n);
        int counter, j;
     
        // Check for every combination
        for (counter = 0; counter < pow_set_size; counter++)
        {
     
            // Store sum for all combiantions
            int sum = 0;
            for (j = 0; j < n; j++)
            {
     
                /* Check if jth bit in the counter is set
                If set then print jth element from set */
                if ((counter & (1 << j)) != 0)
                    sum += a[j]; // if set then consider as '+'
                else
                    sum -= a[j]; // else consider as '-'
            }
     
            // If we can get back to 0
            if (sum % (24 * 60) == 0)
                return true;
        }
        return false;
    }
 
    // Driver code
    public static void main(String []args)
    {
        int a[] = { 60, 60, 120 };
        int n = a.length;
     
        if (checkCombinations(a, n))
            System.out.println("YES");
        else
            System.out.println("NO");
    }
}
     
// This code is contributed by Rituraj Jain


Python 3
# Python 3 program to check if we come
# back to zero or not in a clock
 
# Function to check all combinations
def checkCombinations(a, n):
 
    # Generate all power sets
    pow_set_size = pow(2, n)
 
    # Check for every combination
    for counter in range(pow_set_size):
 
        # Store sum for all combiantions
        sum = 0
        for j in range(n) :
 
            # Check if jth bit in the counter is set
            # If set then print jth element from set
            if (counter & (1 << j)):
                sum += a[j] # if set then consider as '+'
            else:
                sum -= a[j] # else consider as '-'
 
        # If we can get back to 0
        if (sum % (24 * 60) == 0):
            return True
    return False
 
# Driver Code
if __name__ == "__main__":
     
    a = [ 60, 60, 120 ]
    n = len(a)
 
    if (checkCombinations(a, n)):
        print("YES")
    else:
        print("NO")
 
# This code is contributed by ita_c


C#
// C# program to check if we come
// back to zero or not in a clock
using System;
 
class GfG
{
 
    // Function to check all combinations
    static bool checkCombinations(int [] a, int n)
    {
        // Generate all power sets
        int pow_set_size = (int)Math.Pow(2, n);
        int counter, j;
     
        // Check for every combination
        for (counter = 0; counter < pow_set_size; counter++)
        {
     
            // Store sum for all combiantions
            int sum = 0;
            for (j = 0; j < n; j++)
            {
     
                /* Check if jth bit in the counter is set
                If set then print jth element from set */
                if ((counter & (1 << j)) != 0)
                    sum += a[j]; // if set then consider as '+'
                else
                    sum -= a[j]; // else consider as '-'
            }
     
            // If we can get back to 0
            if (sum % (24 * 60) == 0)
                return true;
        }
        return false;
    }
 
    // Driver code
    public static void Main()
    {
        int [] a = { 60, 60, 120 };
        int n = a.Length;
     
        if (checkCombinations(a, n))
            Console.WriteLine("YES");
        else
            Console.WriteLine("NO");
    }
}
     
// This code is contributed by ihritik


PHP


Javascript


输出:
YES

如果我们仔细观察,我们会发现这个问题基本上是 Partition Problem 的一个变种。所以我们可以使用动态规划对其进行优化(请参阅分区问题的方法2)。