这个题目要求是,
#define TIME_TAB_MAX 6
char g_time_tab[TIME_TAB_MAX][6] = {
"13:46",
"08:23",
"23:12",
"05:59",
};
char g_sleep_time[6] = {
"13:12"
};
用c编一个程序,在g_time_tab中找到距离char g_sleep_time[6]里面时间最近的那个时间,并输出。
**************************************************************************************************************
代码
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include<stdio.h>
#include<math.h>
#define TIME_TAB_MAX 6
char g_time_tab[TIME_TAB_MAX][6] = {
"13:46",
"08:23",
"23:12",
"05:59",
};
char g_sleep_time[6] = {
"13:12"
};
int g_time_number(char *p )
{
int a[6];
int i,b;
for(i = 0;*p != '\0';i++,*(p++))
{
if(*p != ':')
{
a[i] = *p - '0';
//printf("a[%d] = %d\n",i,a[i]);
}
else
{
i--;
continue;
}
}
b = (a[0]*10+a[1]) * 60 + a[2] * 10 + a[3];
return b;
}
int main(void)
{
int a[TIME_TAB_MAX] ;
int i,m,n,k,temp,j;
n = 500;
k = 1;
m = g_time_number(g_sleep_time);
for(i = 0;i < TIME_TAB_MAX;i++)
{
a[i] = g_time_number(g_time_tab[i]);
//printf("%d\n",abs(a[i] - m));
if(abs(a[i] - m) < n)
{
n = abs(a[i] - m);
temp = a[i];
k = i;
}
}
printf("g_time_tab[%d] = %d:%d%d",k,temp/60,(temp%60)/10,(temp%60)%10);
printf("\n");
return 0;
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
代码
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++