大大体题意:
有12小时进制和24小时进制的时间,给你一个时间,hh:mm,如果合法 原样输出,否则修改最少的数字 改成合法的时间!
思路:
算不上贪心,想清楚就可以了!
其中分钟最简单, 如果 大于59 直接输出 0 + 个位数即可!
对于小时来说:
如果是24小时进制的: 如果大于23 直接变成 0 + 个位数!
如果是12小时进制的: 如果等于0 或者大于12 : 如果是10的倍数 就要变成 10,不是10的倍数 变成 0 + 个位数
合法直接输出即可!
#include <bits/stdc++.h>
using namespace std;
int main(){
int d;
int h,m;
scanf("%d",&d);
scanf("%d:%d",&h,&m);
if (d == 24){
if (h > 23) printf("0%d:",h%10);
else printf("%02d:",h);
if (m > 59){
printf("0%d",m%10);
}
else printf("%02d",m);
}
else {
if (h > 12 || !h){
if (h % 10 == 0)printf("10:");
else printf("0%d:",h%10);
if (m > 59){
printf("0%d",m%10);
}
else printf("%02d",m);
}else {
printf("%02d:",h);
if (m > 59){
printf("0%d",m%10);
}
else printf("%02d",m);
}
}
puts("");
return 0;
}