Hướng dẫn time conversion python - trăn chuyển đổi thời gian

Giải pháp vấn đề chuyển đổi thời gian ở nhàHackerRank Time Conversion problem solutionHackerRank Time Conversion problem solution

Trong giải pháp vấn đề chuyển đổi thời gian của HackerRank & NBSP; bạn đã dành một thời gian ở định dạng 12 giờ sáng/chiều, bạn cần chuyển đổi nó thành thời gian quân sự (24 giờ).HackerRank Time Conversion problem solution, you have given a time in 12 hour AM/PM format, you need to convert it to military (24-hour) time.HackerRank Time Conversion problem solution, you have given a time in 12 hour AM/PM format, you need to convert it to military (24-hour) time.

Nội dung chính

  • Giải pháp vấn đề trong lập trình Python.
  • Giải pháp vấn đề trong lập trình Java.
  • Giải pháp vấn đề trong lập trình C ++.
  • Giải pháp vấn đề trong lập trình C.
  • Giải pháp vấn đề trong lập trình JavaScript.
  • Nhiệm vụ
  • Định dạng đầu vào
  • Hạn chế

Hướng dẫn time conversion python - trăn chuyển đổi thời gian

Giải pháp vấn đề trong lập trình Python.

#!/bin/python3

import os
import sys

#
# Complete the timeConversion function below.
#
def timeConversion(s):
    if s[-2:] == "AM" and s[:2] == "12": 
        return "00" + s[2:-2] 
    elif s[-2:] == "AM": 
        return s[:-2]
    elif s[-2:] == "PM" and s[:2] == "12": 
        return s[:-2] 
    else:
       ans = int(s[:2]) + 12
       return str(str(ans) + s[2:8]) 
        
    
if __name__ == '__main__':
    f = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()

    result = timeConversion(s)

    f.write(str(result) + '\n')

    f.close()

Giải pháp vấn đề trong lập trình Java.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String dt = sc.next();
        char ap = dt.charAt(dt.length() - 2);
        dt = dt.substring(0, dt.length() - 2);
        if (ap == 'A') {
            int hh = Integer.parseInt(dt.substring(0, 2));
            if (hh == 12) hh = 0;
            String s = Integer.toString(hh);
            if (s.length() == 1) {
                s = "0" + s;
            }
            System.out.println(s + dt.substring(2, dt.length()));
        } else {
            int hh = Integer.parseInt(dt.substring(0, 2));
            if (hh != 12) hh += 12;
            String s = Integer.toString(hh);
            if (s.length() == 1) {
                s = "0" + s;
            }
            System.out.println(hh + dt.substring(2, dt.length()));
        }
    }
}

Giải pháp vấn đề trong lập trình C ++.

#include 
#include 
#include 

using namespace std;
using std::vector;

void solve(){
	int hour, minute, second;
	char c1, c2;
	scanf("%d:%d:%d%c%c", &hour, &minute, &second, &c1, &c2);
	// printf("%d\n%d\n%d\n%c\n%c", hour, minute, second, c1, c2);
	hour = hour % 12;
	if (c1 == 'P'){
		hour = hour + 12;
	}
	printf("%02d:%02d:%02d\n", hour, minute, second);

	return;
}

int main(){

	solve();

    return 0;
}

Giải pháp vấn đề trong lập trình C.

#include 
#include 
#include 
#include 

int main() {
   char t[10];
   scanf("%s", t);

   if(t[8] == 'P') {
     if(t[0] != '1' || t[1] != '2') {
       t[0]++;
       t[1]+=2;
     }
   } else {
     if(t[0] == '1' && t[1] == '2') {
       t[0] = '0';
       t[1] = '0';
     }
   }
   t[8] = '\0';
   printf("%s\n", t);
    
   return 0;
}

Giải pháp vấn đề trong lập trình JavaScript.

function processData(input) {
    input = input.split(':');
    var hours = parseInt(input[0]);
    var timeFrame = input[2].slice(2);
    var seconds = input[2].slice(0,2);
    if ((timeFrame === 'PM') && (hours !== 12)) {
        hours += 12;
    }
    if ((hours === 12) && (timeFrame === 'AM')) {
        hours = '00';
    } else if (hours < 10) {
        hours = '0' + hours.toString();
    } else {
        hours = hours.toString();
    }
    console.log([hours, input[1], seconds].join(':'));
};

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
    _input += input;
});

process.stdin.on("end", function () {
   processData(_input);
});

Nhiệm vụTime Conversion HackerRank Solution which is a Part of HackerRank Algorithm Series.

  • Nhiệm vụ
  • Định dạng đầu vào
  • Hạn chế
  • Xin chào các lập trình viên, hôm nay chúng tôi sẽ giải quyết giải pháp HackerRank chuyển đổi thời gian, một phần của loạt thuật toán HackerRank.Time Conversion HackerRank Solution which is a Part of HackerRank Algorithm Series.
    • C++
    • Giải pháp - Chuyển đổi thời gian

Nhiệm vụ

Định dạng đầu vào12-hour AM/PM format, convert it to military (24-hour) time.

Hạn chế – 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.
– 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.

Xin chào các lập trình viên, hôm nay chúng tôi sẽ giải quyết giải pháp HackerRank chuyển đổi thời gian, một phần của loạt thuật toán HackerRank.Time Conversion HackerRank Solution which is a Part of HackerRank Algorithm Series.

  • Giải pháp - Chuyển đổi thời gian
    Return ’12:01:00′.
  • Python
    Return ’00:01:00′.

Định dạng đầu vào12-hour AM/PM format, convert it to military (24-hour) time.

Hạn chế – 12:00:00AM on a 12-hour clock is 00:00:00 on a 24-hour clock.– 12:00:00PM on a 12-hour clock is 12:00:00 on a 24-hour clock.

S = xông12: 01: 00 PM PhiênReturn12: 01: 00.Return ’12:01:00′.

  • S = xông12: 01: 00 Am hèReturn, 00: 00.Return ’00:01:00′.12 hour format

Mô tả chức năng

  • Hoàn thành chức năng & nbsp; TimeConVersion & nbsp; trong trình soạn thảo bên dưới.Nó sẽ trả về một chuỗi mới đại diện cho thời gian đầu vào ở định dạng 24 giờ.24 hour format

Định dạng đầu vào

Hạn chếs that represents a time in 12-hour clock format (i.e.: hh:mm:ssAM or hh:mm:ssPM).

Hạn chế

  • Xin chào các lập trình viên, hôm nay chúng tôi sẽ giải quyết giải pháp HackerRank chuyển đổi thời gian, một phần của loạt thuật toán HackerRank.Time Conversion HackerRank Solution which is a Part of HackerRank Algorithm Series.

Giải pháp - Chuyển đổi thời gian

07:05:45PM

Python

19:05:45

C++

#include
#include

using namespace std;

int main() {
    string s;
    cin >> s;

    int n = s.length();
    int hh, mm, ss;
    hh = (s[0] - '0') * 10 + (s[1] - '0');
    mm = (s[3] - '0') * 10 + (s[4] - '0');
    ss = (s[6] - '0') * 10 + (s[7] - '0');

    if (hh < 12 && s[8] == 'P') hh += 12;
    if (hh == 12 && s[8] == 'A') hh = 0;

    printf("%02d:%02d:%02d\n", hh, mm, ss);

    return 0;
}

Giải pháp - Chuyển đổi thời gian

import os
import sys

def timeConversion(s):
    time = s.split(":")
    if s[-2:] == "PM":
        if time[0] != "12":
            time[0] = str(int(time[0])+12)
    else:
        if time[0] == "12":
            time[0] = "00"
    ntime = ':'.join(time)
    return str(ntime[:-2])
    
if __name__ == '__main__':
    f = open(os.environ['OUTPUT_PATH'], 'w')

    s = input()

    result = timeConversion(s)

    f.write(result + '\n')

    f.close()

Python The above Problem (Time Conversion) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.