问题描述
给定一个年份y和一个整数d,问这一年的第d天是几月几日?
注意闰年的2月有29天。满足下面条件之一的是闰年:
1) 年份是4的整数倍,而且不是100的整数倍;
2) 年份是400的整数倍。
输入格式
输入的第一行包含一个整数y,表示年份,年份在1900到2015之间(包含1900和2015)。
输入的第二行包含一个整数d,d在1至365之间。
输出格式
输出两行,每行一个整数,分别表示答案的月份和日期。
样例输入
2015
80
样例输出
3
21
样例输入
2000
40
样例输出
2
9
ccf 日期问题 求答案求答案
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- 霓为衣兮风为裳 2015-09-13 09:36关注
#pragma once class CDate { public: CDate(); CDate(unsigned year, unsigned month, unsigned day) :m_nYear(year), m_nMonth(month), m_nDay(day){} virtual ~CDate(); public: friend int DaysMinus(const CDate& smallDate, const CDate& bigDate); private: unsigned int m_nYear; unsigned int m_nMonth; unsigned int m_nDay; }; #include "stdafx.h" #include "CDate.h" CDate::CDate() { m_nYear = 0; m_nMonth = 0; m_nDay = 0; } CDate::~CDate() { } int DaysMinus(const CDate& smallDate, const CDate& bigDate) { if (smallDate.m_nYear > bigDate.m_nYear) cout << "smallDate is bigger than bigDate !"<<endl; else if(smallDate.m_nYear==bigDate.m_nYear){ if (smallDate.m_nMonth>bigDate.m_nMonth) cout << "smallDate is bigger than bigDate !"<<endl; else if (smallDate.m_nMonth==bigDate.m_nYear&&smallDate.m_nDay>bigDate.m_nDay) cout << "smallDate is bigger than bigDate !" << endl; } int days = 0;//相差的天数 for (int i = smallDate.m_nYear + 1; i < bigDate.m_nYear; i++){ days += 365; if ((i % 4 == 0 && i % 100 != 0) || i % 400 == 0)//闰年的条件 days++; } int iSmdays = 0,iBidays = 0;//第一年和最后一年的时间 bool bSm_Run, bBi_Run; bSm_Run = ((smallDate.m_nYear % 4 == 0 && smallDate.m_nYear % 100 != 0) || smallDate.m_nYear % 400 == 0);//小年是闰年 switch (smallDate.m_nMonth) { case 1:iSmdays = smallDate.m_nDay; break; case 2:iSmdays = 31 + smallDate.m_nDay; break; case 3:iSmdays = 31 + 28 + smallDate.m_nDay + bSm_Run; break; case 4:iSmdays = 31 * 2 + 28 + smallDate.m_nDay + bSm_Run; break; case 5:iSmdays = 31 * 2 + 28 + 30 + smallDate.m_nDay + bSm_Run; break; case 6:iSmdays = 31 * 3 + 28 + 30 + smallDate.m_nDay + bSm_Run; break; case 7:iSmdays = 31 * 3 + 28 + 30 * 2 + smallDate.m_nDay + bSm_Run; break; case 8:iSmdays = 31 * 4 + 28 + 30 * 2 + smallDate.m_nDay + bSm_Run; break; case 9:iSmdays = 31 * 5 + 28 + 30 * 2 + smallDate.m_nDay + bSm_Run; break; case 10:iSmdays = 31 * 5 + 28 + 30 * 3 + smallDate.m_nDay + bSm_Run; break; case 11:iSmdays = 31 * 6 + 28 + 30 * 3 + smallDate.m_nDay + bSm_Run; break; case 12:iSmdays = 31 * 6 + 28 + 30 * 4 + smallDate.m_nDay + bSm_Run; break; default: break; } //bSm_Run = ((smallDate.m_nYear % 4 == 0 && smallDate.m_nYear % 100 != 0) || smallDate.m_nYear % 400 == 0);//小年是闰年 bBi_Run = ((bigDate.m_nYear % 4 == 0 && bigDate.m_nYear % 100 != 0) || bigDate.m_nYear % 400 == 0); switch (bigDate.m_nMonth){ case 1:iBidays = bigDate.m_nDay; break; case 2:iBidays = 31 + bigDate.m_nDay; break; case 3:iBidays = 31 + 28 + bigDate.m_nDay + bBi_Run; break; case 4:iBidays = 31 * 2 + 28 + bigDate.m_nDay + bBi_Run; break; case 5:iBidays = 31 * 2 + 28 + 30 + bigDate.m_nDay + bBi_Run; break; case 6:iBidays = 31 * 3 + 28 + 30 + bigDate.m_nDay + bBi_Run; break; case 7:iBidays = 31 * 3 + 28 + 30 * 2 + bigDate.m_nDay + bBi_Run; break; case 8:iBidays = 31 * 4 + 28 + 30 * 2 + bigDate.m_nDay + bBi_Run; break; case 9:iBidays = 31 * 5 + 28 + 30 * 2 + bigDate.m_nDay + bBi_Run; break; case 10:iBidays = 31 * 5 + 28 + 30 * 3 + bigDate.m_nDay + bBi_Run; break; case 11:iBidays = 31 * 6 + 28 + 30 * 3 + bigDate.m_nDay + bBi_Run; break; case 12:iBidays = 31 * 6 + 28 + 30 * 4 + bigDate.m_nDay + bBi_Run; break; default: break; } if (smallDate.m_nYear == bigDate.m_nYear){ days = iBidays - iSmdays; } else{ days = days + 365 + bSm_Run - iSmdays; days = days + iBidays; } return days; } // DATE.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include "CDate.h" int _tmain(int argc, TCHAR* argv[]) { CDate SmallDate(2007,3,12); CDate bigDate(2013,6,16); cout << DaysMinus(SmallDate, bigDate) << endl; system ("pause"); return 0; }
这是原来的一个课堂作业,可能不能满足你的要求,大致就是这么个算法。 如果有用请点击下采纳。
解决 1无用