一道C语言编程题求助:计算时间差V1.0

计算时间差V1.0(4分)
题目内容:
编程从键盘任意输入两个时间(例如4时55分和1时25分),计算并输出这两个时间之间的间隔。要求不输出时间差的负号。
程序的运行结果示例1:
Input time one(hour, second):4,55↙
Input time two(hour, second):1,25↙
3 hour 30 second
程序的运行结果示例2:
Input time one(hour, second):1,56↙
Input time two(hour, second):3,25↙
1 hour 29 second

输入提示信息:"Input time one(hour, second):"
"Input time two(hour, second):"
输入格式:"%d,%d"
输出格式:"%d hour %d second\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
别用百度的,我试过,下面这段没分:
#include <stdio.h>
#include <math.h>
int main()
{ int h1,s1,h2,s2,sum1,sum2,sum3,h3,s3;;
printf("Input time one(hour,second):");
scanf("%d,%d",&h1,&s1);
printf("Input time two(hour,second):");
scanf("%d,%d",&h2,&s2);
sum1=h1*60+s1;
sum2=h2*60+s2;
sum3=abs(sum1-sum2);
h3=sum3/60;
s3=sum3-h3*60;
printf("%d hour %d second\n",h3,s3);
}

#include <stdio.h>
int main()

int h1,s1,h2,s2,h3,s3;;
printf("Input time one(hour,second):");
scanf("%d,%d",&h1,&s1);
printf("Input time two(hour,second):");
scanf("%d,%d",&h2,&s2);
h3=h1-h2;
s3=s1-s2;
if(s3<0&&h3==0)s3=-s3;
else if(h3<0)h3=-h3, s3=-s3;

if(s3<0) h3-=1, s3+=60;
printf("%d hour %d second\n",h3,s3);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-11-09
#include <stdio.h>
void main(){
int h1,h2,m1,m2,s;
printf("Input time one(hour,second):");
scanf("%d,%d",&h1,&m1);
printf("Input time two(hour,second):");
scanf("%d,%d",&h2,&m2);
s=h1*60+m1-h2*60-m2;
if(s<0) s=-s;
printf("%d hour %d second",s/60,s%60);
}

注意:有的编程软件要求主函数返回值,如果有此要求,void改为int,最后添加语句return 0;

第2个回答  2017-11-09
等会下班帮你写一下~先留个位置

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网