C语言:输入年份和天数,输出对应的年、月、日
2个回答
展开全部
下面是这段代码,配合注释体会一下吧
#include <stdio.h>
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 <stdio.h>
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;
}
2015-06-13
展开全部
#include <stdio.h>
#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\n",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;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询