C语言-第几天,定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。
1050: C语言-第几天
题目描述
定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。
输入
年月日
输出
当年第几天
样例输入
2000 12 31
样例输出
366
# include<stdio.h>
struct Date
{
int year;
int month;
int day;
}Date1;
int main()
{
int m,n=0,i,j,k,flag=0;
scanf("%d %d %d",&Date1.year,&Date1.month,&Date1.day);
if(Date1.year%4==0) //判断闰年问题
{
if(Date1.year%100!=0)
{
flag=1;
}
}
if(Date1.year%400==0)
{
flag=1;
}
for(i=1;i<Date1.month;i++) //天数的累加
{
switch(i)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: m=31;break;
case 4:
case 6:
case 9:
case 11: m=30;break;
}
if(i==2)
{
if(flag)
{
m=29;
}
else
{
m=28;
}
}
n+=m;
}
n+=Date1.day;
printf("%d",n);
return 0;
}
还没有评论,来说两句吧...