NC16588. 时间(time)
描述
Apojacsleam是一个喜欢特殊时刻的人。
他定义了一个时刻,若电子表显示ab:ba(24小时制),则该时刻为“回文时刻”(可以有前导零)。例如00:00就是回文时刻。
输入描述
两个正整数,用“:”隔开,表示小时和分钟,保证输入时间合法。
输出描述
两行,两个时刻(不含前导0),用“:”隔开,表示上一个时刻和下一个时刻
示例1
输入:
09:33
输出:
5:50 10:1
示例2
输入:
23:32
输出:
22:22 0:0
C(clang11) 解法, 执行用时: 1ms, 内存消耗: 376K, 提交时间: 2020-11-04 21:05:40
#include <stdio.h> int main() { int ta,tb,t,a,b,c,d,i; scanf("%d:%d",&ta,&tb); for(i=-1;i<=1;i+=2) { t=ta*60+tb; t+=i; a=t/600; b=(t/60)%10; c=(t%60)/10; d=t%10; while(a!=d||b!=c) { t+=i; if(t==-1) t=1439; if(t==1440) t=0; a=t/600; b=(t/60)%10; c=(t%60)/10; d=t%10; } a==0?printf("%d:",b):printf("%d%d:",a,b); c==0?printf("%d\n",d):printf("%d%d\n",c,d); } }
C++14(g++5.4) 解法, 执行用时: 2ms, 内存消耗: 376K, 提交时间: 2020-07-23 15:51:16
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; int main() {int c,d; scanf("%d:%d",&c,&d); int a=c,b=d; while(1){ d--; if(d<0){ d=59; c-=1; if(c<0){c=23;}} if(c%10==d/10&&a/10==d%10){ printf("%d:%d\n",c,d); break;}} while(1){b++; if(b>59){b=0;a++; if(a>23){a=0;}} if(a%10==b/10&&a/10==b%10){ printf("%d:%d\n",a,b); break;} }return 0; }
Python3(3.5.2) 解法, 执行用时: 23ms, 内存消耗: 3460K, 提交时间: 2020-08-25 17:02:31
a,b=map(int,input().split(':')) h,m=a,b while True: m-=1 if m<0: h,m=h-1,59 if h<0: h,m=23,59 s='%02d:%02d'%(h,m) if s==s[::-1]: print('%d:%d'%(h,m)) break h,m=a,b while True: m+=1 if m>59: h,m=h+1,0 if h>23: h,m=0,0 s='%02d:%02d'%(h,m) if s==s[::-1]: print('%d:%d'%(h,m)) break
C++11(clang++ 3.9) 解法, 执行用时: 4ms, 内存消耗: 492K, 提交时间: 2018-07-31 21:47:40
#include <stdio.h> int main(){int a,b;scanf("%d:%d",&a,&b);int c=a,d=b;while(1){b++;if(b>=60){b=0;a++;if(a==24)a=0;}if(a%10==b/10&&a/10==b%10)break;}while(1){d-- ;if(d<0){d=59;c--;if(c<0)c= 23;}if(c%10==d/10&&c/10==d%10)break;}printf("%d:%d\n%d:%d\n",c,d,a,b) ;}