代码:

#include <iostream>
#include <string>
#include <vector> using namespace std; int hashMapping(int mouth)
{
vector<int> days{31,28,31,30,31,30,31,31,30,31,30,31};
return days[mouth - 1];
} int IsLeapYear(int year, int mouth)
{
if(((year%4 == 0 && year%100 != 0) || (year%400 == 0)) && mouth == 2)
{
return hashMapping(mouth) + 1;
}
return hashMapping(mouth);
} int CalculateDays(int start_year, int end_year, int start_mouth, int end_mouth)
{
int days = 0;
if (end_mouth >= start_mouth)
{
if (start_year < end_year)
{
for (int i = start_year; i < end_year; i++)
{
for (int j = start_mouth; j <= 12; j++)
days += IsLeapYear(i, j);
for (int j = 1; j <= end_mouth; j++)
days += IsLeapYear(i, j);
}
}
else
{
for (int j = start_mouth; j <= end_mouth; j++)
days += IsLeapYear(start_year, j);
}
}
else
{
for (int i = start_year; i < end_year; i++)
{
for (int j = start_mouth; j <= 12; j++)
days += IsLeapYear(i, j);
for (int j = 1; j <= end_mouth; j++)
days += IsLeapYear(i, j);
}
} return days;
} string hashFunc(int days, int week)
{
vector<string> str{"星期一","星期二","星期三","星期四","星期五","星期六","星期天"};
return str[(week - 1 + days%7)%7];
} void PrintWeek(int start_year, int start_mouth, int start_day, int week, int end_year, int end_mouth, int end_day)
{
string we;
int days;
switch(week)
{
case 1:
days = CalculateDays(start_year, end_year, start_mouth, end_mouth) - (start_day + IsLeapYear(end_year, end_mouth) - end_day);
we = hashFunc(days, week);
break;
case 2:
days = CalculateDays(start_year, end_year, start_mouth, end_mouth) - (start_day + IsLeapYear(end_year, end_mouth) - end_day);
we = hashFunc(days, week);
break;
case 3:
days = CalculateDays(start_year, end_year, start_mouth, end_mouth) - (start_day + IsLeapYear(end_year, end_mouth) - end_day);
we = hashFunc(days, week);
break;
case 4:
days = CalculateDays(start_year, end_year, start_mouth, end_mouth) - (start_day + IsLeapYear(end_year, end_mouth) - end_day);
we = hashFunc(days, week);
break;
case 5:
days = CalculateDays(start_year, end_year, start_mouth, end_mouth) - (start_day + IsLeapYear(end_year, end_mouth) - end_day);
we = hashFunc(days, week);
break;
case 6:
days = CalculateDays(start_year, end_year, start_mouth, end_mouth) - (start_day + IsLeapYear(end_year, end_mouth) - end_day);
we = hashFunc(days, week);
break;
case 7:
days = CalculateDays(start_year, end_year, start_mouth, end_mouth) - (start_day + IsLeapYear(end_year, end_mouth) - end_day);
we = hashFunc(days, week);
break;
}
cout << start_year << '/' << start_mouth << '/' << start_day <<
'~' << end_year << '/' << end_mouth << '/' << end_day << " 共有:"
<< days << "天 " << endl;
cout << end_year << "年" << end_mouth << "月" << end_day << "日是" << we << endl;
} int main()
{
int start_year, start_mouth, start_day;
int end_year, end_mouth, end_day;
int week; cout << "请输入起始年/月/日 以及 星期几:" << endl;
cin >> start_year;
cin >> start_mouth;
cin >> start_day;
cin >> week; cout << "请输入结束年/月/日:" << endl;
cin >> end_year;
cin >> end_mouth;
cin >> end_day; PrintWeek(start_year, start_mouth, start_day, week, end_year, end_mouth, end_day); return 0;
}

  运行结果:

  第二个版本:直接输入xx年xx月xx日 就可以计算日期为星期几:

#include <iostream>
#include <string>
#include <vector> using namespace std; //以 1890/1/1 星期三 为基准 int hashMapping(int mouth)
{
vector<int> days{31,28,31,30,31,30,31,31,30,31,30,31};
return days[mouth - 1];
} int IsLeapYear(int year, int mouth)
{
if(((year%4 == 0 && year%100 != 0) || (year%400 == 0)) && mouth == 2)
{
return hashMapping(mouth) + 1;
}
return hashMapping(mouth);
} int CalculateDays(int year, int mouth)
{
int days = 0; if (1890 < year)
{
for (int i = 1890; i < year; i++)
{
for (int j = 1; j <= 12; j++)
days += IsLeapYear(i, j);
}
for (int j = 1; j <= mouth; j++)
days += IsLeapYear(year, j);
}
else if(1890 > year)
{
for (int j = mouth; j <= 12; j++)
days += IsLeapYear(year, j);
for (int i = year + 1; i < 1890; i++)
{
for (int j = 1; j <= 12; j++)
days += IsLeapYear(i, j);
}
}
else
{
for (int j = 1; j <= mouth; j++)
days += IsLeapYear(1890, j);
} return days;
} string hashFunc(int days)
{
vector<string> str{"星期一","星期二","星期三","星期四","星期五","星期六","星期天"};
return str[(2 + days%7)%7]; // 因为是以星期三为基准 所以是下标2
} void PrintWeek(int year, int mouth, int day)
{
if ((year > 1889) && (mouth <= 12 && mouth >= 1) && (day <= 31 && day >= 1))
{
string we;
int days = CalculateDays(year, mouth) - (1 + IsLeapYear(year, mouth) - day);
if (1890 <= year)
we = hashFunc(days); cout << year << "年" << mouth << "月" << day << "日是 " << we << endl;
}
else
{
cout << "输入有误!" << endl;
exit(1);
}
} int main()
{
int year, mouth, day; cout << "请输入年/月/日:" << endl;
cin >> year;
cin >> mouth;
cin >> day; PrintWeek(year, mouth, day); return 0;
}

  运行结果:

  特殊的:

  ps:1890/1/1 星期三 我是度娘知道的,所以也可以通过度娘检验我的程序结果的正确性。

  程序不能计算1890年以前的。因为不知道为什么目前算法对往回算不正确。原本我是以2018/1/1 星期一为基准的 但就是发现输入过去的日期算法就不正确后,就尽量往以前的年份改,发现度娘的最早也就能算1890/1/1的,但足够用了,不过如果能将年份尽量处于中间段,往前后算的计算次数就会减少很多,虽然算法时间复杂度还是O(n^2)。

  给个度娘截图:

计算xx年xx月xx日是星期几的更多相关文章

  1. 请使用switch语句和if...else语句,计算2008年8月8日这一天,是该年中的第几天。

    请使用switch语句和if...else语句,计算2008年8月8日这一天,是该年中的第几天. #include <stdio.h> int main() { /* 定义需要计算的日期 ...

  2. 通过Calendar简单解析Date日期,获取年、月、日、星期的数值

    有时候项目中需要用到Date的年.月.日.星期的数值.那么解析方法如下: /**解析日期,获取年月日星期*/ private void parseDateToYearMonthDayWeek(Date ...

  3. java8中计算两个日期时间LocalDateTime的时间差,格式化成xx年yy月zz日aa时bb分cc秒

    原则上应该适用Period来计算,因为他是专门为这种需求设计的.当时他只能计算到两个时间差的,年月日 传入参数Period.between(LocalDate,LocalDate) 这里是计算两个Lo ...

  4. C# 根据年、月、周、星期获得日期等

    原文:C# 根据年.月.周.星期获得日期等 /// 取得某月的第一天 /// </summary> /// <param name="datetime">要 ...

  5. JS对象 编程练习 某班的成绩出来了,现在老师要把班级的成绩打印出来。 效果图: XXXX年XX月X日 星期X--班级总分为:81

    编程练习 某班的成绩出来了,现在老师要把班级的成绩打印出来. 效果图: XXXX年XX月X日 星期X--班级总分为:81 格式要求: 1.显示打印的日期. 格式为类似"XXXX年XX月XX日 ...

  6. 我的第一篇博客之js的XXXX年XX月XX日 星期[日一-六] [上下]午 XX时:XX分

    <!DOCTYPE html> <html>     <head> <title>test</title>                 ...

  7. 【BIRT】在页面上展示xxxx年xx月xx日

    我们在做报表开发的时候经常会遇到一个问题,就是需要在报表上展示”xxxx年xx月xx日”这种日期,例如:需要在报表展示日期如下图: 我们现在数据库存储的日期是:20171231 那么我们如何转化为 这 ...

  8. 求当前时间100天后的时间日期,格式化为xxxx年xx月xx日

    package com.demo1; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Da ...

  9. sql中把时间转换成xx年xx月xx日

    DECLARE @dt datetime SET @dt=GETDATE()--1.短日期格式:yyyy-m-d SELECT STUFF(STUFF(CONVERT(char(8),@dt,112) ...

随机推荐

  1. Django跨域:

    下包 pip install django-cors-headers 下面的操作在setting里 添加到appps里 INSTALLED_APPS = [ ... 'corsheaders', .. ...

  2. SQL常用语句和函数

    从一个表中选取数据插入到另一个表中: select column_name(s) into new_table_name from old_table_name --new_table_name表不必 ...

  3. php相关问题学习(以备面试)

    1.原味地址:[ http://www.yiichina.com/tutorial/57 ] 注:本文转自 http://www.icultivator.com/p/5535.html 整理了一份PH ...

  4. javascript中offsetWidth、clientWidth、width、scrollWidth、clientX、screenX、offsetX、pageX

    原文:https://www.cnblogs.com/ifworld/p/7605954.html 元素宽高 offsetWidth //返回元素的宽度(包括元素宽度.内边距和边框,不包括外边距) o ...

  5. 使用docker踩过的坑

    最近需要使用docker,但是win10电脑的系统不是docker windows适用版本,没法在windows上安装 于是就上centos虚拟机里面装了一个docker docker pull文件的 ...

  6. java中怎么表现一对多

    链接:https://www.cnblogs.com/w-xibao/p/8183680.html 链接2:https://blog.csdn.net/C_time/article/details/8 ...

  7. 7、源与值(Source/Values)

    学习目录:树莓派学习之路-GPIO Zero 官网地址:https://gpiozero.readthedocs.io/en/stable/source_values.html 环境:UbuntuMe ...

  8. provide 和 inject高阶使用

    provide 在祖先里授权导出 inject在后代负责接收 foo可以是本组件的函数方法 或者 变量foo 也可以是祖先组件自己 祖先组件foo: this 后代组件 foo.$options.da ...

  9. 微软停止支持Windows 7 数百万台电脑将面临病毒等风险

    导读 微软给出的公告称,从2020年开始停止支持Windows 7操作系统,这意味着该公司不会再向数百万台电脑发布任何软件更新,包括可以防止网络攻击的软件补丁. 微软给出的公告称,从2020年开始停止 ...

  10. 组件向外暴露v-model绑定的参数

    <template> <div class="search-box"> <i class="icon-search">< ...