第几天?
思路:构建一个闰年和非闰年的二维数组模型:int a[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 } };,判断是闰年还是非闰年,计算天数
代码:
#include<iostream> using namespace std; int main() { int year, month, day; int sum = 0; while (scanf("%d/%d/%d", &year, &month, &day) != EOF) { sum = 0; int a[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 } }; int i = 0; if (year % 4 == 0 && year % 100 || year % 400 == 0) { i = 1; } sum += day; for (int j = 1; j < month; j++) { sum += a[i][j]; } printf("%d ", sum); } system("pause"); return 0; }