本題要求編寫程序計算某年某月某日是該年中的第幾天。
輸入格式:
輸入在一行中按照格式“yyyy/mm/dd”(即“年/月/日”)給出日期。注意:閏年的判別條件是該年年份能被4整除但不能被100整除、或者能被400整除。閏年的2月有29天。
輸出格式:
在一行輸出日期是該年中的第幾天。
輸入樣例1:
2009/03/02
輸出樣例1:
61
輸入樣例2:
2000/03/02
輸出樣例2:
62
#include <stdio.h> int main(void) { int tab[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 }; int tab_leap[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 }; int year, month, day, days = 0, isleap; scanf("%d/%d/%d", &year, &month, &day); if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) { isleap = 1; } else { isleap = 0; } if (isleap) { for (int i = 0; i < month - 1; i++) { days += tab_leap[i]; } } else { for (int i = 0; i < month - 1; i++) { days += tab[i]; } } days += day; printf("%d\n", days); return 0; }