【解题思路】
因为要输入不超过10^18的正整数,而unsigned long long的最大值为18446744073709551615 (20位),所以用unsigned long long保存要输入的数n。把时分秒分开计算,已知一天共有24*60*60*1000=86400000ms,n/86400000计算出过了多少天,用小数部分计算出当天的小时。分、秒也是用类似的方法。
#include<iostream>
using namespace std;
#define H(x) x/86400000.0
#define M(x) x*24.0
#define T(x) x*60.0
int main()
{
unsigned long long n;
cin >> n;
double x = H(n);
while (x >= 1) x -= 1;
int h = 24.0 * x;
if (h > 9) cout << h << ":";
else cout << "0" << h << ":";
double y = M(x) - h;
int m =y*60.0;
if (m <= 9)cout << "0" << m << ":";
else cout << m << ":";
double z = T(y) - m;
int t =z*60.01;
if (t<= 9)cout << "0" << t<< endl;
else cout << t << endl;
}
ps:我的代码还是有一点问题,输入1582924327000时,计算秒如果用正常的t=z*60.0,保留的整数是6,而正确答案是7,为了过系统检测我用的z*60.01。希望有大佬告诉我更好的方法。