题目1186:打印日期 题目9

这道题需要注意的是:年份输出的时候格式 %04d

题目描述:

给出年分m和一年中的第n天,算出第n天是几月几号。

输入:

输入包括两个整数y(1<=y<=3000),n(1<=n<=366)。

输出:

可能有多组测试数据,对于每组数据,
按 yyyy-mm-dd的格式将输入中对应的日期打印出来。

样例输入:
2000 3
2000 31
2000 40
2000 60
2000 61
2001 60
样例输出:
2000-01-03
2000-01-31
2000-02-09
2000-02-29
2000-03-01
2001-03-01
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
 
using namespace std;
//判断是否是闰年,是为true,不是为false
int IsRunNian(int iYear)
{
if (0 == iYear%400)
{
return 1;
}

if (0 == iYear%4 && 0 != iYear%100)
{
return 1;
}

return 0;
}
int iCount[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 main()

int iYear,iDay;
while (scanf("%d %d",&iYear,&iDay)!=EOF)
{
int iMonth=0,i=0;
int iIs=IsRunNian(iYear);
if (0==iIs&&366==iDay)
{
iYear++;iMonth=0;iDay=1;
}
else
{
for (i=0;i<12;i++)
{
if (iDay-iCount[iIs][iMonth]>0)
{
iDay-=iCount[iIs][iMonth];
iMonth++;
}
else
{
break;
}
}
}
printf("%04d-%02d-%02d\n",iYear,iMonth+1,iDay);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值