📌  相关文章
📜  计算给定数字时钟显示相同数字的次数

📅  最后修改于: 2021-06-26 00:37:31             🧑  作者: Mango

给定一个通用的数字时钟,其小时数为h,分钟数为m ,任务是找出时钟显示相同时间的次数。如果小时和分钟中的每个数字都相同,则表示特定时间是相同的,即时间的类型为D:DD:DDDD:DDD:DD
请注意,时间写在数字时钟上,没有任何前导零,并且时钟显示的时间介于0h – 1小时和0m – 1分钟之间。几个相同时间的例子是:

  • 1:1
  • 22:22
  • 3:33
  • 11:1

例子:

方法:正如我们在所说明的示例中看到的那样,我们必须首先计算单位时间的位数(小时)相同的时间,然后计算两位数的小时数。在每个计数期间,我们都需要考虑个位数分钟和两位数分钟。
将有两个循环。第一循环处理单位数小时。第二个则处理两位数的小时数。在每个循环中,应该有两个条件。首先,如果迭代器变量小于总分钟数,则增加计数器。其次,如果(迭代器变量+迭代器变量* 10)小于总分钟数,则增加计数器。最后,我们将获得时钟显示的总相同时间。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count of
// identical times the clock shows
int countIdentical(int hours, int minutes)
{
 
    // To store the count of identical times
    // Initialized to 1 because of 0:0
    int i, count = 1;
 
    // For single digit hour
    for (i = 1; i <= 9 && i < hours; i++) {
 
        // Single digit minute
        if (i < minutes)
            count++;
 
        // Double digit minutes
        if ((i * 10 + i) < minutes)
            count++;
    }
 
    // For double digit hours
    for (i = 11; i <= 99 && i < hours; i = i + 11) {
 
        // Single digit minute
        if ((i % 10) < minutes)
            count++;
 
        // Double digit minutes
        if (i < minutes)
            count++;
    }
 
    // Return the required count
    return count;
}
 
// Driver code
int main()
{
    int hours = 24;
    int minutes = 60;
   
      // Function Call
    cout << countIdentical(hours, minutes);
 
    return 0;
}


Java
// Java implementation of the above approach
class GFG {
 
    // Function to return the count of
    // identical times the clock shows
    static int countIdentical(int hours, int minutes)
    {
 
        // To store the count of identical times
        // Initialized to 1 because of 0:0
        int i, count = 1;
 
        // For single digit hour
        for (i = 1; i <= 9 && i < hours; i++) {
 
            // Single digit minute
            if (i < minutes) {
                count++;
            }
 
            // Double digit minutes
            if ((i * 10 + i) < minutes) {
                count++;
            }
        }
 
        // For double digit hours
        for (i = 11; i <= 99 && i < hours; i = i + 11) {
 
            // Double digit minutes
            if (i < minutes) {
                count++;
            }
 
            // Single digit minute
            if ((i % 10) < minutes) {
                count++;
            }
        }
 
        // Return the required count
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int hours = 24;
        int minutes = 60;
       
        // Function Call
        System.out.println(countIdentical(hours, minutes));
    }
}
 
/* This code contributed by PrinciRaj1992 */


Python3
# Python 3 implementation of the approach
 
# Function to return the count of
# identical times the clock shows
 
 
def countIdentical(hours, minutes):
 
    # To store the count of identical times
    # Initialized to 1 because of 0:0
    count = 1
    i = 1
 
    # For single digit hour
    while(i <= 9 and i < hours):
 
        # Single digit minute
        if (i < minutes):
            count += 1
 
        # Double digit minutes
        if ((i * 10 + i) < minutes):
            count += 1
 
        i += 1
 
    # For double digit hours
    i = 11
    while(i <= 99 and i < hours):
 
         # Double digit minutes
        if (i < minutes):
            count += 1
 
        # Single digit minute
        if ((i % 10) < minutes):
            count += 1
 
        i += 11
 
    # Return the required count
    return count
 
 
# Driver code
if __name__ == '__main__':
    hours = 24
    minutes = 60
     
    # Function Call
    print(countIdentical(hours, minutes))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the above approach
using System;
 
class GFG {
 
    // Function to return the count of
    // identical times the clock shows
    static int countIdentical(int hours, int minutes)
    {
 
        // To store the count of identical times
        // Initialized to 1 because of 0:0
        int i, count = 1;
 
        // For single digit hour
        for (i = 1; i <= 9 && i < hours; i++) {
 
            // Single digit minute
            if (i < minutes) {
                count++;
            }
 
            // Double digit minutes
            if ((i * 10 + i) < minutes) {
                count++;
            }
        }
 
        // For double digit hours
        for (i = 11; i <= 99 && i < hours; i = i + 11) {
 
            // Double digit minutes
            if (i < minutes) {
                count++;
            }
 
            // Single digit minute
            if ((i % 10) < minutes) {
                count++;
            }
        }
 
        // Return the required count
        return count;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int hours = 24;
        int minutes = 60;
       
        // Function Call
        Console.WriteLine(countIdentical(hours, minutes));
    }
}
 
// This code has been contributed by 29AjayKumar


PHP


Javascript


输出
19

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。