C语言定义一个结构体变量(包括年、月、日),输入一个日期,计算该日在本年中是第几天。

考虑闰年 2.有多组测试用例,每组测试用例一行,包括年月日中间用空格隔开,直到文件尾位置
样例输入:1992 1 30
2003 2 20
1900 3 1
2008 12 31
1902 3 1
样例输出:30
51
60
366
60

//希望我的回答对你的学习有帮助
#include <stdio.h>

struct ymd { 
int Y,M,D; 
}; 
const short MonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 
 
int YMD_2_JD(int Y, int M, int D){ 
const short MonthDay[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; 
int JD,i; 
JD=D; 
for (i=0;i<M;i++) JD+=MonthDay[i]; 
if (((Y%4==0)&&(Y%100!=0)||(Y%400==0)) && (M>2)) JD++;  
return JD; 

  
 
int main() { 
int d; 
struct ymd a;  
while(~scanf("%d%d%d",&a.Y,&a.M,&a.D)){
d=YMD_2_JD(a.Y,a.M,a.D); 
printf("%d\n",d); 


return 0; 
}

温馨提示:答案为网友推荐,仅供参考
相似回答