logo

Converteix el temps donat en paraules

Prova-ho a GfG Practice ' title=

Donada una hora en el format hh:mm (format de 12 hores) 0< hh < 12 0 <= mm < 60. The task is to convert it into words as shown:
Exemples:  
 

Input : h = 5 m = 0 Output : five o' clock Input : h = 6 m = 24 Output : twenty four minutes past six


Els casos de cantonada són m = 0 m = 15 m = 30 i m = 45.
 

6:00 six o'clock 6:10 ten minutes past six 6:15 quarter past six 6:30 half past six 6:45 quarter to seven 6:47 thirteen minutes to seven


 




La idea és utilitzar la declaració if-else-if per determinar el temps en paraules. D'acord amb l'exemple anterior a partir dels minuts, podem classificar el temps en paraules en 8, que són minuts iguals a 0 15 30 45 1 59 i en un interval inferior a 30 o superior a 30. Comproveu el valor dels minuts i imprimiu en conseqüència.
A continuació es mostra la implementació d'aquest enfocament:
 

C++
// C++ program to convert time into words #include    using namespace std; // Print Time in words. void printWords(int h int m) {  char nums[][64] = { 'zero' 'one' 'two' 'three' 'four'  'five' 'six' 'seven' 'eight' 'nine'  'ten' 'eleven' 'twelve' 'thirteen'  'fourteen' 'fifteen' 'sixteen' 'seventeen'  'eighteen' 'nineteen' 'twenty' 'twenty one'  'twenty two' 'twenty three' 'twenty four'  'twenty five' 'twenty six' 'twenty seven'  'twenty eight' 'twenty nine'  };  if (m == 0)  printf('%s o' clockn' nums[h]);  else if (m == 1)  printf('one minute past %sn' nums[h]);  else if (m == 59)  printf('one minute to %sn' nums[(h % 12) + 1]);  else if (m == 15)  printf('quarter past %sn' nums[h]);  else if (m == 30)  printf('half past %sn' nums[h]);  else if (m == 45)  printf('quarter to %sn' nums[(h % 12) + 1]);  else if (m <= 30)  printf('%s minutes past %sn' nums[m] nums[h]);  else if (m > 30)  printf('%s minutes to %sn' nums[60 - m]  nums[(h % 12) + 1]); } // Driven Program int main() {  int h = 6;  int m = 24;  printWords(h m);  return 0; } 
Java
// Java program to convert time into words public class GFG {    // Print Time in words.  static void printWords(int h int m)  {  String nums[] = { 'zero' 'one' 'two' 'three' 'four'  'five' 'six' 'seven' 'eight' 'nine'  'ten' 'eleven' 'twelve' 'thirteen'  'fourteen' 'fifteen' 'sixteen' 'seventeen'  'eighteen' 'nineteen' 'twenty' 'twenty one'  'twenty two' 'twenty three' 'twenty four'  'twenty five' 'twenty six' 'twenty seven'  'twenty eight' 'twenty nine'  };    if (m == 0)  System.out.println(nums[h] + ' o' clock ');    else if (m == 1)  System.out.println('one minute past ' +   nums[h]);    else if (m == 59)  System.out.println('one minute to ' +   nums[(h % 12) + 1]);    else if (m == 15)  System.out.println('quarter past ' + nums[h]);    else if (m == 30)  System.out.println('half past ' + nums[h]);    else if (m == 45)  System.out.println('quarter to ' +   nums[(h % 12) + 1]);    else if (m <= 30)  System.out.println( nums[m] + ' minutes past ' +  nums[h]);    else if (m > 30)  System.out.println( nums[60 - m] + ' minutes to ' +   nums[(h % 12) + 1]);  }    // Driven code  public static void main(String []args)  {  int h = 6;  int m = 24;  printWords(h m);  } } // This code is contributed by ihritik 
Python3
# Python3 program to convert  # time into words # Print Time in words. def printWords(h m): nums = ['zero' 'one' 'two' 'three' 'four' 'five' 'six' 'seven' 'eight' 'nine' 'ten' 'eleven' 'twelve' 'thirteen' 'fourteen' 'fifteen' 'sixteen' 'seventeen' 'eighteen' 'nineteen' 'twenty' 'twenty one' 'twenty two' 'twenty three' 'twenty four' 'twenty five' 'twenty six' 'twenty seven' 'twenty eight' 'twenty nine']; if (m == 0): print(nums[h] 'o' clock'); elif (m == 1): print('one minute past' nums[h]); elif (m == 59): print('one minute to' nums[(h % 12) + 1]); elif (m == 15): print('quarter past' nums[h]); elif (m == 30): print('half past' nums[h]); elif (m == 45): print('quarter to' (nums[(h % 12) + 1])); elif (m <= 30): print(nums[m]'minutes past' nums[h]); elif (m > 30): print(nums[60 - m] 'minutes to' nums[(h % 12) + 1]); # Driver Code h = 6; m = 24; printWords(h m); # This code is contributed  # by Princi Singh 
C#
// C# program to convert time into words using System; class GFG {    // Print Time in words.  static void printWords(int h int m)  {  string [] nums = { 'zero' 'one' 'two' 'three' 'four'  'five' 'six' 'seven' 'eight' 'nine'  'ten' 'eleven' 'twelve' 'thirteen'  'fourteen' 'fifteen' 'sixteen' 'seventeen'  'eighteen' 'nineteen' 'twenty' 'twenty one'  'twenty two' 'twenty three' 'twenty four'  'twenty five' 'twenty six' 'twenty seven'  'twenty eight' 'twenty nine'  };    if (m == 0)  Console.WriteLine(nums[h] + ' o' clock ');    else if (m == 1)  Console.WriteLine('one minute past ' + nums[h]);    else if (m == 59)  Console.WriteLine('one minute to ' +  nums[(h % 12) + 1]);    else if (m == 15)  Console.WriteLine('quarter past ' + nums[h]);    else if (m == 30)  Console.WriteLine('half past ' + nums[h]);    else if (m == 45)  Console.WriteLine('quarter to ' +   nums[(h % 12) + 1]);    else if (m <= 30)  Console.WriteLine( nums[m] + ' minutes past ' +  nums[h]);    else if (m > 30)  Console.WriteLine( nums[60 - m] + ' minutes to ' +   nums[(h % 12) + 1]);  }    // Driven code  public static void Main()  {  int h = 6;  int m = 24;  printWords(h m);  } } // This code is contributed by ihritik 
PHP
 // PHP program to convert  // time into words // Print Time in words. function printWords($h $m) { $nums = array('zero' 'one' 'two' 'three' 'four' 'five' 'six' 'seven' 'eight' 'nine' 'ten' 'eleven' 'twelve' 'thirteen' 'fourteen' 'fifteen' 'sixteen' 'seventeen' 'eighteen' 'nineteen' 'twenty' 'twenty one' 'twenty two' 'twenty three' 'twenty four' 'twenty five' 'twenty six' 'twenty seven' 'twenty eight' 'twenty nine'); if ($m == 0) echo $nums[$h] 'o' clockn' ; else if ($m == 1) echo 'one minute past ' $nums[$h] 'n'; else if ($m == 59) echo 'one minute to ' $nums[($h % 12) + 1] 'n'; else if ($m == 15) echo 'quarter past ' $nums[$h] 'n'; else if ($m == 30) echo 'half past ' $nums[$h]'n'; else if ($m == 45) echo 'quarter to ' ($nums[($h % 12) + 1]) 'n'; else if ($m <= 30) echo $nums[$m] ' minutes past ' $nums[$h]'n'; else if ($m > 30) echo $nums[60 - $m] ' minutes to ' $nums[($h % 12) + 1] 'n'; } // Driver Code $h = 6; $m = 24; printWords($h $m); // This code is contributed by aj_36 ?> 
JavaScript
<script>  // Javascript program to convert time into words    // Print Time in words.  function printWords(h m)  {  let nums = [ 'zero' 'one' 'two' 'three' 'four'  'five' 'six' 'seven' 'eight' 'nine'  'ten' 'eleven' 'twelve' 'thirteen'  'fourteen' 'fifteen' 'sixteen' 'seventeen'  'eighteen' 'nineteen' 'twenty' 'twenty one'  'twenty two' 'twenty three' 'twenty four'  'twenty five' 'twenty six' 'twenty seven'  'twenty eight' 'twenty nine'  ];    if (m == 0)  document.write(nums[h] + ' o' clock ' + '
'
); else if (m == 1) document.write('one minute past ' + nums[h] + '
'
); else if (m == 59) document.write('one minute to ' + nums[(h % 12) + 1] + '
'
); else if (m == 15) document.write('quarter past ' + nums[h] + '
'
); else if (m == 30) document.write('half past ' + nums[h] + '
'
); else if (m == 45) document.write('quarter to ' + nums[(h % 12) + 1] + '
'
); else if (m <= 30) document.write( nums[m] + ' minutes past ' + nums[h] + '
'
); else if (m > 30) document.write( nums[60 - m] + ' minutes to ' + nums[(h % 12) + 1] + '
'
); } let h = 6; let m = 24; printWords(h m); </script>

Sortida
twenty four minutes past six 

Complexitat temporal: O(1)

Espai Auxiliar : S'ha utilitzat O(1) com a espai constant


 

Crea un qüestionari