代码:

#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. Dataguard单机—>单机

    本演示案例所用环境: primary Standby OS Hostname CHINA-DB1 CHINA-DB2 OS Version SUSE Linux Enterprise Server 1 ...

  2. 文本编辑器EditPlus的安装

  3. 16,div+css的布局较table布局有什么优点?

    改版的时候更加方便,只要改css文件 页面加载速度更快,结构化清晰,页面显示简洁 表现与结构相分离 易于搜索引擎优化,排名更靠前

  4. lucky 的 时光助理(3)

    今天lucky小姐哭笑不得的说, 昨天下班时跟经理一起走的时候, 地铁站手机被小偷偷走,那时一个人孤单单的,除了惊愕, 她不知道该去联系谁, 借了同事的手机,给家里打去电话. 她说,因为那是她唯一记得 ...

  5. MTSQL主主同步方案

    ** MySQL主主+Keepalived **MySQL+DRBD+Heartbeat 在企业中,数据库高可用一直是企业的重中之重,中小企业很多都是使用mysql主主方案,一主多从,读写分离等,但是 ...

  6. Kosaraju's algorithm

    推荐到我的这篇博客中看完整版的. 该算法用于求解有向图的强连通分量,也就是强连通子图的个数. 算法实现摘自Kosaraju's algorithm - 百度百科: #include <iostr ...

  7. Django项目配置数据库时,已安装mysqlclient,却提示 Did you install mysqlclient错误,后右报错ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3

    错误信息如下: 解决方案是: 找到自己的项目文件夹下的__init__.py  添加如下代码 解决这个问题后,右报错django2.2/mysql ImproperlyConfigured: mysq ...

  8. 批量导出存储在msdb库的SSIS包

    http://blog.51cto.com/ultrasql/1924464 use msdb go IF OBJECT_ID('msdb.dbo.usp_ExportSSISPkgs') IS NO ...

  9. Centos7 安装virtualenv bash: virtualenv: command not found...的解决

    安装好了python3的环境前提下 1.使用pip3安装virtualenv pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple virt ...

  10. 基础_04_list and tuple

    一.list(列表) list是Python里的一种容器,里面可以存储多个任何类型的数据,长度也可以任意伸缩,可以像C语言中数组那样,按照索引下标获取对应的值.但数组是一个存储多个固定类型变量的连续内 ...