题目
题目链接
题解
就是考查取模运算???
我是FW,居然用了牛刀,我是FW啊。(而且我居然以为一秒等于一百毫秒)
因为时、分、秒、毫秒之间的换算关系是不随着年月日的不同而变化的,所以直接整除就可以了。
可以理解为时分之间为60进制进位关系,分秒之间为60进制进位关系,秒毫秒之间为1000进制进位关系,类比计算100的百分位、十分位和个位,就是利用整除和取模进行计算。
代码
我的代码
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL second2ms = 1000;
const LL minute2ms = 60 * second2ms;
const LL hour2ms = 60 * minute2ms;
const LL day2ms = 24 * hour2ms;
LL n;
LL getallyear_days (int year) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) return 366;
return 365;
}
LL getallyear_ms (int year) {
LL days = getallyear_days (year);
return days * day2ms;
}
LL getmonth_ms (int year, int month) {
if (month != 2) {
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
return 31 * day2ms;
else
return 30 * day2ms;
} else {
if (getallyear_days (year) == 366)
return 29 * day2ms;
else
return 28 * day2ms;
}
}
int main()
{
cin >> n;
int year = 1970, month = 1, day = 1;
int hour = 0, minute = 0, second = 0;
while (n >= getallyear_ms(year)) {
n -= getallyear_ms(year);
year ++;
}
while (n >= getmonth_ms (year, month)) {
n -= getmonth_ms (year, month);
month ++;
}
while (n >= day2ms) {
n -= day2ms;
day ++;
}
while (n >= hour2ms) {
n -= hour2ms;
hour ++;
}
while (n >= minute2ms) {
n -= minute2ms;
minute ++;
}
while (n >= second2ms) {
n -= second2ms;
second ++;
}
printf ("%02lld:%02lld:%02lld\n", hour, minute, second);
return 0;
}
简单代码
#include <iostream>
using namespace std;
typedef long long LL;
int main()
{
LL n;
cin >> n;
int hour = n / (60 * 60 * 1000) % 24;
int minute = n / (60 * 1000) % 60;
int second = n / 1000 % 60;
printf ("%02d:%02d:%02d\n", hour, minute, second);
return 0;
}
|