C语言(21)日期计算

来源:互联网 发布:excel表格怎么数据统计 编辑:程序博客网 时间:2024/05/20 22:02
描述
如题,输入一个日期,格式如:2010 10 24 ,判断这一天是这一年中的第几天。
输入
第一行输入一个数N(0<N<=100),表示有N组测试数据。后面的N行输入多组输入数据,每行的输入数据都是一个按题目要求格式输入的日期。
输出
每组输入数据的输出占一行,输出判断出的天数n
样例输入
32000 4 52001 5 42010 10 24
样例输出
96124297
#include <stdio.h>int run(int);int which_day(int);int main(void){int n, year, month, day, flag;scanf("%d", &n);while(n--){scanf("%d%d%d", &year, &month, &day);flag=0;if((0 == run(year)) && (month > 2)){flag=1;}printf("%d\n", which_day(month)+day+flag);}return 0;}/*判断是否为闰年*/int run(int year){if(year%4 == 0){if(year%100==0 && year%400 != 0){return -1;}else{return 0;}}else{return -1;}}/*判断是该年的哪一天*/int which_day(int month){int sum;switch(month){case 1:sum=0;break;case 2:sum=31;break;case 3:sum=59;break;case 4:sum=90;break;case 5:sum=120;break;case 6:sum=151;break;case 7:sum=181;break;case 8:sum=212;break;case 9:sum=243;break;case 10:sum=273;break;case 11:sum=304;break;case 12:sum=334;break;default:sum=-1;break;}return sum;}

原创粉丝点击