#include <stdio.h>
void main()
{
int year,month,day,s=0,a,i;
int m[11] = {31,28,31,30,31,30,31,31,30,31,30,31};//建立一维数组,放入每个月的天数
printf("Please input year-month-day : ");
scanf("%d-%d-%d",&year,&month,&day);//输入年月日
if(month > 2 && ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)))//判断闰年且月份大于2
a=1;//如果闰年且月份大于2,在天数上加1
else
a=0;//不是则不加
for(i = 0 ; i < month - 1 ; i ++)
s = s + m[i];//把输入月份之前的每个月天数相加;比如输入4月,则此时s=31+28+31
printf("That is the %d(th) day of %d.\n",s+day+a,year);//所求值为s+day+a
那个,您可以解释一下这个程序吗?谢谢您
已在代码上加了注释
2020-03-22
#include <stdio.h>
#include <math.h>
int main (void) {
int y, m, d, sum = 0;
int days[2][12] = {
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
scanf ("%d %d %d", &y, &m, &d);
for (int i = 0; i < m - 1; i++) {
if ( (y % 4 == 0 && y % 100 != 0) || y % 400 == 0)
sum += days[0][i];
else
sum += days[1][i];
}
sum += d;
printf ("%d", sum);
return 0;
}