方法1
include<stdio.h>
main()
{
int hour = 3;
int min = 55;
int pass_hour = 1;
int pass_min = 22;
int new_hour;
int new_min;
new_hour = hour + pass_hour + (min + pass_min)/60;
new_min = (min + pass_min) % 60;
printf("%d:%d
",new_hour , new_min);
}
方法2
include<stdio.h>
main()
{
int hour = 3;
int min = 55;
int pass_hour = 1;
int pass_min = 22;
int new_hour;
int new_min;
if(min + pass_min >= 60)
{
new_min = min + pass_min - 60;
new_hour = hour + pass_hour + 1;
}
if(min + pass_min < 60)
{
new_min = min + pass_min;
new_hour = hour + pass_hour;
}
printf("%d:%d
", new_hour , new_min);
}
方法3:(利用结构体封装)
include<stdio.h>
struct time add(struct time now, struct time pass);
struct time{
int hour;
int min;
};
main()
{
struct time now = {3, 55}, pass = {1, 33}, result;
result = add(now, pass);
printf("%d:%d
", result.hour, result.min);
}
struct time add(struct time now, struct time pass)
{
struct time rel;
rel.hour = now.hour + pass.hour + (now.min + pass.min)/60;
rel.min = (now.min + pass.min)%60;
return rel;
}