C语言经典题求助:题目:输入某年某月某日,判断这一天是这一年的第几天?

2024-11-26 14:24:44
推荐回答(1个)
回答1:

/*你的要这样改*, scanf("%d,%d,%d",&i,&j,&k); 你的错了 */
/* scanf("%d%d%d",&i,&j,&k);对/
#include
int main()
{
int i,j,k,x,y;
scanf("%d%d%d\n",&i,&j,&k);
{if((i%4==0&&i%100!=0)||i%400==0)
y=1;
else y=0;}
switch(j)
{
case 1:x=0;break;
case 2:x=31;break;
case 3:x=31+28+y;break;
case 4:x=31+28+y;break;
case 5:x=30+31+28+y;break;
case 6:x=31+30+31+28+y;break;
case 7:x=30+31+30+31+28+y;break;
case 8:x=31+30+31+30+31+28+y;break;
case 9:x=31+31+30+31+30+31+28+y;break;
case 10:x=30+31+31+30+31+30+31+28+y;break;
case 11:x=31+0+31+31+30+31+30+31+28+y;break;
case 12:x=30+31+0+31+31+30+31+30+31+28+y;break;
}
x+=k;
printf("It is the %dth day\n",x);

/*我的下面*/
#include

int leapyear(int year)/*是否闰年?*/
{
if(year%400==0)return 1;
else if(year%4==0&&year%100!=0)return 1;
return 0;
}

int count(int month,int day)/*不是闰年时是第几天*/
{
int a[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int i,count=0;
for(i=0;i count=count+a[i];
return count+day;
}

void main()
{
int year,month,day,a;

printf("请输入日期(yyyymmdd):");
scanf("%4d%2d%2d",&year,&month,&day);
a=count(month,day);
if(leapyear(year)&&month>2) a=a+1;
printf("%d年%d月%d日是%d年的第%d天。\n",year,month,day,year,a);
}