Time Conversion

Problem Statement

You are given time in AM/PM format. Can you convert this into a 24-hour format?

Input

Input consists of time in the AM/PM format i.e. hh:mm:ssAM or hh:mm:ssPM 
where 01≤hh≤12.

Sample: 07:05:45PM

Output

You need to print the time in a 24-hour format i.e. hh:mm:ss 
where 00≤hh≤23.

Sample output for the above input: 19:05:45

Note: Midnight is 12:00:00AM or 00:00:00. Noon is 12:00:00PM.

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT */
    string time="";
    cin>>time;

    if(time[time.size()-2]==‘A‘){
        if(time.substr(0,2)=="12") cout<<"00"<<time.substr(2,time.size()-4)<<endl;
        else cout<<time.substr(0,time.size()-2)<<endl;
    }

    if(time[time.size()-2]==‘P‘){
        if(time.substr(0,2)=="12") cout<<time.substr(0,time.size()-2)<<endl;
        else cout<<to_string(stoi(time.substr(0,2))+12)<<time.substr(2,time.size()-4)<<endl;
    }

    return 0;
}
时间: 2024-11-07 03:24:36

Time Conversion的相关文章

【Leet Code】ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSII

No.006:ZigZag Conversion

题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R Write the code that will take a string a

LeetCode 6 - ZigZag Conversion

原题如下: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHN

No.006 ZigZag Conversion

6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for bette

错误提示:LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt 的解决方法

最近在win7 系统下,打算利用 cmake 生成项目文件,然后用vs2010进行编译.但是在cmake的时候出现错误弹窗: 并出现错误信息:LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt 通过网上查找相关资料可知,问题出在cvtres.exe 这个文件上.LNK通过调用cvtres.exe来进行文件向coff格式转换. 网上讲通过搜索cvtres.exe 发现电脑中存在

迅雷、QQ旋风-链接自动转换(Download link conversion)

插件介绍: 在使用互联网的过程中,想必各位小伙伴都遇到过这样的情况,在花了不少时间找到自己需要的资源后,发现下载链接仅支持迅雷,而恰好电脑里又没有迅雷,又得花时间去找下载迅雷,其实这个问题很简单,只需要转换一下,迅雷.QQ旋风-链接自动转换这个插件就可以帮助你解决问题,它的作用就是将迅雷.QQ旋风这些软件的专用下载链接转换为普通的HTTP下载地址. 使用说明: 将迅雷.QQ旋风-链接自动转换(Download link conversion)添加至chrome,并在扩展器中启动. 功能介绍: -

NUMBER BASE CONVERSION(进制转换)

Description Write a program to convert numbers in one base to numbers in a second base. There are 62 different digits: { 0-9,A-Z,a-z } HINT: If you make a sequence of base conversions using the output of one conversion as the input to the next, when

View and Data API Tips : Conversion between DbId and node

By Daniel Du In View and Data client side API, The assets in the Autodesk Viewer have an object tree, a tree structure that represents the model hierarchy. Each element in model can be representing as a node of model tree. Each node has a dbId, this

ZigZag Conversion

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSII

[LeetCode]7. ZigZag Conversion ZigZag转换

The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H NA P L S I I GY I R And then read line by line: "PAHNAPLSIIGY