int isLeapYear(int year) { if(((year % 4 == 0) && (year % 100 != 0)) || year % 400 ==0) return 1; else return 0; } int sincebefore(int year,int month,int day) { int days = 0; for(int i = 1; i< year; ++i) { days += isLeapYear(i) ? 366 : 365; } int monthDays[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; for(int i = 1; i< month; ++i) { if(i == 2 && isLeapYear(year)) { days += 29; } else { days += monthDays[i]; } } days += day; return days; } int main() { int year1,month1,day1; int year2,month2,day2; scanf("%4d%2d%2d",&year1,&month1,&day1); //这里的格式 scanf("%4d%2d%2d",&year2,&month2,&day2); //这里的格式必须是这样,因为年占4个位置,月占2,天也是2 int days1 = sincebefore(year1,month1,day1); int days2 = sincebefore(year2,month2,day2); int day = days1 > days2 ? days1 - days2 +1 : days2 -days1+1; printf("%d\n", day) ; return 0; }
#include <stdio.h> #include <stdbool.h> void nextDay(int *year, int *month, int *day) { bool isLeap = (*year % 400 == 0) || (*year % 4 == 0 && *year % 100 != 0); int dayOfMonth[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; if (isLeap) dayOfMonth[2] = 29; (*day)++; if (*day > dayOfMonth[*month]) { *day = 1; (*month)++; } if (*month > 12) { *month = 1; (*year)++; } } int main(void) { int year1, month1, day1; int year2, month2, day2; // printf("请输入两个年、月、日,输出两个日期的差值\n"); scanf("%4d%2d%2d", &year1, &month1, &day1); scanf("%4d%2d%2d", &year2, &month2, &day2); int count = 1; while(!(year1==year2 && month1==month2 && day1==day2)) { nextDay(&year1, &month1, &day1); count++; } printf("%d\n", count); return 0; }
#include <stdio.h> #include <stdbool.h> int daytab[2][13] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, }; bool IsLeapYear(int year) { return (year % 100 != 0 && year % 4 == 0) || (year % 400 == 0); } int NumberOfYear(int year) { if (IsLeapYear(year)) { return 366; } else { return 365; } } int NumberOfDate(int year, int month, int day) { int row = IsLeapYear(year); int number = 0; for (int i = 0; i < month; i++) { number += daytab[row][i]; } number += day; return number; } int main() { int year1, month1, day1; int year2, month2, day2; int number; while (scanf("%04d%02d%02d\n%04d%02d%02d", &year1, &month1, &day1, &year2,&month2,&day2) != EOF) { if (year1 != year2) { number = NumberOfYear(year1) - NumberOfDate(year1, month1, day1); number += NumberOfDate(year2, month2, day2); int a = year2 - year1; for (int i = 1; i < a; i++) { number += NumberOfYear(year1 + a); } } else { number = NumberOfDate(year2, month2, day2) - NumberOfDate(year1, month1, day1); } printf("%d\n", number + 1); } return 0; }
#include <stdio.h> int daytab[2][13]={ {0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31} }; _Bool IsLeapYear(int year){ return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0); } int NumberOfYear(int year){ if(IsLeapYear(year)){ return 366; }else { return 365; } } int main() { int year1,month1,day1; int year2,month2,day2; int n1,n2; while(scanf("%d%d",&n1,&n2)!=EOF){ if(n1<n2){ int temp=n1; n1=n2; n2=temp; } year1=n1/10000; month1=n1/100%100; day1=n1%100; year2=n2/10000; month2=n2/100%100; day2=n2%100; int sum1=0,sum2=0; int row; row=IsLeapYear(year1); for(int i=0;i<month1;i++){ sum1+=daytab[row][i]; } sum1=day1+sum1; row=IsLeapYear(year2); for(int i=0;i<month2;i++){ sum2+=daytab[row][i]; } sum2+=day2; int count=0; count=sum1-sum2+1; while(year1>year2){ count+=NumberOfYear(year1); year1--; } printf("%d\n",count); } return 0; }