PAT基础编程题目-7-7 12-24小时制
PAT基础编程题目-7-7 12-24小时制
题目详情
解答
C语言版
#include<stdio.h>
int main() {
int hour, minute;
scanf("%d:%d", &hour, &minute);
if (hour<12) // 上午
{
printf("%d:%d AM", hour, minute);
}
else if(hour > 12) {
// 下午
hour = hour - 12;
printf("%d:%d PM", hour, minute);
}
else {
// 中午12点
printf("%d:%d PM", hour, minute);
}
return 0;
}
C++版
#include<iostream>
using namespace std;
int main() {
int hour, minute;
char colon;
cin >> hour>>colon>>minute;
if (hour < 12) // 上午
{
cout << hour << ":" << minute << " AM" << endl;
}
else if (hour > 12) {
// 下午
hour = hour - 12;
cout << hour << ":" << minute << " PM" << endl;
}
else {
// 中午12点
cout << hour << ":" << minute << " PM" << endl;
}
return 0;
}
Java版
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
int hour=0,minute=0;
String time = null;
Scanner scanner = new Scanner(System.in);
if (scanner.hasNext()) {
time = scanner.nextLine();
}
scanner.close();
/** * split(":", 2)表示按:分隔成2份,返回字符串数组 * Integer.parseInt()把字符串类型转为整型 */
hour = Integer.parseInt(time.split(":", 2)[0]);
minute = Integer.parseInt(time.split(":", 2)[1]);
if (hour<12) // 上午
{
System.out.println(hour+":"+minute+" AM");
}
else if(hour > 12) {
// 下午
hour = hour - 12;
System.out.println(hour+":"+minute+" PM");
}
else {
// 中午12点
System.out.println(hour+":"+minute+" PM");
}
}
}
创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!