我刚刚用KN King的C编程:一个现代的方法来解决这个问题。这不是家庭作业,我只是在教自己出书......
下表显示了从一个城市到另一个城市的每日航班:
出发时间 - 到达时间
上午8:00 - 上午10:16
上午9:43 - 上午11:52 上午11:19 - 下午1:31 下午12:47 - 下午3:00 下午2:00 - 下午4:08
下午3:45 - 下午5:55 晚上7点到9点20分 晚上9:45 - 晚上11:58编写一个程序,要求用户输入时间(以小时和分钟表示,使用24小时制)。然后程序显示出发时间最接近用户输入的航班的出发和到达时间:
输入24小时: 13:15
最近的缩放时间是下午12:47,到达下午3:00。提示:将输入转换为自午夜以分钟为单位的时间,并将其与出发时间进行比较,也以午夜时分后的分钟数表示。例如,13:15是从午夜开始的13 x 60 + 15 = 795分钟,接近下午12:47(午夜767分钟),而不是其他任何出发时间。
到目前为止,我们只介绍了基本的比较表达式以及if和switch语句,所以我的答案必须基于这些,而且没什么太奇特的。我想出的代码如下,我想知道是否有人愿意看看它,看看我是否在正确的轨道上,它似乎工作但似乎是很多代码对于这么小的事情。也许只是教我们所涉及的逻辑。我没有粘贴整个代码,其余的一遍又一遍,因为它比较了值。我没有编程经验,所以请保持温和!
谢谢你的时间, 安德鲁
#include <stdio.h>
int main (int argc, const char * argv[]) {
// Flight departure times since midnight
// 8am, 9:45am, 11:19am, 12:47pm
// 2pm, 3:45pm, 7pm, 7:45pm
int a = 480, b = 585, c = 679, d = 767,
e = 840, f = 945, g = 1140, h = 1185;
// Flight arrival times for respective departure times.
int a1 = 616, b1 = 712, c1 = 811, d1 = 900,
e1 = 968, f1 = 1075, g1 = 1280, h1 = 1438;
int hours, minutes, time, t, u;
// Get the users time
printf("Enter a 24 hour time (hh:mm): \n");
scanf("%d:%d", &hours, &minutes);
time = hours * 60 + minutes;
printf("Closest departure time is ");
if (time <= a)
printf("8:00am");
else
if (time > a && time <= b) {
t = time - a;
u = b - time;
if (t < u) {
printf("%.2d:%.2d", a / 60, a % 60);
if (a / 60 == 0)
printf("am");
else if (a / 60 < 12)
printf("am");
else if (a / 60 == 12)
printf("pm");
else
printf("pm");
printf(", arriving at %d:%.2d", a1 / 60, a1 % 60);
if (a1 / 60 == 0)
printf("am");
else if (a1 / 60 < 12)
printf("am");
else if (a1 / 60 == 12)
printf("pm");
else
printf("pm");
}
else {
printf("%.2d:%.2d", b / 60, b % 60);
if (b / 60 == 0)
printf("am");
else if (b / 60 < 12)
printf("am");
else if (b / 60 == 12)
printf("pm");
else
printf("pm");
printf(", arriving at %d:%.2d", b1 / 60, b1 % 60);
if (b1 / 60 == 0)
printf("am");
else if (b1 / 60 < 12)
printf("am");
else if (b1 / 60 == 12)
printf("pm");
else
printf("pm");
}
}
根据我的建议进行更改:(感谢xamypah和Gabe) ...
int hours, minutes, time, t, u, x, y;
// Get the users time
printf("Enter a 24 hour time (hh:mm): \n");
scanf("%d:%d", &hours, &minutes);
time = hours * 60 + minutes;
printf("Closest departure time is ");
if (time <= a)
printf("8:00am");
else
if (time > a && time <= b) {
t = time - a;
u = b - time;
if (t <= u) {
x = a;
y = a1;
}
else {
x = b;
y = b1;
}
然后在程序结束之后的几个以上:
printf("%.2d:%.2d", x / 60, x % 60);
if (x / 60 < 12)
printf("am");
else
printf("pm");
printf(", arriving at %d:%.2d", y / 60, y % 60);
if (y / 60 < 12)
printf("am");
else
printf("pm");
实际上我不得不对那个结束的打印声明进行一些更改,否则它会以24小时格式显示时间:am和pm:
if (x / 60 < 12)
printf("%d:%.2d am", x / 60, x % 60);
else
printf("%d:%.2d pm", (x / 60) - 12, x % 60);
printf(", arriving at ");
if (y / 60 < 12)
printf("%d:%.2d am", y / 60, y % 60);
else
printf("%d:%.2d pm", (y / 60) - 12, y % 60);
答案 0 :(得分:3)
嗯,是的,你是在正确的轨道上,但你也可以大大减少代码的大小。 看,我认为,同一条代码在你的程序中经常重复:
printf("%.2d:%.2d", a / 60, a % 60);
if (a / 60 == 0)
printf("am");
else if (a / 60 < 12)
printf("am");
else if (a / 60 == 12)
printf("pm");
else
printf("pm");
printf(", arriving at %d:%.2d", a1 / 60, a1 % 60);
if (a1 / 60 == 0)
printf("am");
else if (a1 / 60 < 12)
printf("am");
else if (a1 / 60 == 12)
printf("pm");
else
printf("pm");
对于a
,b
等重复{...}。
通常在执行结束时完成。
所以,如果你在两个方面打破你的算法:首先 - 找到合适的时间范围,第二个 - 打印出来,然后你就可以摆脱这个副本/粘贴。
答案 1 :(得分:2)
仔细看看这段代码:
if (b / 60 == 0)
printf("am");
else if (b / 60 < 12)
printf("am");
你知道为什么它是多余的吗?
自
0 < 12
以来,只要b / 60 == 0
为真,b / 60 < 12
也将为真。 您可以将代码缩减为if (b / 60 < 12) printf("am"); else printf("pm");
答案 2 :(得分:1)
明智的答案需要使用数组和函数。你似乎也不是很近看。
数据可以这样表示,使用宏来隐藏重复计算:
#define TIME(hh, mm) ((hh) * 60 + (mm))
static const int dep[] = { TIME( 8, 0), TIME( 9, 45), TIME(11, 19), TIME(12, 47),
TIME(14, 0), TIME(15, 45), TIME(19, 0), TIME(19, 45) };
static const int arr[] = { TIME(10, 16), TIME(11, 52), TIME(13, 31), TIME(15, 0),
TIME(16, 8), TIME(17, 55), TIME(21, 20), TIME(23, 58) };
enum { NUM_TIMES = sizeof(dep) / sizeof(dep[0]) };
您还应该使用函数将时间格式化为字符串。请注意,上午/下午时间很奇怪;你必须用12代替0(所以12:59 am就在凌晨1:00之前)。该函数假定传递的缓冲区足够大(最少9个字节)来获取格式化的字符串。
char *timestr(int hhmm, char *buffer)
{
int hh = (hhmm / 60) % 12;
if (hh == 0)
hh = 12;
sprintf(buffer, "%.2d:%.2d %s", hh, hhmm % 60, (hhmm < TIME(12, 0)) ? "am" : "pm");
return buffer;
}
现在,给定读取的值,您可以非常简单地找到正确的值:
void find_and_print_nearest_time(int time)
{
int i = 0;
if (time < dep[i])
// Before first departure
print_times(time, dep[i], arr[i]);
else if (time >= dep[NUM_TIMES-1])
// After last departure
print_times(time, dep[NUM_TIMES-1], arr[NUM_TIMES-1]);
else
{
// Somewhere in the middle
for (i = 0; i < NUM_TIMES - 1; i++)
{
if (time >= dep[i+1])
continue;
if (time < (dep[i] + dep[i+1]) / 2)
print_times(time, dep[i], arr[i]);
else if (time < dep[i+1])
print_times(time, dep[i+1], arr[i+1]);
break;
}
}
}
void print_times(int u_time, int dep, int arr)
{
char buffer1[9];
char buffer2[9];
char buffer3[9];
printf("Given %s, the nearest departure time is %s, arriving at %s\n",
timestr(u_time, buffer1), timestr(dep, buffer2), timestr(arr, buffer3));
}
只留下main()
写:
#include <stdio.h>
int main(void)
{
int hours, minutes, time;
// Get the user's time
printf("Enter a 24 hour time (hh:mm): \n");
if (scanf("%d:%d", &hours, &minutes) == 2)
{
time = hours * 60 + minutes;
find_and_print_nearest_time(time);
}
return(0);
}
或者,使用循环(更容易进行测试!):
int main(void)
{
int hours, minutes, time;
while (scanf("%d:%d", &hours, &minutes) == 2)
{
// Should check 0 <= hours <= 23
// Should check 0 <= minutes <= 59
// Might accept 24:00
time = TIME(hours, minutes);
find_and_print_nearest_time(time);
}
return(0);
}
测试时,我按照略有不同的顺序组装了一些东西,但代码的工作方式与我第一次的预期相同。给定输入数据:
00:00
06:00
08:00
08:50
08:51
08:52
08:53
08:54
08:55
09:42
09:43
09:44
10:30
10:31
10:32
10:33
14:50
14:51
14:52
14:53
14:54
14:55
21:44
21:45
21:46
23:59
输出结果为:
Given 12:00 am, the nearest departure time is 08:00 am, arriving at 10:16 am
Given 06:00 am, the nearest departure time is 08:00 am, arriving at 10:16 am
Given 08:00 am, the nearest departure time is 08:00 am, arriving at 10:16 am
Given 08:50 am, the nearest departure time is 08:00 am, arriving at 10:16 am
Given 08:51 am, the nearest departure time is 08:00 am, arriving at 10:16 am
Given 08:52 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 08:53 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 08:54 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 08:55 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 09:42 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 09:43 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 09:44 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 10:30 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 10:31 am, the nearest departure time is 09:45 am, arriving at 11:52 am
Given 10:32 am, the nearest departure time is 11:19 am, arriving at 01:31 pm
Given 10:33 am, the nearest departure time is 11:19 am, arriving at 01:31 pm
Given 02:50 pm, the nearest departure time is 02:00 pm, arriving at 04:08 pm
Given 02:51 pm, the nearest departure time is 02:00 pm, arriving at 04:08 pm
Given 02:52 pm, the nearest departure time is 03:45 pm, arriving at 05:55 pm
Given 02:53 pm, the nearest departure time is 03:45 pm, arriving at 05:55 pm
Given 02:54 pm, the nearest departure time is 03:45 pm, arriving at 05:55 pm
Given 02:55 pm, the nearest departure time is 03:45 pm, arriving at 05:55 pm
Given 09:44 pm, the nearest departure time is 07:45 pm, arriving at 11:58 pm
Given 09:45 pm, the nearest departure time is 07:45 pm, arriving at 11:58 pm
Given 09:46 pm, the nearest departure time is 07:45 pm, arriving at 11:58 pm
Given 11:59 pm, the nearest departure time is 07:45 pm, arriving at 11:58 pm
答案 3 :(得分:0)
我认为你需要一个循环表达式来排序和计算最近的时间点。 您可以将条件表达式打包到函数中。 像这样:
char* getTimeSuffix(int time) {
char* ret = "pm"
if ((time/60 == 0) || (time/60 < 12))
return "am";
return ret;
}
答案 4 :(得分:0)
我的方法:
#include <stdio.h>
int main(void)
{
int hours, minutes, time;
printf("Enter a 24-hour time: ");
scanf("%d:%d", &hours, &minutes);
time = hours * 60 + minutes;
if(time < /*hour in minutes*/8*60+/*minutes*/0+((/*minutes to the next hour*/60-0+/*more hours in minutes?*/0+/*extra minutes*/43)/*dividing by 2 gives the half of time*/2.0)){
printf("Closest departure time is 8:00 a.m, arriving at 10:16 a.m.\n");
}
else if(time < 9*60+43+((60-43+60+19)/2.0)){
printf("Closest departure time is 9:43 a.m, arriving at 11:52 a.m.\n");
}
else if(time < 11*60+19+((60-19+0+47)/2.0)){
printf("Closest departure time is 11:19 a.m, arriving at 1:31 p.m.\n");
}
else if(time < 12*60+47+((60-47+60+0)/2.0)){
printf("Closest departure time is 12:47 p.m, arriving at 3:00 p.m.\n");
}
else if(time < 14*60+0+((60-0+0+45)/2.0)){
printf("Closest departure time is 2:00 p.m, arriving at 4:08 p.m.\n");
}
else if(time < 15*60+45+((60-45+60*3+0)/2.0)){
printf("Closest departure time is 3:45 p.m, arriving at 5:55 p.m.\n");
}
else if(time < 19*60+45+((60-0+60+45)/2.0)){
printf("Closest departure time is 7:00 p.m, arriving at 9:20 p.m.\n");
}
else{
printf("Closest departure time is 9:45 p.m, arriving at 11:58 p.m.\n");
}
return 0;
}
答案 5 :(得分:0)
进行更改以使其更完美
int main()
{
int h,m,t,temp1,temp2,
a1,a2,d1,d2; //a1,a1 & d1,d2 are ariival & dep. time res.
//Departure times
// dt1 = 480, dt2 = 583, dt3 = 679, dt = 767,
// dt5 = 840, dt6 = 945, dt = 1140, dt = 1305;
//Arrival times
// at1 = 616, at2 = 712, at3 = 811, at4 = 900,
// at5 = 968, at6 = 1075, at7 = 1280, at8 = 1438;
printf("Enter a 24-hour time(hh:mm): ");
scanf("%d:%d",&h,&m);
t = h*60 + m;
printf("Closest departure time is ");
//CONDITIONS FOR CLOSEST DEPARTURE TIME.
if(t <= 480)
{
temp1 = 480 - t;
temp2 = t + 135; //Since time is 9:45p.m and not midnight;
d1 = 480; d2 = 1305;
a1 = 616; a2 = 1438;
}
else if(t <= 583)
{
temp1 = 583 - t;
temp2 = t - 480;
d1 = 583; d2 = 480;
a1 = 712; a2 = 616;
}
else if(t <= 679)
{
temp1 = 679 - t;
temp2 = t - 582;
d1 = 679; d2 = 582;
a1 = 811; a2 = 712;
}
else if(t <= 767)
{
temp1 = 767 - t;
temp2 = t - 679;
d1 = 767; d2 = 679;
a1 = 900; a2 = 811;
}
else if(t <= 840)
{
temp1 = 840 - t;
temp2 = t - 767;
d1 = 840; d2 = 767;
a1 = 968; a2 = 900;
}
else if(t <= 945)
{
temp1 = 945 - t;
temp2 = t - 840;
d1 = 945; d2 = 840;
a1 = 1075; a2 = 968;
}
else if(t <= 1140)
{
temp1 = 1140 - t;
temp2 = t - 945;
d1 = 1140; d2 = 945;
a1 = 1280; a2 = 1075;
}
else if(t <= 1305 || t >= 1305)
{
temp1 = 1305 - t;
temp2 = t - 1140;
d1 = 1305; d2 = 1140;
a1 = 1438; a2 = 1280;
}
// COMPUTING CLOSEST DEPARTURE TIME.
if(temp1 < temp2)
{
h = d1/60;
m = d1%60;
}
else
{
h = d2/60;
m = d2%60;
}
//PRINTING CLOSEST DEPARTURE TIME.
if(h < 12)
printf("%.2d:%.2d a.m.",h,m);
else
{
if(h == 12)
printf("%.2d:%.2d p.m.",h,m);
else
printf("%.2d:%.2d p.m.",h - 12,m);
}
//COMPUTING CORRESPONDING ARRIVAL TIME.
printf(", arriving at ");
if(temp1 < temp2)
{
h = a1/60;
m = a1%60;
}
else
{
h = a2/60;
m = a2%60;
}
//PRINTING CORRESPONDING ARRIVAL TIME.
if(h < 12)
printf("%.2d:%.2d a.m.",h,m);
else
{
if(h == 12)
printf("%.2d:%.2d p.m.",h,m);
else
printf("%.2d:%.2d p.m.",h - 12,m);
}
getch();
}
答案 6 :(得分:-1)
1.编写一个程序,询问用户12小时的时间,以24小时的形式显示时间。
输入12小时的时间:晚上9:11
相当于24小时制:21:11
答案 7 :(得分:-1)
@aussie_aj; 1.在上午1点进入你的课程时间,你将在早上8点而不是晚上9点45分离开! 2.下午12:10在你的节目中看到魔术!! :)
#include <stdio.h>
int main()
{
int h,m,t,temp1,temp2,
a1,a2,d1,d2; //a1,a1 & d1,d2 are ariival & dep. time res.
//Departure times
// dt1 = 480, dt2 = 583, dt3 = 679, dt = 767,
// dt5 = 840, dt6 = 945, dt = 1140, dt = 1305;
//Arrival times
// at1 = 616, at2 = 712, at3 = 811, at4 = 900,
// at5 = 968, at6 = 1075, at7 = 1280, at8 = 1438;
printf("Enter a 24-hour time(hh:mm): ");
scanf("%d:%d",&h,&m);
t = h*60 + m;
printf("Closest departure time is ");
//CONDITIONS FOR CLOSEST DEPARTURE TIME.
if(t <= 480)
{
temp1 = 480 - t;
temp2 = t - 0;
d1 = 480; d2 = 1305;
a1 = 616; a2 = 1438;
}
else if(t >=480 && t <= 583)
{
temp1 = 583 - t;
temp2 = t - 480;
d1 = 583; d2 = 480;
a1 = 712; a2 = 616;
}
else if(t <= 679)
{
temp1 = 679 - t;
temp2 = t - 582;
d1 = 679; d2 = 582;
a1 = 811; a2 = 712;
}
else if(t <= 767)
{
temp1 = 767 - t;
temp2 = t - 679;
d1 = 767; d2 = 679;
a1 = 900; a2 = 811;
}
else if(t <= 840)
{
temp1 = 840 - t;
temp2 = t - 767;
d1 = 840; d2 = 767;
a1 = 968; a2 = 900;
}
else if(t <= 945)
{
temp1 = 945 - t;
temp2 = t - 840;
d1 = 945; d2 = 840;
a1 = 1075; a2 = 968;
}
else if(t <= 1140)
{
temp1 = 1140 - t;
temp2 = t - 945;
d1 = 1140; d2 = 945;
a1 = 1280; a2 = 1075;
}
else if(t <= 1305 || t >= 1305)
{
temp1 = 1305 - t;
temp2 = t - 1140;
d1 = 1305; d2 = 1140;
a1 = 1438; a2 = 1280;
}
// COMPUTING CLOSEST DEPARTURE TIME.
if(temp1 < temp2)
{
h = d1/60;
m = d1%60;
}
else
{
h = d2/60;
m = d2%60;
}
//PRINTING CLOSEST DEPARTURE TIME.
if(h < 12)
printf("%.2d:%.2d a.m.",h,m);
else
{
if(h == 12)
printf("%.2d:%.2d p.m.",h,m);
else
printf("%.2d:%.2d p.m.",h - 12,m);
}
//COMPUTING CORRESPONDING ARRIVAL TIME.
printf(", arriving at ");
if(temp1 < temp2)
{
h = a1/60;
m = a1%60;
}
else
{
h = a2/60;
m = a2%60;
}
//PRINTING CORRESPONDING ARRIVAL TIME.
if(h < 12)
printf("%.2d:%.2d a.m.",h,m);
else
{
if(h == 12)
printf("%.2d:%.2d p.m.",h,m);
else
printf("%.2d:%.2d p.m.",h - 12,m);
}
getch();
}