C语言:输入年份和天数,输出对应的年、月、日
答案:2 悬赏:40 手机版
解决时间 2021-03-28 17:08
- 提问者网友:嗝是迷路的屁
- 2021-03-27 20:56
C语言:输入年份和天数,输出对应的年、月、日
最佳答案
- 五星知识达人网友:七十二街
- 2021-03-27 22:05
下面是这段代码,配合注释体会一下吧
#include
void MonthDay(int year,int yearDay,int *pMonth,int *pDay);
int main()
{
int year,yearDay,month,day;
scanf("%d%d",&year,&yearDay);//输入年份和天数
MonthDay(year,yearDay,&month,&day);
printf("%d-%d-%d",year,month,day);
return 0;
}
void MonthDay(int year,int yearDay,int *pMonth,int *pDay)
{
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};//这个数组保存每月的天数
if(year%4==0&&year%100!=0||year%400==0)//判断是否为闰年
days[1]=29;//闰年的二月有29天
*pMonth=1;//从一月开始看
while(yearDay>days[*pMonth-1])//如果哪一天超出这个月
{
yearDay-=days[*pMonth-1];//求出超出此月的天数
(*pMonth)++;//继续考虑下一月
}
*pDay=yearDay;
}
#include
void MonthDay(int year,int yearDay,int *pMonth,int *pDay);
int main()
{
int year,yearDay,month,day;
scanf("%d%d",&year,&yearDay);//输入年份和天数
MonthDay(year,yearDay,&month,&day);
printf("%d-%d-%d",year,month,day);
return 0;
}
void MonthDay(int year,int yearDay,int *pMonth,int *pDay)
{
int days[12]={31,28,31,30,31,30,31,31,30,31,30,31};//这个数组保存每月的天数
if(year%4==0&&year%100!=0||year%400==0)//判断是否为闰年
days[1]=29;//闰年的二月有29天
*pMonth=1;//从一月开始看
while(yearDay>days[*pMonth-1])//如果哪一天超出这个月
{
yearDay-=days[*pMonth-1];//求出超出此月的天数
(*pMonth)++;//继续考虑下一月
}
*pDay=yearDay;
}
全部回答
- 1楼网友:西岸风
- 2021-03-27 22:18
#include
#define ISLEAP(y) ((y)%400==0||(y)%4==0&&(y)%100!=0)
void MonthDay(int year,int yearDay,int *pMonth,int *pDay);
int main(void)
{
int year,yearDay,month,day;
while(scanf("%d%d",&year,&yearDay)!=EOF)
{
MonthDay(year,yearDay,&month,&day);
printf("%d-%d-%d ",year,month,day);
}
return 0;
}
void MonthDay(int year,int yearDay,int *pMonth,int *pDay)
{
int i,date[]={31,28,31,30,31,30,31,31,30,31,30,31};
for(i=0,*pMonth=1;yearDay>date[i];++i,++*pMonth)
{
yearDay-=i==1?date[i]+ISLEAP(year):date[i];
}
if(yearDay==0)
{
--*pMonth;
*pDay=*pMonth-1==1?date[*pMonth-1]+ISLEAP(year):date[*pMonth-1];
}
else
{
*pDay=yearDay;
}
}
#define ISLEAP(y) ((y)%400==0||(y)%4==0&&(y)%100!=0)
void MonthDay(int year,int yearDay,int *pMonth,int *pDay);
int main(void)
{
int year,yearDay,month,day;
while(scanf("%d%d",&year,&yearDay)!=EOF)
{
MonthDay(year,yearDay,&month,&day);
printf("%d-%d-%d ",year,month,day);
}
return 0;
}
void MonthDay(int year,int yearDay,int *pMonth,int *pDay)
{
int i,date[]={31,28,31,30,31,30,31,31,30,31,30,31};
for(i=0,*pMonth=1;yearDay>date[i];++i,++*pMonth)
{
yearDay-=i==1?date[i]+ISLEAP(year):date[i];
}
if(yearDay==0)
{
--*pMonth;
*pDay=*pMonth-1==1?date[*pMonth-1]+ISLEAP(year):date[*pMonth-1];
}
else
{
*pDay=yearDay;
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯