2008上交:Day of Week
- 题目描述:
-
We now use the Gregorian style of dating in Russia. The leap years are years with number divisible by 4 but not divisible by 100, or divisible by 400.
For example, years 2004, 2180 and 2400 are leap. Years 2004, 2181 and 2300 are not leap.
Your task is to write a program which will compute the day of week corresponding to a given date in the nearest past or in the future using today’s agreement about dating.
- 输入:
-
There is one single line contains the day number d, month name M and year number y(1000≤y≤3000). The month name is the corresponding English name starting from the capital letter.
- 输出:
-
Output a single line with the English name of the day of week corresponding to the date, starting from the capital letter. All other letters must be in lower case.
- 样例输入:
-
9 October 2001 14 October 2001
- 样例输出:
-
Tuesday Sunday
思路:先2016年7月3日(正好为星期日)为基准。求两日期之间的差值时可分别求两年份与1000年1月1日之间的距离,再做差。#include <iostream> #include <string.h> using namespace std; char Month[13][20]={"Null","January","February","March","April","May","June","July","August","September","October","November","December"}; char Week[8][20]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; int mds[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int d,y,m; char mon[20]; int sMon(char s[]) { for(int i=1;i<=12;i++) { if(strcmp(Month[i],s)==0) { return i; } } } bool isLeap(int year) { if((year%4==0&&year%100!=0)||year%400==0) { return true; } return false; } int caldays(int year) { int sum=0; for(int i=1000;i<=year;i++) { if(isLeap(i)) { sum+=366; } else sum+=365; } return sum; } int standard; int main() { standard+=caldays(2015); for(int i=1;i<=6;i++) { standard+=mds[i]; } if(isLeap(2016)) standard+=1; standard+=3; while(cin>>d>>mon>>y) { m=sMon(mon); int days=0; days+=caldays(y-1);//本年不算 for(int i=1;i<m;i++)//本月不算 { days+=mds[i]; } if(m>2&&isLeap(y)) days+=1; days+=d; int diff=days-standard; if(diff>=0) { diff%=7; cout<<Week[diff]<<endl; } else { diff=-diff; diff%=7; if(diff!=0) diff=7-diff; cout<<Week[diff]<<endl; } } return 0; }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 对象命名为何需要避免'-er'和'-or'后缀
· SQL Server如何跟踪自动统计信息更新?
· AI与.NET技术实操系列:使用Catalyst进行自然语言处理
· 分享一个我遇到过的“量子力学”级别的BUG。
· Linux系列:如何调试 malloc 的底层源码
· 对象命名为何需要避免'-er'和'-or'后缀
· JDK 24 发布,新特性解读!
· C# 中比较实用的关键字,基础高频面试题!
· .NET 10 Preview 2 增强了 Blazor 和.NET MAUI
· SQL Server如何跟踪自动统计信息更新?