一个描述12小时制时间的字符串。所有的输入都是合理的,不用考虑输入不合理的情况。
一个描述24小时制时间的字符串。
08:03:45PM
20:03:45
/*
思路:十二小时制:AM中从0-11都是二十四小时制一样 12AM改成00:00:00
十二小时制的PM则需要开始变化,把每个时辰都加上12(时位)
*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] str = br.readLine().split(":");
//分情况讨论
//①AM且小于12:00:00
int hour = Integer.parseInt(str[0]);
char ch = str[2].charAt(2);
if(hour<12 && ch == 'A'){
System.out.println(str[0] + ":" + str[1] + ":" + str[2].substring(0,2));
}else if(hour == 12 && ch == 'A'){
System.out.println("00" + ":" + str[1] + ":" + str[2].substring(0,2));
}else if(hour <12 && ch == 'P'){
hour = hour+12;
System.out.println(String.valueOf(hour) + ":" + str[1] + ":" + str[2].substring(0,2));
}else if(hour == 12 && ch == 'P'){
System.out.println("12" + ":" + str[1] + ":" + str[2].substring(0,2));
}
}
} #include <bits/stdc++.h>
using namespace std;
int main(){
string s,t;
cin>>s;
int l = s.length();
t = s.substr(0,l-2);
if(s.substr(l-2,2)=="AM"){
if(t=="12:00:00")
cout<<"00:00:00"<<endl;
else
cout<<t<<endl;
}else if(s.substr(l-2,2)=="PM"){
if(t=="12:00:00")
cout<<t<<endl;
else{
int h = stoi(t.substr(0,2));
cout<<h+12<<t.substr(2,l-2)<<endl;
}
}
return 0;
} #include <bits/stdc++.h>
using namespace std;
int main(){
string str;
cin>>str;
if(str.substr(8,2)=="AM"){
if(str[0]=='1'&&str[1]=='2')
cout<<"00:00:00";
else
cout<<str.substr(0,8);
}
else{
if(str[0]=='1'&&str[1]=='2')
cout<<str.substr(0,8);
else{
int h=stoi(str.substr(0,2));
h+=12;
cout<<h<<str.substr(2,6);
}
}
return 0;
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
string s;
cin>>s;
int shi=0,fen=0,miao=0,f=0;
string t=s.substr(s.size()-2),s1=s.substr(2,6),s2=s.substr(0,8);
for(int i=0;i<2;i++)
shi=shi*10+s[i]-'0';
if(t=="AM")
{
if(shi==12)
cout<<"00"<<s1<<endl;
else
cout<<s2<<endl;
}
else
{
if(shi==12)
cout<<s2<<endl;
else
{
if((shi+12)%24<10)
cout<<"0";
cout<<(shi+12)%24<<s1<<endl;
}
}
return 0;
} 除去两个特例,其他都符合常识
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
int hour = Integer.parseInt(str.substring(0, 2));
String min_sec = str.substring(2, 8);
if (str.equals("12:00:00AM")) {
System.out.println("00:00:00");
} else if (str.equals("12:00:00PM")) {
System.out.println("12:00:00");
} else if (str.charAt(8) == 'P') {
System.out.println((hour + 12) + min_sec);
} else {
System.out.println(str.substring(0, 8));
}
}
}
class MainActivity:
def main(self):
# Read the data
s = input()
# Transform
if s == '12:00:00AM':
print('00:00:00')
elif s == '12:00:00PM':
print(s.rstrip('PM'))
else:
if s.endswith('AM'):
print(s.rstrip('AM'))
else:
s = s.rstrip('PM')
nums = s.split(':')
nums[0] = str(int(nums[0]) + 12).zfill(2)
print(':'.join(nums))
if __name__ == '__main__':
M = MainActivity()
M.main() #include<cstdio>
int main(void) {
int hour,min,s;
char ss[3];
scanf("%d:%d:%d%s",&hour,&min, &s, ss);
if(*ss =='A') {
if(hour==12) hour=0;
printf("%02d:%02d:%02d",hour,min,s);
}else {
hour+=12;
if(hour==24) hour = 12;
printf("%02d:%02d:%02d",hour,min,s);
}
return 0;
} //用了stoi函数,剩下的就是注意12:00:00AM和12:00:00PM两个时刻了。比较简单。
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s,temp;
while (cin >> s)
{
temp = s.substr(0, 2);
int i = stoi(temp, 0, 10);
if (s[8] == 'A'&&s!="12:00:00AM")
{
cout << s.substr(0, 8) << endl;
}
else if(s[8]=='P'&&s!= "12:00:00PM")
{
i += 12;
cout << i << s.substr(2, 6) << endl;
}
else if (s == "12:00:00AM")
{
cout << "00:00:00" << endl;
}
else if (s == "12:00:00PM")
{
cout << "12:00:00" << endl;
}
}
} //原来12:00:00AM是凌晨啊
import java.util.Scanner;
public class Main{
public static String change(String time){
Character c=time.charAt(time.length()-2);
int hour=Integer.valueOf(time.substring(0,2));
if(c=='P'){
if(hour!=12)
hour+=12;
}
if(c=='A'&&hour==12)
return "00"+time.substring(2,time.length()-2);
if(hour>=10)
return hour+time.substring(2,time.length()-2);
else
return "0"+hour+time.substring(2,time.length()-2);
}
public static void main(String[] args){
Scanner sc=new Scanner(System.in );
String time=sc.nextLine();
System.out.println(change(time));
}
} /*
这种写法是不是有点sha?
*/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <limits.h>
#include <stack>
#include <unordered_map>
#include <map>
#include <queue>
using namespace std;
void TimeScaleConversion() {
string s;
cin >> s;
int len = s.size();
int h = 0;
string h0, h1, res;
if (s == "00:00:00AM" || s == "12:00:00AM")
{
res = "00:00:00";
}
else if(s == "12:00:00PM" || s == "00:00:00PM")
{
res = "12:00:00";
}
else if (s[8] == 'A')
{
h = 10 * (s[0] - '0') + (s[1] - '0');
h1 = h % 10 + '0';
h0 = h / 10 % 10 + '0';
res = h0 + h1 + s.substr(2, 6);
}
else
{
h = 10 * (s[0] - '0') + (s[1] - '0') + 12;
h1 = h % 10 + '0';
h0 = h / 10 % 10 + '0';
res = h0 + h1 + s.substr(2, 6);
}
cout << res << endl;
}
int main(){
TimeScaleConversion(); return 0;
}
#include<iostream>
(720)#include<string>
using namespace std;
int main(void){
int hour, min, sec;
string str;
char c;
string shour, smin, ssec;
cin>>hour>>c>>min>>c>>sec>>str;
if (hour == 12 && min == 0 && sec == 0 && str == "AM"){
cout<<"00:00:00"<<endl;
return 0;
}
else if(hour == 12 && min == 0 && sec == 0 && str == "PM"){
cout<<"12:00:00"<<endl;
return 0;
}
if (str == "PM")
shour = to_string(hour + 12);
else if(hour < 10){
shour = to_string(0) + to_string(hour);
}
else{
shour = to_string(hour);
}
if (min < 10){
smin = to_string(0) + to_string(min);
}
else
smin = to_string(min);
if (sec < 10){
ssec = to_string(0) + to_string(sec);
}
else
ssec = to_string(sec);
cout<<shour<<":"<<smin<<":"<<ssec<<endl;
return 0;
} s = input()
if s == '12:00:00AM':
print('00:00:00')
elif s == '12:00:00PM':
print('12:00:00')
elif s[:2] == '12' and s[-2:] == 'PM':
print(s[:-2])
elif s[:2] == '12' and s[-2:] == 'AM':
print('00'+s[2:-2])
else:
if s[-2:] == 'AM':
print(s[:-2])
else:
hour = int(s[:2])
hour += 12
print(str(hour)+s[2:-2]) #include<bits/stdc++.h>
using namespace std;
int main()
{
string str;
cin >> str;
int len = str.size();
if(str[len-2] == 'A')
{
if(str.substr(0,2) == "12") cout << "00"+str.substr(2,len-4) << endl;
else cout << str.substr(0, len-2);
}
else{
if(str.substr(0,2) == "12") cout << str.substr(0,len-2) <<endl;
else cout << to_string(stoi(str.substr(0,2))+12) <<str.substr(2,len-4) <<endl;
}
return 0;
}
oldstyle = input() newstyle = "" if oldstyle == "12:00:00AM": newstyle = "00:00:00" elif oldstyle == "12:00:00PM": newstyle = "12:00:00" elif oldstyle[-2:] == "AM": newstyle = oldstyle[:-2] else: hour = int(oldstyle[:2]) hour = 12 + hour newstyle = str(hour) + oldstyle[2:-2] print(newstyle)