WY51. 牛牛的闹钟
描述
牛牛总是睡过头,所以他定了很多闹钟,只有在闹钟响的时候他才会醒过来并且决定起不起床。从他起床算起他需要X分钟到达教室,上课时间为当天的A时B分,请问他最晚可以什么时间起床输入描述
每个输入包含一个测试用例。输出描述
输出两个整数表示牛牛最晚起床时间。示例1
输入:
3 5 0 6 0 7 0 59 6 59
输出:
6 0
C 解法, 执行用时: 2ms, 内存消耗: 336KB, 提交时间: 2019-12-03
#include <stdlib.h> #include <stdio.h> #include <string.h> void get_clock(char clock[][8], int h, int m) { int byte = m / 8; int bit = m % 8; char *p_clock = &clock[h][byte]; int counter = 0; while( (*p_clock) == 0) { p_clock--; counter++; } if (counter != 0) { bit = 7; if (byte >= counter) byte -= counter; else { int rm_h = counter / 8; int rm_b = counter % 8; h -= rm_h; if (byte >= rm_b) byte -= rm_b; else { h--; byte = 8 + byte - rm_b; } } } while (1) { int tmp = (*p_clock) & (1 << bit); if (tmp) break; else bit--; if (bit < 0) { bit = 7; again: p_clock--; byte--; if (byte < 0) { byte = 7; h--; } if ( (*p_clock) == 0) goto again; } } m = byte * 8 + bit; printf ("%d %d\n", h, m); } int main(int argc, char **argv) { char clock_record[24][8]; memset(clock_record, 0, sizeof(clock_record)); int n; scanf("%d", &n); int i = 0; int h = 0; int m = 0; while (i < n) { scanf("%d %d", &h, &m); int l = m / 8; int r = m % 8; clock_record[h][l] |= (1 << r); i++; } int x, a, b; scanf("%d", &x); scanf("%d %d", &a, &b); if (x > b) { a--; b = 60 + b - x; } else b = b - x; get_clock(clock_record, a, b); return 0; }
C 解法, 执行用时: 2ms, 内存消耗: 344KB, 提交时间: 2019-08-03
int main() { int n; scanf("%d",&n); int time[n]; int hour,mine; for(int i=0;i<n;i++) { scanf("%d %d",&hour,&mine); time[i] = hour*60+mine; } int get_time; scanf("%d",&get_time); int lasthour,lastmine; scanf("%d %d",&lasthour,&lastmine); int lasttime = lasthour*60 + lastmine; int max=-1,maxhour=0,maxmine=0; for(int i=0;i<n;i++) { if(time[i]+get_time<=lasttime && time[i]>max) { max = time[i]; maxhour = time[i]/60; maxmine = time[i]-maxhour*60; } } printf("%d %d",maxhour,maxmine); }