分啊
程序代码:
#include <stdio.h> #include <stdlib.h> #define TIME_TAB_MAX 10 #define ONEDAY 1440 char g_tab_time[TIME_TAB_MAX][6] = { "22:08", "08:31", "18:25", "21:35", "01:35", "23:59", "01:03", "09:34", "07:23", "09:59" }; char g_sleep_time[6] ="00:03"; int charconvertint(char c){ int i=c; switch(c) { case 48: return 0; case 49: return 1; case 50: return 2; case 51: return 3; case 52: return 4; case 53: return 5; case 54: return 6; case 55: return 7; case 56: return 8; case 57: return 9; default: return 0; } } int converttime(char *time){ return (charconvertint(time[0])*10+charconvertint(time[1]))*60+(charconvertint(time[3])*10+charconvertint(time[4])); } int nearTime(char sleep_time[],char tab_time[][6]){ int sleep_time_int=0; int tab_time_int=0; int sum[10]={0}; int i=0,j=0; int min=0; sleep_time_int=converttime(sleep_time); for(i=0;i<10;i++) { tab_time_int=converttime(tab_time[i]); if(sleep_time_int>tab_time_int) { sum[i]=((sleep_time_int-tab_time_int)<(tab_time_int+ONEDAY-sleep_time_int)?(sleep_time_int-tab_time_int):(tab_time_int+ONEDAY-sleep_time_int)); } else if(sleep_time_int<tab_time_int) { sum[i]=((tab_time_int-sleep_time_int)<(sleep_time_int+ONEDAY-tab_time_int)?(tab_time_int-sleep_time_int):(sleep_time_int+ONEDAY-tab_time_int)); } else return i; } i=0; min=sum[0]; while(i<10) { if(min>=sum[i]) { min=sum[i]; j=i; } i++; } return j; } int main(){ int k=0; k=nearTime(g_sleep_time,g_tab_time); printf("%s\n",g_tab_time[k]); } 抄袭了一点楼上的的,请不要介意。