每日一题,练题第一天
题目:
首先在做本题时,有两个难点,四舍五入与格式输出。
四舍五入有两个方法:
一是应用头文件math.h的函数round(),确实#include<math.h>PTA会报错。
二是自己手写int n = ((b - a) + 50) / 100。
第二个就是格式的输入,因为没察觉到需要两位数输出时分秒,所以一直有输出错误。查看他人代码发现是格式输出错误,所以应用c的输出printf来进行格式输出(printf("%02d:%02d:%02d", x, y, z))。
下面是完整的代码:
#include
#include<math.h>
using namespace std;
int main() {
int a, b, c,x, y, z;
cin >> a >> b;
c = round((b-a)/100.0);
x = c / 3600;
c = c - (x * 3600);
y = c / 60;
z = c - y * 60;
printf("%02d:%02d:%02d", x, y, z);
return 0;
}