水DP,不过这道题目的数据好像有问题。24时制可表示范围是0~23,12时制可表示范围应为1~12。也就是说24点(即0点)其实表示为12:00:00am,而中午12:00:00其实表示为12:00:00pm,而且从12:00:00~12:59:59都是12:xx:xx pm,而13点开始则应该表示为01:xx:xx pm。题目数据比较小,虽然貌似不考虑这个也可以ac。但是,我个人是按规范做的。
#include <stdio.h> #define MAXNUM 2005 int times[MAXNUM];
int adjacent[MAXNUM];
int dp[MAXNUM]; int mymin(int a, int b) {
return a<b ? a:b;
} void test(int *sec, int *min, int *hour) {
*sec = ;
*min = ;
*hour = ;
} int main() {
int case_n, n;
int i, sec, min, hour;
char str[]; scanf("%d", &case_n);
str[] = '\0';
while (case_n--) {
scanf("%d", &n);
for (i=; i<=n; ++i)
scanf("%d", ×[i]);
for (i=; i<n; ++i)
scanf("%d", &adjacent[i]);
dp[] = ;
dp[] = times[];
for (i=; i<=n; ++i)
dp[i] = mymin(dp[i-]+times[i], dp[i-]+adjacent[i-]); sec = dp[n];
hour = ;
min = sec / ; sec = sec % ;
hour += min / ; min = min%;
//test(&sec, &min, &hour);
if (hour >= )
hour = hour%;
if (hour>=) {
if (hour > )
hour -= ;
printf("%02d:%02d:%02d pm\n", hour, min, sec);
} else {
if (hour == )
hour = ;
printf("%02d:%02d:%02d am\n", hour, min, sec);
}
} return ;
}