c语言编程:用数组的知识来设计一个程序 输入一个日期,计算这一天是一年中的第几天

2024-11-30 23:03:54
推荐回答(2个)
回答1:

#include
main()
{int a[12]={31,28,31,30,31,30,31,31,30,31,30,31},/*平年的*/
b[12]={31,29,31,30,31,30,31,31,30,31,30,31};/*闰年的*/
int year,month,day,sum,n=0;
scanf("%d,%d,%d",&year,&month,&day);
if(year%4==0)
{for(sum=0;n sum=sum+b[n];
sum=sum+day;}
else
{ for(sum=0;n sum=sum+a[n];
sum=sum+day;}
printf("%d",sum);
getchar();
}

回答2:

#include
int day_of_year(int (*p)[13],int year,int month,int day)
{
int i,leap;
leap=(year%100 !=0 && year%4 ==0||year%400 ==0);
for(i=1;iday+=*(*(p+leap)+i);
return day;
}
main()
{
static int day_tab[][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
printf("%d\n",day_of_year(day_tab,a,b,c));
}