题目:
定义一个结构体变量(包括年、月、日),编程序,要求输入年月日,计算并输出该日在本年中第几天。
样例输入:
1985 1 20 2006 3 12
样例输出:
20 71
代码:
#include<stdio.h>
struct Date
{
int year;
int month;
int day;
};
int main()
{
struct Date date;
while(~(scanf("%d %d %d",&date.year,&date.month,&date.day)))
{
int num[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int days=0;
if(date.month==1)
{
days=date.day;
}
else
{
for(int i=1;i<date.month;i++)
{
days+=num[i];
}
days+=date.day;
if((date.year%400==0||(date.year%100!=0&&date.year%4==0))&&date.month>2)
{
days++;
}
}
printf("%d\n",days);
}
return 0;
}
这是一个C语言程序,用于输入年月日并计算该日期在当年中的第几天。程序通过数组存储每月的天数,并考虑了闰年的特殊情况。样例输入包括1985年12月和2006年3月,输出分别为20和71。

807

被折叠的 条评论
为什么被折叠?



