HJ73. 计算日期到天数转换
描述
输入描述
输入一行,每行空格分割,分别是年,月,日
输出描述
输出是这一年的第几天示例1
输入:
2012 12 31
输出:
366
示例2
输入:
1982 3 4
输出:
63
C 解法, 执行用时: 1ms, 内存消耗: 340KB, 提交时间: 2021-09-12
#include <stdio.h> int isleapyear(int year) { if(0 == year%4) { if(0 == year%100 && 0 != year%400) return 0; else return 1; } else return 0; } int main() { int daynum_of_leapyear[12] = {31,29,31,30,31,30,31,31,30,31,30,31}; int daynum_of_commonyear[12] = {31,28,31,30,31,30,31,31,30,31,30,31}; int year,month,day; scanf("%d", &year); scanf("%d", &month); scanf("%d", &day); int daynum = 0; if(1 == isleapyear(year)) { for(int i =0; i<month-1; i++) { daynum += daynum_of_leapyear[i]; } } else { for(int i =0; i<month-1; i++) { daynum += daynum_of_commonyear[i]; } } daynum += day; printf("%d",daynum); return 0; }
C 解法, 执行用时: 1ms, 内存消耗: 348KB, 提交时间: 2021-09-09
#include<stdio.h> int main(){ int year, month, day; int md[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int res = 0; scanf("%d %d %d", &year, &month, &day); //check if run year if(year %4 == 0 && year % 100 != 0 || year % 40 == 0){ md[1] = 29; } for(int i = 0; i < month - 1; i++){ res = res + md[i]; } res += day; printf("%d", res); return 0; }