一年中的第n天是几月几号?

主要思想是用表来表示数据结构,从而简化代码。

废话不多说,直接上代码:

 #include #define IS_LEAP(X) (((X) % 400 ==0 || (X) % 100 != 0 && (X) % 4 == 0) ? 1 : 0) int day_count_of_month[2][12] = {{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}}; void convert_to_date(int year, int n) { int leap = IS_LEAP(year); if(year < 1970 || year > 5000 || n <= 0 || n > 365 + leap) printf("bad year/n"); else { int i = 0; for(; n > day_count_of_month[leap][i]; ++i) n -= day_count_of_month[leap][i]; printf("year : %d, month : %d, day : %d", year, i + 1, n); } } int main() { convert_to_date(2009, 100); }

你可能感兴趣的:(经典算法,C/C++)