问题描述:
输入年月日,计算该天是本年的第N天。如输入数据为y=2017,m=12,d=24,则输出结果N=358。
代码展示:
1 #include<stdio.h> 2 int isRunnian(int year); 3 int main(){ 4 int year,month,day; 5 int days=0; //统计一年有多少天 6 printf("请分别输入年、月、日:"); 7 scanf("%d %d %d",&year,&month,&day); 8 days += day; 9 switch(month){ 10 case 12: 11 days += 30; 12 case 11: 13 days += 31; 14 case 10: 15 days += 30; 16 case 9: 17 days += 31; 18 case 8: 19 days += 31; 20 case 7: 21 days += 30; 22 case 6: 23 days += 31; 24 case 5: 25 days += 30; 26 case 4: 27 days += 31; 28 case 3: 29 if(isRunnian(year) == 1){ 30 days += 29; 31 }else if(isRunnian(year) == 0){ 32 days += 28; 33 } 34 case 2: 35 days += 31; 36 default: 37 break; 38 } 39 printf("%d年%d月%d日是本年的第 %d天\n",year,month,day,days); 40 41 } 42 43 int isRunnian(int year){ //判断闰年 44 if(year%4==0 && year%100!=0){ 45 return 1; 46 } 47 if(year%400 == 0){ 48 return 1; 49 } 50 return 0; 51 }
运行截图: