在給定的24小時制時間中替換‘_’以最大化時間


在給定的24小時制時間格式中用數字替換‘_’以最大化時間是一個涉及計算透過替換24小時制時間格式中缺失的數字來獲得最大可能時間的問題。任務是透過用任何數字替換字元“_”來找到最大可能的時間。在本教程中,我們將討論如何使用C++程式語言解決這個問題。我們將逐步解釋用於計算最大可能時間的演算法,以及實現該演算法的C++程式碼。

此外,我們將包含測試示例來說明問題及其解決方案,以及對測試示例的解釋。在本教程結束時,讀者將更好地理解如何使用C++解決這個問題,並將能夠運用這些知識來解決未來的類似問題。讓我們開始吧!

問題陳述

目標是透過替換字串S中用‘_’標記的位置上的任何數字來確定最大可能的時間,其中S代表24小時制時間格式。

示例1

輸入

S = "23:4_"

輸出

"23:49"

解釋 − 在給定的輸入字串中,將第三個字元替換為'9'以獲得最大可能的時間。因此,輸出為“23:49”。

示例2

輸入

S = "1_:22"

輸出

"19:22"

解釋 − 在給定的輸入字串中,將第一個字元替換為'1'以獲得24小時制格式中的最大可能小時數。因此,輸出為“19:22”。

注意 − 在兩個示例中,輸入字串S都代表24小時制時間格式,其中缺失的數字用'_'表示。目標是用任何數字替換缺失的數字以獲得最大可能的時間。第一個示例演示瞭如何替換分鐘位上的第二個數字以獲得最大時間。第二個示例顯示瞭如何替換小時位上的第一個數字以獲得24小時制格式中的最大小時數。

演算法

  • 步驟1 − 從使用者處獲取輸入字串S。

  • 步驟2 − 將最大小時值和分鐘值分別初始化為23和59。

  • 步驟3 − 如果分鐘位上的第二位數字缺失,則將其替換為'9'。

  • 步驟4 − 如果小時位上的第一位數字缺失

    • 如果第二位數字小於'4',則將其替換為'9'。

    • 否則,將其替換為'3'。

  • 步驟5 − 如果小時位上的第二位數字缺失

    • 如果第一位數字小於'2'或也缺失,則將其替換為'2'。

    • 否則,將其替換為'1'。

  • 步驟6 − 輸出最大可能的時間。

示例

以下是實現上述演算法的程式:

在下面的程式中,替換給定時間中的缺失數字以找到最大可能的時間。程式檢查並用'9'替換分鐘位上缺失的第二位數字。然後,它透過在第二位數字小於'4'時用'2'替換它,否則用'1'替換它來處理小時位上缺失的第一位數字。最後,如果小時位上的第二位數字缺失,則如果第一位數字小於'2',則用'3'替換它,否則用'2'替換它。程式根據所做的替換輸出最大可能的時間。

#include <stdio.h>
#include <string.h>

int main() {
   char S[] = "1_:22";
   printf("Given time with missing digits: %s\n", S);
   // Initialize the maximum hour and minute values
   int maxHour = 23;
   int maxMinute = 59;
   // If the second digit in the minute's place is missing, replace it with '9'
   if (S[4] == '_') {
      S[4] = '9';
   }
   // If the first digit in the hour's place is missing, replace it with '2' if the second digit is less than '4',
   // otherwise replace it with '1'
   if (S[1] == '_') {
      if (S[0] < '2') {
         S[1] = '9';
      } else {
         S[1] = '3';
      }
   }
   // If the second digit in the hour's place is missing, replace it with '3' if the first digit is less than '2',
   // otherwise replace it with '2'
   if (S[0] == '_') {
      if (S[1] < '4' || S[1] == '_') {
         S[0] = '2';
      } else {
         S[0] = '1';
      }
   }
   // Output the maximum possible time
   printf("The maximum possible time is: %s\n", S);
   return 0;
}

輸出

Given time with missing digits: 1_:22
The maximum possible time is: 19:22
#include <iostream>
#include <algorithm>
int main() {
   std::string S = "1_:22";
   std::cout << "Given time with missing digits: " << S << std::endl;
   // Initialize the maximum hour and minute values
   int maxHour = 23;
   int maxMinute = 59;
   // If the second digit in the minute's place is missing, replace it with '9'
   if (S[4] == '_') {
      S[4] = '9';
   }
   // If the first digit in the hour's place is missing, replace it with '2' if the second digit is less than '4',
   // otherwise replace it with '1'
   if (S[1] == '_') {
      if (S[0] < '2') {
         S[1] = '9';
      } else {
         S[1] = '3';
      }
   }
   // If the second digit in the hour's place is missing, replace it with '3' if the first digit is less than '2',
   // otherwise replace it with '2'
   if (S[0] == '_') {
      if (S[1] < '4' || S[1] == '_') {
         S[0] = '2';
      } else {
         S[0] = '1';
      }
   }
   // Output the maximum possible time
   std::cout << "The maximum possible time is: " << S << std::endl;
   return 0;
}

輸出

Given time with missing digits: 1_:22
The maximum possible time is: 19:22
public class MaximumTime {
   public static String calculateMaximumTime(String S) {
      // Initialize the maximum hour and minute values
      int maxHour = 23;
      int maxMinute = 59;

      char[] chars = S.toCharArray();

      // If the second digit in the minute's place is missing, replace it with '9'
      if (chars[4] == '_') {
         chars[4] = '9';
      }

      // If the first digit in the hour's place is missing, replace it with '2' if the second digit is less than '4',
      // otherwise replace it with '1'
      if (chars[1] == '_') {
         if (chars[0] < '2') {
            chars[1] = '9';
         } else {
            chars[1] = '3';
         }
      }

      // If the second digit in the hour's place is missing, replace it with '3' if the first digit is less than '2',
      // otherwise replace it with '2'
      if (chars[0] == '_') {
         if (chars[1] < '4' || chars[1] == '_') {
            chars[0] = '2';
         } else {
            chars[0] = '1';
         }
      }

      // Convert the character array back to a string
      return new String(chars);
   }

   public static void main(String[] args) {
      String inputStr = "1_:22";
      System.out.println("Given time with missing digits: " + inputStr);
      String result = calculateMaximumTime(inputStr);
      System.out.println("The maximum possible time is: " + result);
   }
}

輸出

Given time with missing digits: 1_:22
The maximum possible time is: 19:22
def calculate_maximum_time(S):
   # Initialize the maximum hour and minute values
   max_hour = 23
   max_minute = 59
    
   # If the second digit in the minute's place is missing, replace it with '9'
   S = list(S)
   if S[4] == '_':
      S[4] = '9'
    
   # If the first digit in the hour's place is missing, replace it with '2' if the second digit is less than '4',
   # otherwise replace it with '1'
   if S[1] == '_':
      if S[0] < '2':
         S[1] = '9'
      else:
         S[1] = '3'
    
   # If the second digit in the hour's place is missing, replace it with '3' if the first digit is less than '2',
   # otherwise replace it with '2'
   if S[0] == '_':
      if S[1] < '4' or S[1] == '_':
         S[0] = '2'
      else:
         S[0] = '1'
    
   # Convert the list back to a string
   result = ''.join(S)
   return result

input_str = "1_:22"
print("Given time with missing digits:", input_str)
result = calculate_maximum_time(input_str)
print("The maximum possible time is:", result)

輸出

Given time with missing digits: 1_:22
The maximum possible time is: 19:22

結論

總而言之,透過替換給定24小時制時間格式中的'_'來最大化時間的問題,可以使用本教程中提供的演算法和C++程式碼輕鬆解決。透過遵循分步方法並理解演算法背後的邏輯,讀者可以獲得寶貴的見解,從而解決類似的問題。藉助測試示例,讀者可以驗證程式碼的正確性,並增強其程式設計技能的信心。

更新於:2023年10月23日

瀏覽量:132

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.