【C/C++ 09】万年历

一、题目

输入一个年份,以日历的格式打印这一年的所有天数,需要正确的表示每一天是周几。

二、算法

以公元1年1月1日作为万年历的起始日期,公元1年1月1日是周一,所以算法的核心就是就算某一天距离起始日期的天数差,然后根据天数差取模就能得到周几。

拿到输入的年份后,循环打印每个月的日历表格,每个月都计算出这个月第一条距离万年历其实日期的天数差,便能得到当月第一天是周几,然后根据当月的总天数,便能打印出当月的日历表。

三、代码

cpp 复制代码
#define _CRT_SECURE_NO_WARNINGS 1

// 起始日期:公元1年1月1日,周一

#include <iostream>
#include <vector>
using namespace std;

vector<int> g_monthDays = { 31, 28, 31, 30, 31, 30,
						    31, 31, 30, 31, 30, 31 };

bool LeapYear(int year)
{
	if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
		return true;
	return false;
}

int GetMonthDays(int year, int month)
{
	if (LeapYear(year) && month == 2)
		return 29;
	return g_monthDays[month - 1];
}

int GetYearDays(int year)
{
	if (LeapYear(year))
		return 366;
	return 365;
}

int GetDays(int year, int month, int day)
{
	int sum = 0;
	for (int i = 1; i < year; ++i)
	{
		sum += GetYearDays(i);
	}
	for (int i = 1; i < month; ++i)
	{
		sum += GetMonthDays(year, i);
	}
	sum += day;
	return sum;
}

void PrintMonth(int year, int month)
{
	printf("|-------------- %d 年 %02d 月 -------------|\n", year, month);
	cout << "|------------------------------------------|" << endl;
	cout << "| 周日 " << " 周一 " << " 周二 " << " 周三 " << " 周四 " << " 周五 " << " 周六 " << "|" << endl;

	int monthDays = GetMonthDays(year, month);
	int firstDays = GetDays(year, month, 1);

	int weekday = firstDays % 7;
	cout << "|";
	for (int i = 0; i < weekday; ++i)
		cout << "      ";
	for (int i = 1; i <= monthDays; ++i)
	{
		printf("  %02d  ", i);
		weekday = (weekday + 1) % 7;
		if (weekday == 0)
			cout << "|" << endl << "|";
	}
	if (weekday != 0)
	{
		for (int i = 0; i < 7 - weekday; ++i)
			cout << "      ";
		cout << "|" << endl << "|";
	}
	cout << "------------------------------------------|" << endl;
	cout << endl << endl;
}

int main()
{
	cout << "请输入年份:";
	int year;
	cin >> year;

	for (int i = 1; i <= 12; ++i)
	{
		PrintMonth(year, i);
	}

	return 0;
}

四、测试

共12个月,截取部分月份。

相关推荐
MobotStone5 分钟前
我的 AI 代码清理方法论:从原型到生产,只需 5 步
算法·程序员·架构
沐苏瑶7 小时前
Java 搜索型数据结构全解:二叉搜索树、Map/Set 体系与哈希表
java·数据结构·算法
ZoeJoy88 小时前
算法筑基(二):搜索算法——从线性查找到图搜索,精准定位数据
算法·哈希算法·图搜索算法
Alicx.8 小时前
dfs由易到难
算法·蓝桥杯·宽度优先
桦08 小时前
【C++复习】:继承
开发语言·c++
_日拱一卒8 小时前
LeetCode:找到字符串中的所有字母异位词
算法·leetcode
鱼难终9 小时前
类和对象(下)
c++
云泽8089 小时前
深入 AVL 树:原理剖析、旋转算法与性能评估
数据结构·c++·算法
Wilber的技术分享10 小时前
【LeetCode高频手撕题 2】面试中常见的手撕算法题(小红书)
笔记·算法·leetcode·面试
邪神与厨二病10 小时前
Problem L. ZZUPC
c++·数学·算法·前缀和