** 时间类型 - 6. 求总秒数 **
请编写函数,求所给时间对应的当天总秒数。
函数原型
int TotalSecond(const MYTIME *time);
说明:参数 time 为指示时间结构变量的指针。函数值为当天总秒数。
裁判程序
#include <stdio.h>
#define hoursPerDay 24
#define minutesPerDay 1440
#define secondsPerDay 86400
#define minutesPerHour 60
#define secondsPerHour 3600
#define secondsPerMinute 60
…
int TotalSecond(const MYTIME *time);
int main()
{
MYTIME a;
TimeInput(&a);
printf("%d\n", TotalSecond(&a));
return 0;
}
输入样例1
0:0:0
输出样例1
0
输入样例2
23:59:59
输出样例2
86399
一开始有点懵,后来发现挺简单的,需要写出完整代码
代码如下:
#include <stdio.h>
#define hoursPerDay 24
#define minutesPerDay 1440
#define secondsPerDay 86400
#define minutesPerHour 60
#define secondsPerHour 3600
#define secondsPerMinute 60
typedef struct{
int second;
int minute;
int hour;
}MYTIME;
int TotalSecond(const MYTIME *time);
int main()
{
MYTIME a;
TimeInput(&a);
printf("%d\n", TotalSecond(&a));
return 0;
}
int TotalSecond(const MYTIME *time)
{ return (time->hour)*secondsPerHour+(time->minute)*secondsPerMinute+time->second;
}
TimeInput(const MYTIME *time)
{ scanf("%d:%d:%d",&(time->hour),&(time->minute),&(time->second));
}
提交代码
int TotalSecond(const MYTIME *time)
{ return (time->hour)*secondsPerHour+(time->minute)*secondsPerMinute+time->second;
}