我一开始也是被“分钟小于0,输出不满4位且小时是0”这块不对所困扰,之后我也不断尝试数据,发现没有问题,之后再次读题,发下疑点。
原题目部分要求如下:
输出格式:
输出不多于四位数字表示的终止时间,当小时为个位数时,没有前导的零。题目保证起始时间和终止时间在同一天内。
重点在于黄色加粗的部分,题目隐藏的条件当小时为0时,小时的两位前导0可以省略,但0小时本身不能省,比如当最终输出为0小时59分,输出格式就要为059,而不能是59,否则就会出现“分钟小于0,输出不满4位且小时是0”这模块的分得不到。
解决方案如下:
#include <stdio.h>
int main()
{
int start, through;
int end(int start, int through);
scanf("%d %d", &start, &through);
printf("%03d",end(start, through)); //重点在这的printf()的内容
}
int end(int start, int through)
{
int start1, start2, end1, end2, end3;
start1=start/100 ;start2=start%100;
start=start1*60+start2;
end3=start+through;
end1=end3/60 ;end2=end3%60;
end3=end1*100+end2;
return end3;
}
我的代码printf("%03d",end(start, through));把%d改为%03d,这样当输出位数不满三位时,用零补齐。
补充:
%03d用在printf或scanf函数中的格式符,用来以特定的格式输入和输出字符。
% 是格式符的开始。
d 表示有符号整数。
3 表示最小输出3位数字。
0 表示如果数字不足3位,在左边用0补成3位。
如 printf("%03d", 25); 会打印出 025。
本文针对编程竞赛中特定输出格式的题目进行了解析,并通过示例代码详细解释了如何使用%03d格式化输出来解决不满4位数且小时为0的问题。
1851





