输入某一年的第几天,计算并输出它是这一年的第几月第几日,c语言,怎么写,谢谢
3个回答
2019-02-01
展开全部
#include <stdio.h>
#include <stdlib.h>
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
main()
{
int year,day,d,m,leap;
int i;
int Month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
printf("输入年份和这一年的第多少天:\n");
scanf("%d",&year);
scanf("%d",&day);
if( (year%4!=0) ||( (year%100==0)&& (year%400!=0)))
leap=0;//不是闰年
else
leap=1;//是闰年
if(leap==1)
Month[1]=29;//闰年二月29天
m=1;
for(i=0;i0)
{
day=d;
m++;//月数加1
}
else
{
d = d+Month[i];
break;
}
}
printf("The date is:%d年%d月%d日",year,m,d);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <math.h>
#include <stdio.h>
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询