#include <iostream> using namespace std; //每个月多少天 const int daysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //入职年月日 const int YEAR = 2014; const int MONTH = 8; const int DAY = 18; //函数功能:是否为闰年 inline bool isLeapYear(int year) { return (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0)); } //函数功能:某一年有多少天 inline int allDaysOfYear(int year) { return isLeapYear(year) ? 366 : 365; } //函数功能:[year1, year2)年间一共有多少天,仅考虑年 int daysBetweenYears(int year1, int year2) { int sum = 0; for (int i = year1; i < year2; i++) { sum += allDaysOfYear(i); } return sum; } //函数功能:给出日期,计算出该年的第多少天。 int daysInYear(int year, int month, int day) { int result = day; for (int i = 0; i < month - 1; i++) { if (i == 1 && isLeapYear(year)) result += 1; result += daysInMonth[i]; } return result; } //函数功能:从入职起多少天 int daysFromEntry(int year, int month, int day) { if (year < YEAR) return 0; int daysOfYear = daysInYear(year, month, day); int daysEntryYear = daysInYear(YEAR, MONTH, DAY); if (daysOfYear < daysEntryYear && year == YEAR) return 0; int result = 0; return daysOfYear - daysEntryYear + daysBetweenYears(YEAR, year) + 1; } //函数功能:日期输入是否合法 bool isCorrectDate(int year, int month, int day) { if (month <= 0 || month > 12 || day <= 0) return false; if (isLeapYear(year) && month == 2 && day <= daysInMonth[1] + 1) return true; if (day <= daysInMonth[month - 1]) return true; return false; } int main() { int year, month, day; year = 2015; for (month = 1; month <= 12; month++) { for (day = 1; day <= 31; day++) { if (!isCorrectDate(year, month, day)) cout << "日期错误" << endl; else cout << month << "-" << day << " : " << daysFromEntry(year, month, day) << endl; } system("pause"); } return 0; }
#include<stdio.h>
#include<iostream>
using namespace std;
int main() {
int year, month, day;
//wd入职天数;nd入职日期数值化
int wd = 0, nd = 0, ipd = 0;
int md[12] = { 31,28,31,30,31,30,31,31,30,31,10,31 };
int years[1000];
cin >> year >> month >> day;
//中间年份闰年问题解决
for (int i = 0; i < year - 2014; i++) {
years[i] = 2015 + i;
if (years[i] % 4 == 0 && years[i] % 100 != 0 || years[i] % 400 == 0) {
md[1] = 29;
ipd += 1;
}
else
{
md[1] = 28;
}
}
//将2014年1月1日看做原点,然后日期数值化 nd = 18;
for (int i = 1; i < 8; i++) {
nd += md[i];
}
//输入的日期数值化
ipd += (year - 2014) * 365 + day;
for (int i = 1; i < month; i++) {
ipd += md[i];
}
wd = ipd - nd;
cout << wd;
return 0;
}
这题居然不给个能测试通过率的题型,这会让我觉得我100%行#include <iostream> using namespace std; const int months[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int YEAR = 2014; const int MONTH = 8; const int DAY = 18; //是否为闰年 int isLeapYear(int year){ return (year % 4 == 0 && year % 100) || (year & 400 == 0); } //某年天数。 int daysOfYear(int year){ return isLeapYear(year) ? 366 : 365; } //某年经过的天数。 int daysOfPass(int year, int month, int day){ int res = 0; for(int i = 0; i < month - 1; i++) res += months[i]; res += isLeapYear(year) ? day + 1 : day; return res; } //距离明年的天数。 int daysToNextYear(){ return daysOfYear(YEAR) - daysOfPass(YEAR, MONTH, DAY) + 1; } //总天数。 int daysOfWork(int year, int month, int day){ if(YEAR == year){ return daysOfPass(year, month, day) - daysOfPass(YEAR, MONTH, DAY) + 1; } int res = daysToNextYear(); for(int i = YEAR + 1; i < year; i++) res += daysOfYear(i); res += daysOfPass(year, month, day); return res; } int main(){ cout << daysOfWork(2014, 8, 31) << endl; cout << daysOfWork(2016, 3, 5) << endl; }