目前使用的格里高利历闰年的规则如下:
- 公元年分非4的倍数,为平年。
- 公元年分为4的倍数但非100的倍数,为闰年。
- 公元年分为100的倍数但非400的倍数,为平年。
- 公元年分为400的倍数为闰年。
请用一个表达式 (不能添加括号) 判断某一年是否为闰年。
bool isLeapYear(int year) {return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
}
输入某一天的年月日,输出下一天的年月日。
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>int DaysOfMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool isLeapYear(int year);int main(void) {int year, month, day;printf("Please enter year, month and day: ");scanf("%d%d%d", &year, &month, &day);day++;if (isLeapYear(year)) {DaysOfMonth[2]++;}if (day > DaysOfMonth[month]) {day = 1;month++;}if (month > 12) {month = 1;year++;}printf("Next day is: %d/%d/%d\n", year, month, day);return 0;
}bool isLeapYear(int year) {return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
}
输入某两天的年月日,输出这两天的相距多少天。
int distance(int year1, int month1, int day1, int year2, int month2, int day2) {int days = 0;// 计算year1年份的天数days += DaysOfMonth[month1] - day1;if (isLeapYear(year1) && month1 == 2) {days++;}for (int i = month1 + 1; i <= 12; i++) {days += DaysOfMonth[i];}if (isLeapYear(year1) && month1 == 1) {days++;}// 计算中间的年份的天数for (int i = year1 + 1; i < year2; i++) {days += 365;if (isLeapYear(i)) {days++;}}// 计算year2年份的天数for (int i = 1; i < month2; i++) {days += DaysOfMonth[i];}if (isLeapYear(year2) && month2 > 2) {days++;}days += day2;// 如果 year1 == year2, 则多算了一整年的天数。if (year1 == year2) {days -= 365;if (isLeapYear(year1)) {days--;}}return days;
}
(d) 已知1970年1月1日是星期四,输入之后的某一天的年月日,判断它是星期几?
#include <stdio.h>int is_leap_year(int year) {return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
}int get_day_of_week(int year, int month, int day) {int days_per_month[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };int days = 0;for (int y = 1970; y < year; y++) {days += is_leap_year(y) ? 366 : 365;}for (int m = 1; m < month; m++) {days += days_per_month[m];if (m == 2 && is_leap_year(year)) {days++; // 闰年的2月多加一天}}days += day - 1; // 减去1是因为我们从1970年1月1日开始计算的return (days + 4) % 7; // 1970年1月1日是星期四,所以加上4再对7取模
}int main() {int year, month, day;printf("请输入年月日(格式:yyyy mm dd):");scanf("%d %d %d", &year, &month, &day);int day_of_week = get_day_of_week(year, month, day);switch (day_of_week) {case 0: printf("星期四\n"); break;case 1: printf("星期五\n"); break;case 2: printf("星期六\n"); break;case 3: printf("星期日\n"); break;case 4: printf("星期一\n"); break;case 5: printf("星期二\n"); break;case 6: printf("星期三\n"); break;}return 0;
}
输入1970年之后任意一年的年份,输出该年的年历。对话如下:
输入:
Please input the year whose calendear you want to know?
2004
输出:
|=====================The Calendar of Year 2004====================|
: 1 SUN MON TUE WED THU FRI SAT 7 SUN MON TUE WED THU FRI SAT :
: 1 2 3 1 2 3 :
: 4 5 6 7 8 9 10 4 5 6 7 8 9 10 :
: 11 12 13 14 15 16 17 11 12 13 14 15 16 17 :
: 18 19 20 21 22 23 24 18 19 20 21 22 23 24 :
: 25 26 27 28 29 30 31 25 26 27 28 29 30 31 :
: 2 SUN MON TUE WED THU FRI SAT 8 SUN MON TUE WED THU FRI SAT :
: 1 2 3 4 5 6 7 1 2 3 4 5 6 7 :
: 8 9 10 11 12 13 14 8 9 10 11 12 13 14 :
: 15 16 17 18 19 20 21 15 16 17 18 19 20 21 :
: 22 23 24 25 26 27 28 22 23 24 25 26 27 28 :
: 29 29 30 31 :
: 3 SUN MON TUE WED THU FRI SAT 9 SUN MON TUE WED THU FRI SAT :
: 1 2 3 4 5 6 1 2 3 4 :
: 7 8 9 10 11 12 13 5 6 7 8 9 10 11 :
: 14 15 16 17 18 19 20 12 13 14 15 16 17 18 :
: 21 22 23 24 25 26 27 19 20 21 22 23 24 25 :
: 28 29 30 31 26 27 28 29 30 :
: 4 SUN MON TUE WED THU FRI SAT 10 SUN MON TUE WED THU FRI SAT :
: 1 2 3 1 2 :
: 4 5 6 7 8 9 10 3 4 5 6 7 8 9 :
: 11 12 13 14 15 16 17 10 11 12 13 14 15 16 :
: 18 19 20 21 22 23 24 17 18 19 20 21 22 23 :
: 25 26 27 28 29 30 24 25 26 27 28 29 30 :
: 31 :
: 5 SUN MON TUE WED THU FRI SAT 11 SUN MON TUE WED THU FRI SAT :
: 1 1 2 3 4 5 6 :
: 2 3 4 5 6 7 8 7 8 9 10 11 12 13 :
: 9 10 11 12 13 14 15 14 15 16 17 18 19 20 :
: 16 17 18 19 20 21 22 21 22 23 24 25 26 27 :
: 23 24 25 26 27 28 29 28 29 30 :
: 30 31 :
: 6 SUN MON TUE WED THU FRI SAT 12 SUN MON TUE WED THU FRI SAT :
: 1 2 3 4 5 1 2 3 4 :
: 6 7 8 9 10 11 12 5 6 7 8 9 10 11 :
: 13 14 15 16 17 18 19 12 13 14 15 16 17 18 :
: 20 21 22 23 24 25 26 19 20 21 22 23 24 25 :
: 27 28 29 30 26 27 28 29 30 31 :
|==================================================================|
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdbool.h>int DaysOfMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const char* DayOfWeek[] = { "SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT" };bool isLeapYear(int year);
int distance(int year1, int month1, int day1, int year2, int month2, int day2);
int weekday(int year, int month, int day);
void printCalendar(int year);int main(void) {int year;printf("Please input the year whose calendear you want to know?\n");scanf("%d", &year);printCalendar(year);return 0;
}bool isLeapYear(int year) {return year % 400 == 0 || year % 4 == 0 && year % 100 != 0;
}int weekday(int year, int month, int day) {int days = distance(1970, 1, 1, year, month, day);return (4 + days) % 7;
}int distance(int year1, int month1, int day1, int year2, int month2, int day2) {int days = 0;// 计算year1年份的天数days += DaysOfMonth[month1] - day1;if (isLeapYear(year1) && month1 == 2) {days++;}for (int i = month1 + 1; i <= 12; i++) {days += DaysOfMonth[i];}if (isLeapYear(year1) && month1 == 1) {days++;}// 计算中间的年份的天数for (int i = year1 + 1; i < year2; i++) {days += 365;if (isLeapYear(i)) {days++;}}// 计算year2年份的天数for (int i = 1; i < month2; i++) {days += DaysOfMonth[i];}if (isLeapYear(year2) && month2 > 2) {days++;}days += day2;// 如果 year1 == year2, 则多算了一整年的天数。if (year1 == year2) {days -= 365;if (isLeapYear(year1)) {days--;}}return days;
}void printCalendar(int year) {if (isLeapYear(year)) {DaysOfMonth[2]++;}printf("|=====================The Calendar of Year %d====================|\n", year);for (int i = 1; i <= 6; i++) {printf(": %2d SUN MON TUE WED THU FRI SAT %2d SUN MON TUE WED THU FRI SAT :\n", i, i + 6);// 打印每个月的第一行printf(": ");int wd1 = weekday(year, i, 1);int day1 = 1;for (int j = 0; j < wd1; j++) {printf(" ");}while (wd1 != 0 || day1 == 1) {printf("%4d", day1++);wd1 = (wd1 + 1) % 7;}printf(" ");int wd2 = weekday(year, i + 6, 1);int day2 = 1;for (int j = 0; j < wd2; j++) {printf(" ");}while (wd2 != 0 || day2 == 1) {printf("%4d", day2++);wd2 = (wd2 + 1) % 7;}printf(" :\n");// 打印每个月的剩余行while (day1 <= DaysOfMonth[i] || day2 <= DaysOfMonth[i + 6]) {printf(": ");int d1 = DaysOfMonth[i] - day1 + 1;if (d1 <= 0) {printf(" ");} else if (d1 < 7) {for (int k = 1; k <= d1; k++) {printf("%4d", day1++);}for (int k = 1; k <= 7 - d1; k++) {printf(" ");}} else {for (int k = 1; k <= 7; k++) {printf("%4d", day1++);}}printf(" ");int d2 = DaysOfMonth[i + 6] - day2 + 1;if (d2 <= 0) {printf(" ");} else if (d2 < 7) {for (int k = 1; k <= d2; k++) {printf("%4d", day2++);}for (int k = 1; k <= 7 - d2; k++) {printf(" ");}} else {for (int k = 1; k <= 7; k++) {printf("%4d", day2++);}}printf(" :\n");}}
}