开始刷题,感觉代码量和基础太渣了,导致数据结构不会,重头慢慢刷吧~
1、题目
输入样例:
1120 110
输出样例:
1310
2、代码
#include<stdio.h>
int main(){
int m,n;
scanf("%d %d",&m,&n);
int p=m/100;
int q=m%100;
if((q+n)>0){
p+=(q+n)/60;
q=(q+n)%60;
}
else if((q+n)<-60){
p=p-1-(-(q+n))/60;
q=60-(-(q+n))%60;
}
else if(((q+n)>=-60)&&((q+n)<0)){
p=p-1;
q=60+(q+n);
}
else{
p=p;
q+=n;
}
printf("%d%02d",p,q);
return 0;
}
3、讨论
很简单的一个题,但是分了好多种可能,比如加起来超过60,加起来不到60,加起来大于-60但是小于0,加起来小于-60等等。
另外在网上找到一种思路,将时间转化为分钟数,操作之后,再换算成结束时间。
妙啊!!!
#include <stdio.h>
int main(){
int start, m1, m2, time, end;
scanf("%d %d", &start, &m1);
// 计算start表示的时间距离00:00有多少分钟
// 将小时数转换为分钟,避免出现未进位的情况
m2 = start / 100 * 60 + start % 100;
time = m2 + m1;
end = time / 60 *100 + time % 60;
printf ("%d", end);
return 0;
}