1 /*
2 * Main.c
3 * B12-分支-12. 计算火车运行时间
4 * Created on: 2014年6月4日
5 * Author: Boomkeeper
6 *
7 ********测试未通过*******
8 */
9 #include <stdio.h>
10 #include <stdlib.h>
11
12 int startTime,arrTime;
13 int *p_startTime=&startTime;
14 int *p_arrTime=&arrTime;
15
16 /*将输入时间全部转化成分钟,再做时间差,算出运行的分钟数*/
17 int changeMinute(int *time)
18 {
19 //printf("\nnow changMinute() starting...\n");
20 int hour,minute,newtime;
21
22 hour=*time/100;
23 minute=*time%100;
24 newtime=hour*60+minute;
25 //printf("now changeMinute() is closing...\n");
26 return newtime;
27 }
28
29 /*将运行时间(分钟数)再转成hh:mm形式*/
30 void changeHour(int *runTime)
31 {
32 //printf("\nnow changeHour() is starting...\n");
33 int runTimeHour,runTimeMinute;
34
35 runTimeHour=*runTime/60;
36 //printf("runTimeHour = %i\n",runTimeHour);
37 runTimeMinute=*runTime-runTimeHour*60;
38 //printf("runTimeMinute = %i\n",runTimeMinute);
39
40 printf("%02i:%02i\n",runTimeHour,runTimeMinute);
41 //printf("now changeHour() is closing...\n");
42 }
43
44 /*如果没有跨过半夜12点*/
45 void inOneDay()
46 {
47 //printf("\nnow inOneDay() is starting ...\n");
48 int startTimeMinute=changeMinute(p_startTime);
49 //printf("startTimeMinute = %i\n",startTimeMinute);
50 int arrTimeMinute=changeMinute(p_arrTime);
51 //printf("arrTimeMinute = %i\n",arrTimeMinute);
52 int runtime=arrTimeMinute-startTimeMinute;
53 //printf("\nruntime in minute = %i\n",runtime);
54 int *runTime=&runtime;
55
56 changeHour(runTime);
57 //printf("now inOneDay() is closing ...\n");
58
59 }
60
61 /*如果跨过半夜12点*/
62 void inTwoDay()
63 {
64 //printf("\nnow inTwoDay() is starting ...\n");
65 int startTimeMinute=changeMinute(p_startTime);
66 //printf("startTimeMinute = %i\n",startTimeMinute);
67 int arrTimeMinute=changeMinute(p_arrTime);
68 //printf("arrTimeMinute = %i\n",arrTimeMinute);
69 int runtime=24*60-(startTimeMinute-arrTimeMinute);
70 //printf("\nruntime in minute = %i\n",runtime);
71 int *runTime=&runtime;
72
73 changeHour(runTime);
74 //printf("now inTwoDay() is closing ...\n");
75 }
76
77 /*判断输入数据的合法性*/
78 void judge(int *p_startTime,int *p_arrTime)
79 {
80 //printf("now judge() is starting...\n");
81 if(*p_startTime<0 || *p_startTime>2359)
82 exit(0);
83 if(*p_arrTime<0 || *p_arrTime>2359)
84 exit(0);
85 if(*p_startTime == *p_arrTime)
86 {
87 printf("24:00\n");//这里到底算不算在同一天内?不管怎么改,测试结果仍一样
88 exit(0);
89 }
90 if(*p_startTime > *p_arrTime)
91 inTwoDay();
92 else
93 inOneDay();
94 //printf("now judge() is closing...\n");
95 }
96
97 int main()
98 {
99 scanf("%4i %4i",p_startTime,p_arrTime);
100 //printf("get startTime=%i,arrTime=%i\n\n",*p_startTime,*p_arrTime);
101
102 judge(p_startTime,p_arrTime);
103
104 return 0;
105 }
![](//images0.cnblogs.com/i/638935/201406/162035212545124.png)