您当前的位置: 首页 >  蓝桥杯

不牌不改

暂无认证

  • 0浏览

    0关注

    422博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

蓝桥杯2021年第十二届真题第一场-时间显示

不牌不改 发布时间:2022-03-07 22:23:00 ,浏览量:0

题目

题目链接

题解

就是考查取模运算???

我是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             
关注
打赏
1662186765
查看更多评论
0.0417s