#include <iostream> using namespace std; int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; char a[9], b[9]; int main() { while (scanf("%s%s", a, b) != EOF) { // 注意 while 处理多个 case int year1, year2; int month1, month2; int day1, day2; sscanf(a,"%4d%2d%2d",&year1,&month1,&day1);//格式化读入 sscanf(b,"%4d%2d%2d",&year2,&month2,&day2); //把year1变成小的日期 if (year1 < year2 || year1 == year2 && month1 < month2 || year1 == year2 && month1 == month2 && day1 < day2) { //不用交换的情况 } else { int t = year1; year1 = year2; year2 = t; t = month1; month1 = month2; month2 = t; t = day1; day1 = day2; day2 = t; } int count = 0; while (year1 != year2 || month1 != month2 || day1 != day2) { day1++; count++; if (day1 > days[month1]) { month1++; day1 = 1; } if (month1 > 12) { year1++; month1 = 1; } } printf("%d\n", count + 1); } } // 64 位输出请用 printf("%lld")
采用格式化的读入节省了很多代码