#include
#include
void MonthDay(int year, int yearDay, int *pMonth, int *pDay);
int main()
{
int year,yearDay,pMonth,pDay;
scanf("%d",&year);
scanf("%d",&yearDay);
MonthDay(year,yearDay,&pMonth,&pDay);
printf("\n%d %d",pMonth,pDay);
return 0;
}
void MonthDay(int year, int yearDay, int *pMonth, int *pDay)
{
int day[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},
{31,29,31,30,31,30,31,31,30,31,30,31}};
int flag = 0,i=0;
if(((year%4==0)&&(year%100)!=0)||(year%400==0))
flag = 1;
if (flag == 0&&yearDay>365)
{
printf("天数输入不合法\n");
exit(0);
}
else if (flag == 1&&yearDay>366)
{
printf("天数输入不合法\n");
exit(0);
}
while(yearDay>day[flag][i])
{
yearDay-=day[flag][i];
i++;
}
*pMonth = i+1;
*pDay = yearDay;
}
楼上的答案不对,输入2015,365却显示12月 28号
#include
#include
void Monthday(int year,int yearDay,int *pMonth,int *pDay)
{
int dec ;
if((year%4 == 0 && year %100 != 0) || (year % 400 == 0) )//若是闰年
{
dec = 0;
}
else dec = 1;
if(yearDay >= 1 && yearDay <= 31 )
{
*pMonth = 1;
*pDay = yearDay;
}
else if(yearDay >= 32 && yearDay <= 60 - dec)
{
*pMonth = 2;
*pDay = yearDay - 31;
}
else if(yearDay >= 61- dec && yearDay <= 91- dec )
{
*pMonth = 3;
*pDay = yearDay - 60- dec;
}
else if(yearDay >= 92- dec && yearDay <= 121 - dec)
{
*pMonth = 4;
*pDay = yearDay - 91- dec;
}
else if(yearDay >= 122- dec && yearDay <= 152- dec )
{
*pMonth = 5;
*pDay = yearDay - 121- dec;
}
else if(yearDay >= 153- dec && yearDay <= 183- dec )
{
*pMonth = 6;
*pDay = yearDay - 152- dec;
}
else if(yearDay >= 184- dec && yearDay <= 214 - dec)
{
*pMonth = 7;
*pDay = yearDay - 183- dec;
}
else if(yearDay >= 215- dec && yearDay <= 245 - dec)
{
*pMonth = 8;
*pDay = yearDay - 214- dec;
}
else if(yearDay >= 246- dec && yearDay <= 275- dec )
{
*pMonth = 9;
*pDay = yearDay - 245- dec;
}
else if(yearDay >= 276- dec && yearDay <= 306- dec )
{
*pMonth = 10;
*pDay = yearDay - 275- dec;
}
else if(yearDay >= 307- dec && yearDay <= 336- dec )
{
*pMonth = 11;
*pDay = yearDay - 306- dec;
}
else if(yearDay >= 337- dec && yearDay <= 367- dec )
{
*pMonth = 12;
*pDay = yearDay - 336- dec;
}
}
int main()
{
int year,yearDay;
int *pMonth = new int(0);
int *pDay = new int(0);
printf("请输入 年,日:\n");
scanf("%d,%d",&year,&yearDay);
Monthday(year,yearDay,pMonth,pDay);
printf("month = %d , day = %d\n",*pMonth,*pDay);
return 0;
}