2023年8月12日修改自适应年一月一日周一值
int 返回月的天数(int 年, int 月)
{//缘由https://bbs.youkuaiyun.com/topics/395074486
return (月 == 2 ?
(((!(年 % 4) && 年 % 100) || !(年 % 400)) ? 29 : 28) :
(((月 <= 7 && 月 % 2) || (月>7 && !(月 % 2))) ? 31 : 30));
if (月 == 2){ if ((!(年 % 4) && 年 % 100) || !(年 % 400)) return 29; else return 28; }
else if ((月 <= 7 && 月 % 2) || (月 > 7 && !(月 % 2)))return 31; else return 30;
}
int 返回年总天数(int 年)
{//返回指定年到12月的值。
return (年 ? 365 * 年 + ((年 / 4) - (年 / 100)) : 0);
}
//int 返回年总天数(int 年, int 标)
//{//返回指定年到12月,可减去标志年如1900年必须减去1年因为1900年1月1日是周一因此1900年必须计算在内。
// return ((年 -= (标 ? --标 : 标)) ? 365 * 年 + ((年 / 4) - (年 / 100)) : 0);
//}
int 自适年一月一日周一值(int 年)
{//自适应跳过400倍年之前,返回1月1日是周1的值。
return ((年 > 400 ? --年 -= 年 / 400 * 400 : --年) ? 365 * 年 + ((年 / 4) - (年 / 100) ) : 0);
}
int 返回年总天数(int 年, bool k = false)
{//返回指定年到12月,自适应调整400倍数最接近年份的总天数差,如输入2020则输出从2001-2020整年总天数。
return ((年 -= (k ? 年 / 400 * 400 - 1: 0)) ? 365 * 年 + ((年 / 4) - (年 / 100)) : 0);
}
int 返回年差天数(int 年1, int 年2)
{//返回指定年到12月,自适应调整标记年的值,返回400倍数最接近年份的总天数差。
int 自适应年 = (年1 > 年2 ? 年2 / 400 * 400 : 年1 / 400 * 400);
年1 -= 自适应年; 年2 -= 自适应年;
年1 = 365 * 年1 + ((年1 / 4) - (年1 / 100));
年2 = 365 * 年2 + ((年2 / 4) - (年2 / 100));
return ((年1 > 年2) ? 年1 - 年2 : 年2 - 年1);
}
cout << 返回年差天数(2021, 2020) << endl << 返回年差天数(2019, 2020);
int a = 24;
cout << "该年天数" << (返回年总天数(a) - 返回年总天数(a - 1))
<< "\t总天数" << 返回年总天数(a-1)
<<"\t周几"<< (返回年总天数(a-1)+1) % 7 << endl;
int n = a, y = 1, r = 1, nn = n;
while (n)
{
while (nn)if (--y)r += 返回月的天数(nn, y); else --nn, y = 13;
cout << n << "总天数" << r << "年1月1日是星期" << r % 7 << endl;//if (r % 7 == 1)
nn = --n, y = 1, r = 1;
}