Suppose a bus schedule is given for a certain day, in the form of a list of numerical values. Determine the bus nearest to the current time.
How to do it? I would like to see an example of code on something C .
Well, something on the bare C:
char * schedule[] = { "8:25", "10:15", "14:20", "18:20", "19:40" }; char * current_time[] = { "05:15", "08:25", "17:40", "20:15" }; int main(int argc, const char * argv[]) { for(int i = 0; i < sizeof(current_time)/sizeof(current_time[0]); ++i) { int h,m; int found = 0; sscanf(current_time[i],"%d:%d",&h,&m); int ct = h*60+m; for(int j = 0; j < sizeof(schedule)/sizeof(schedule[0]); ++j) { sscanf(schedule[j],"%d:%d",&h,&m); int st = h*60+m; if (ct <= st) { printf("Current time: %s, nearest bus at %s\n", current_time[i],schedule[j]); found = 1; break; } } if (!found) { printf("Current time: %s, nearest bus at %s tomorrow\n", current_time[i],schedule[0]); } } } As I understand it, he is interested in the nearest one in the future, and not the one who left a minute ago? :)
Source: https://ru.stackoverflow.com/questions/617211/
All Articles
lsearchbsearchhere are examples on with like - nick_n_a