OR71. 计算一年中的第几天
描述
今年的第几天?
输入年、月、日,计算该天是本年的第几天。
输入描述
包括三个整数年(1<=Y<=3000)、月(1<=M<=12)、日(1<=D<=31)。输出描述
输入可能有多组测试数据,对于每一组测试数据,示例1
输入:
1990 9 20 2000 5 1
输出:
263 122
C 解法, 执行用时: 1ms, 内存消耗: 256KB, 提交时间: 2020-07-07
#include<stdio.h> #include<string.h> int nian_fun(int n){ switch (n){ case 1:return 0; case 2:return 31; case 3:return 59; case 4:return 90; case 5:return 120; case 6:return 151; case 7:return 181; case 8:return 212; case 9:return 243; case 10:return 273; case 11:return 304; case 12:return 334; default:return 0; } } int runn_fun(int n){ switch (n){ case 1:return 0; case 2:return 31; case 3:return 60; case 4:return 91; case 5:return 121; case 6:return 152; case 7:return 182; case 8:return 213; case 9:return 244; case 10:return 274; case 11:return 305; case 12:return 335; default:return 0; } } int main(void){ int year,month,day; while(scanf("%d %d %d",&year,&month,&day)!=EOF){ if(year%4==0){ printf("%d\n",runn_fun(month)+day); } else printf("%d\n",nian_fun(month)+day); } }
C 解法, 执行用时: 2ms, 内存消耗: 224KB, 提交时间: 2019-04-27
#include<stdio.h> int main() { int a[12]={31,28,31,30,31,30,31,31,30,31,30,31}; int year,month,day; int days=0,i; scanf("%d%d%d",&year,&month,&day); for(i = 0; i < month -1;i++) days+=a[i]; days+=day; if(((year%4==0&&year%100!=0)||(year%400==0))&& month >= 3) days+=1; printf("%d\n",days); return 0; }