【题目】
德国数学家克里斯蒂安·蔡勒(Julius Christian Johannes Zeller, 1822- 1899)在1886年推导出蔡勒公式(Zeller’s Formula),这是一种计算任何一日属一星期中哪一日的算法。
具体公式为:
W = [C/4] – 2C + y + [y/4] + [13 * (M+1) / 5] + d – 1
公式都是基于公历的置闰规则来考虑。
公式中的符号含义如下:
W:星期
c:世纪数减一(年份前两位数)
y:年(后两位数)
m:月(m的取值范围为3至14,即在蔡勒公式中,某年的1、2月要看作上一年的13、14月来计算,比如2003年1月1日要看作2002年的13月1日来计算)
d:日
[ ]:称作高斯符号,代表取整,即只要整数部份。
mod:同余(这里代表括号里的答案除以7后的余数
这个公式由世纪数减一、年份末两位、月份和日数即可算出W,再除以7,得到的余数是几就表示这一天是星期几,唯一需要变通的是要把1月和2月当成上一年的13月和14月, C和y都按上一年的年份取值。
【样例】
对2022年4月26日这一天来说,c=20,y=22,m=4,d=26,代入公式中计算可得
W = [20/4] – 40 + 22 + [22/4] + [13 * (4+1) / 5] + 26 – 1
w=5-40+22+5+13+26-1
w=30,再除以7,余2,说明这一天是星期二。
C++代码
//#ifndef coutAIO_H
#include <iostream>//数据类型前面加mutable可以避免常函数中。
#include <windows.h>//Ctrl+K+C一键注释,Ctrl+K+U一键去释 /* ——— —— —— —— —— —— */
#include <conio.h>
#include <stack>
#include <algorithm>
#include <cmath>
#include <vector>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>
#define MAX 1000//最大
using namespace std;
long long sz[10000][10000];//数组,用于分解整数
long long jc(long long a) {//阶乘(!a)
long long k = 1;
for (long long i = 0; i < a; i++)
{
k *= i;
}
return 1;
}
long long zsws(long long a) {//求整数位数
short b = 0;
while (a) {
a /= 10;
b++;
}
return b;
}
void fj(long long a) {//分解整数各位
long long b = zsws(a), i;
while (b--) {
sz[0][b] = a % 10;
a /= 10;
}
}
void xycfb(long long x = 9, long long y = 9) {//把xy乘法表存在sz里。
for (long long i = 0; i < x; i++) {
for (long long j = 0; j < y; j++) {
sz[i][j] = (i + 1) * (j + 1);
}
}
}
void scsz(long long x = 9, long long y = 9) {//输出数组
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
cout << sz[i][j] << "\t";
}
cout << endl;
}
}
void sccfb(long long a = 9, long long b = 9) {//输出ab乘法表
xycfb(a, b);
scsz(a, b);
}
long long fbnq(long long a) {//斐波那契数列求和
if (a > 2)
return fbnq(a - 1) + fbnq(a - 2);
return 1;
}
void printsz(char* arr[]) {//打印数组
long long i = 0;
for (; *arr != NULL; arr++) {
cout << i << " " << *arr << endl;//第几个被输出与
i++;
}
}
//以上新添
void gotoxy(int y, int x)
{
COORD coord;
coord.X = x;
coord.Y = y;
HANDLE a = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(a, coord);
}
bool isprime(long long a) {
long long l = 0;
if (a == 2) {
return true;
}
else if (a % 2 == 0) {
return false;
}
else {
for (size_t m = 3; m < a; m += 2)
{
if (a % m == 0) {
return false;
}
}
return a;
}
}
void color(int a)//打印颜色
{
/*亮白*/ if (a == 0) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
/*蓝色*/ if (a == 1) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN | FOREGROUND_BLUE);
/*绿色*/ if (a == 2) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_GREEN);
/*紫色*/ if (a == 3) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_BLUE);
/*红色*/ if (a == 4) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED);
/*黄色*/ if (a == 5) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
/*深蓝色*/ if (a == 6) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_INTENSITY | FOREGROUND_BLUE);
/*土黄色or金黄色*/ if (a == 7) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN);
/*灰色接近白*/ if (a == 8) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
void bukaqingping() {
gotoxy(0, 0);/*
for (int i = 0; i < 50; i++) {
cout << " " << endl;
}*/
system("cls");
gotoxy(0, 0);
}
template<typename T>
void swap(T & a, T & b) {
T temp = a;
a = b;
b = temp;
}
long long numberaio(string n) {
for (int i = 0; i < 100000; i++) {
if (n[i] == '\0')
{
return i;
}
}
}
void coutAIO(string b, int truefalse0 = 0, bool truefalse1 = 0) {//说的话,速度,是否回车。
int i = 0, s = 0;
long long a = numberaio(b);
while (a != 0) {
color(2);
/*
s = rand() % 9;
if (a != 0 && s == 0)
{
color(0);
}
if (a != 0 && s == 1)
{
color(1);
}
if (a != 0 && s == 2)
{
color(2);
}
if (a != 0 && s == 3)
{
color(3);
}
if (a != 0 && s == 4)
{
color(4);
}
if (a != 0 && s == 5)
{
color(5);
}
if (a != 0 && s == 6)
{
color(6);
}
if (a != 0 && s == 7)
{
color(7);
}
if (a != 0 && s == 8)
{
color(8);
}*/
cout << b[i];
a--;
i++;
if (truefalse0 == 1) {
Sleep(10);
}
if (truefalse0 == 2) {
Sleep(20);
}
if (truefalse0 == 3) {
Sleep(40);
}
if (truefalse0 == 4) {
Sleep(100);
}
if (truefalse0 == 5) {
Sleep(300);
}
if (truefalse0 == 6) {
Sleep(1000);
}
}
if (truefalse1 == 0) {
cout << endl;
}
}
constexpr size_t tmp_FNV_offset_basis = 2166136261U;
constexpr size_t tmp_FNV_prime = 16777619U;
size_t hash(string Hash) {//求哈希值
string str = Hash;
size_t val = tmp_FNV_offset_basis;
for (int i = 0; i < str.length(); ++i) {
val ^= static_cast<size_t>(str[i]);
val *= tmp_FNV_prime;
}
return val;
}
void showMenu() {//显示面板
coutAIO("************************", 0);
coutAIO("***** 1.添加联系人 *****", 0);
coutAIO("***** 2.显示联系人 *****", 0);
coutAIO("***** 3.删除联系人 *****", 0);
coutAIO("***** 4.查找联系人 *****", 0);
coutAIO("***** 5.修改联系人 *****", 0);
coutAIO("***** 6.清空通讯录 *****", 0);
coutAIO("***** 0.退出通讯录 *****", 0);
coutAIO("************************", 0);
}//仅供参考
//联系人结构体
struct people {
string m_Name;//姓名
int m_Sex;//性别
int m_Age;//年龄
string m_Phone;//电话
string m_Addr;//住址
}
;
//通讯录结构体
struct Addressbook {
Addressbook() {
m_Size = 0;
ifstream ifs;
ifs.open("MyFriend.txt", ios::in);
char ch;
ifs >> ch;
if (ifs.eof()) {
ifs.close();
return;
}
string name;
int sex;
int age;
string phone;
string addr;
int num = 0;
//空格是分割内容的,便于分别读取数据
//流提取符 >> 会跳过输入流中的空格、tab、换行符等空白字符
while (ifs >> name && ifs >> sex && ifs >> age && ifs >> phone && ifs >> addr) {
peopleArray[num].m_Name = name;
if (!num) {
peopleArray[num].m_Name = ch + name;
}
peopleArray[num].m_Sex = sex;
peopleArray[num].m_Age = age;
peopleArray[num].m_Phone = phone;
peopleArray[num].m_Addr = addr;
num++;
}
m_Size = num;
ifs.close();
}
int m_Size;//人数个数
vector<people> peopleArray;//联系人数组
};
void save(Addressbook * abs) {
ofstream ofs;
ofs.open("MyFriend.txt", ios::out);
for (size_t i = 0; i < abs->m_Size; i++)
{
ofs << abs->peopleArray[i].m_Name << " "
<< abs->peopleArray[i].m_Sex << " "
<< abs->peopleArray[i].m_Age << " "
<< abs->peopleArray[i].m_Phone << " "
<< abs->peopleArray[i].m_Addr << endl;
}
ofs.close();
}
class Cube {
friend void operator <<(ostream& cout, Cube& p);
long double m_L; // 长 length
long double m_W; // 宽 width
long double m_H; // 高 height
public:
Cube(long long a = 0, long long b = 0, long long c = 0) {//分别写出长宽高
this->m_L = a;
this->m_W = b;
this->m_H = c;
}
Cube operator+(Cube& p) {
Cube temp;
temp.m_L = this->m_L + p.m_L;
temp.m_W = this->m_W + p.m_W;
temp.m_H = this->m_H + p.m_H;
return temp;
}
Cube operator-(Cube& p) {
Cube temp;
temp.m_L = this->m_L - p.m_L;
temp.m_W = this->m_W - p.m_W;
temp.m_H = this->m_H - p.m_H;
return temp;
}
Cube operator*(Cube& p) {
Cube temp;
temp.m_L = this->m_L * p.m_L;
temp.m_W = this->m_W * p.m_W;
temp.m_H = this->m_H * p.m_H;
return temp;
}
Cube operator/(Cube& p) {
Cube temp;
temp.m_L = this->m_L / p.m_L;
temp.m_W = this->m_W / p.m_W;
temp.m_H = this->m_H / p.m_H;
return temp;
}
void set_H(long long h) { m_H = h; }
void set_L(long long l) { m_L = l; }
void set_W(long long w) { m_W = w; }
long long get_H() { return m_H; }
long long get_L() { return m_L; }
long long get_W() { return m_W; }
long long area() { // 表面积
return 2 * (m_L * m_W + m_L * m_H + m_W * m_H);
}
bool CompareSurfaceArea(Cube & a, Cube & b) { // 比较表面积
return a.area() > b.area();
}
long long volume() { // 体积
return m_L * m_W* m_H;
}
bool hasSameVolume(Cube & b) { // 比较体积是否相等
return volume() == b.volume();
}
};
//cubage是容积,Compare是比较,
//dia/meter是直径,radius是半径,
//volume是体积,circumference是周长
//Superficial area或surface area是表面积。
//C:组合数 Combination
//A:排列数 Arrangement
//!:阶乘 Factorial
void operator <<(ostream& cout, Cube& p) {
coutAIO("长为 ", 1, 1);
cout << p.m_L << "\t\t";
coutAIO(" ,宽为 ", 1, 1);
cout << p.m_W << "\t\t";
coutAIO(" ,高为 ", 1, 1);
cout << p.m_H;
coutAIO(" 。", 6);
}
void full_screen()
{
HWND hwnd = GetForegroundWindow();
int cx = GetSystemMetrics(SM_CXSCREEN); // 屏幕宽度 像素
int cy = GetSystemMetrics(SM_CYSCREEN); // 屏幕高度 像素
LONG l_WinStyle = GetWindowLong(hwnd, GWL_STYLE); // 获取窗口信息
// 设置窗口信息 最大化 取消标题栏及边框 //
//SetWindowLong(hwnd,GWL_STYLE,( l_WinStyle |WS_MAXIMIZE| WS_POPUP) & ~WS_CAPTION & ~WS_THICKFRAME & ~WS_BORDER);//
SetWindowPos(hwnd, HWND_TOP, 0, 0, cx, cy, 0);
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int* test01() {
static int a = 100;
return &a;
}
string Hex1, Hex2, strans;
int DecArr[MAX] = { 0 };
void Reverse(char* s) {//新轮子,翻转字符串
int n = numberaio(s);
for (int i = 0, j = n - 1; i < j; i++, j--) {
char c = s[i];
s[i] = s[j];
s[j] = c;
}
}
long long HexToDec(string aHex)//转化十六进制
{
long Dec = 0;
int temp = 0;
int count = 0;
int DecCount = 0;
int strlen = aHex.length();
int StrLen = strlen;
while (strlen--)
{
if (aHex[count] < '0' || (aHex[count] > '9' && aHex[count] < 'A') || (aHex[count] > 'F' && aHex[count] < 'a') || aHex[count]>'f')
{
cout << "输入的不是十六进制格式" << endl;
return 0;
}
count++;
}
count = StrLen - 1;
while (StrLen--)
{
if (aHex[count] >= '0' && aHex[count] <= '9')
{
DecArr[DecCount++] = aHex[count] - '0';
}
else if (aHex[count] >= 'A' && aHex[count] <= 'F')
{
DecArr[DecCount++] = aHex[count] - 'A' + 10;
}
else
{
DecArr[DecCount++] = aHex[count] - 'a' + 10;
}
count--;
}
int j = 0;
for (int i = 0; i < DecCount; i++)
{
if (i < 1)
{
Dec = Dec + DecArr[i];
}
else
{
Dec = (Dec + (DecArr[i] * (16 << j)));
j += 4;
}
}
return Dec;
}
void decToHex(long long n)
{
char hexNum[MAX] = "", strtemp[MAX] = "";
int i = 0;
while (n != 0)
{
int temp = 0;
temp = n % 16;
if (temp < 10)
{
hexNum[i] = temp + 48;
i++;
}
else
{
hexNum[i] = temp + 87;
i++;
}
n /= 16;
}
Reverse(hexNum);
strans.assign(hexNum);
}
//void Time() {
// time_t curtime;
// time(&curtime);
// cout << "1970到目前经过秒数:" << time(&curtime) << endl;
// cout << "本地日期和时间:" << ctime(&curtime) << endl;
// tm* nowtime = localtime(&curtime);
// // 输出tm结构的年月日
// cout << "年: " << 1900 + nowtime->tm_year << endl;
// cout << "月: " << 1 + nowtime->tm_mon << endl;
// cout << "日: " << nowtime->tm_mday << endl;
// cout << "时间: " << nowtime->tm_hour << ":";
// cout << nowtime->tm_min << ":";
// cout << nowtime->tm_sec << endl;
//}
int main()
{
srand((unsigned)time(NULL));
system("title 通讯录管理系统");
/*FILE* stream;
freopen_s(&stream,"源.in","r",stdin);
freopen_s(&stream,"源.out", "w", stdout);*/
long long W, c, m, y, d;
char Y[MAX],y1[MAX],y2[MAX];
while (true) {
cout << "请输入年份:" << endl;
cin >> y;
cout << "请输入月份:" << endl;
cin >> m;
cout << "请输入日期:" << endl;
cin >> d;
y1[0] = Y[0];
y1[1] = Y[1];
c = atoi(y1);
y2[0] = Y[2];
y2[1] = Y[3];
y = atoi(y2);
W = c / 4 - 2 * c + y + y / 4 + 13 * (m + 1) / 5 + d - 1;
bukaqingping();
cout << "这天周" << W % 7 << endl;
system("pause");
bukaqingping();
}
return 0;
}