题目
题目链接
题解就是考查取模运算???
我是FW,居然用了牛刀,我是FW啊。(而且我居然以为一秒等于一百毫秒)
因为时、分、秒、毫秒之间的换算关系是不随着年月日的不同而变化的,所以直接整除就可以了。
可以理解为时分之间为60进制进位关系,分秒之间为60进制进位关系,秒毫秒之间为1000进制进位关系,类比计算100的百分位、十分位和个位,就是利用整除和取模进行计算。
代码我的代码
#include
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) // run
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 ++;
}
// 确定了秒
// cout
关注
打赏
最近更新
- 深拷贝和浅拷贝的区别(重点)
- 【Vue】走进Vue框架世界
- 【云服务器】项目部署—搭建网站—vue电商后台管理系统
- 【React介绍】 一文带你深入React
- 【React】React组件实例的三大属性之state,props,refs(你学废了吗)
- 【脚手架VueCLI】从零开始,创建一个VUE项目
- 【React】深入理解React组件生命周期----图文详解(含代码)
- 【React】DOM的Diffing算法是什么?以及DOM中key的作用----经典面试题
- 【React】1_使用React脚手架创建项目步骤--------详解(含项目结构说明)
- 【React】2_如何使用react脚手架写一个简单的页面?