题滴链接http://118.190.20.162/view.page?gpid=T31
问题描述
试题编号: | 201509-2 |
试题名称: | 日期计算 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 给定一个年份y和一个整数d,问这一年的第d天是几月几日? 输入格式 输入的第一行包含一个整数y,表示年份,年份在1900到2015之间(包含1900和2015)。 输出格式 输出两行,每行一个整数,分别表示答案的月份和日期。 样例输入 2015 样例输出 3 样例输入 2000 样例输出 2 |
鄙人拙见:
- 先判断是闰年还是平年
- 分别建每月相对应的天数的数组
- 大题来看每够1月则月数加1,而减少相对应的天数
- 注意边界
C++参考代码1.0:
#include<bits/stdc++.h>
using namespace std;
int runorping(int y)
{
int flag = 0;
if( ( y % 4 == 0 && y % 100 != 0 ) || ( y % 400 == 0 ))
{
flag = 1;
}
return flag;
}
int main()
{
int y,d;
scanf("%d",&y);
scanf("%d",&d);
int flag;
flag = runorping(y);
/*1 2 3 4 5 6 7 8 9 10 11 12*/
int ping[15] = {31,28,31,30,31,30,31,31,30,31,30,31};
/*1 2 3 4 5 6 7 8 9 10 11 12*/
int run[15] = {31,29,31,30,31,30,31,31,30,31,30,31};
int count = 1;
if(flag == 0)
{
int t = 0;
while( d > 28)
{
d = d - ping[t];
t = t + 1;
count = count + 1;
}
if(d == 0 )
{
count = count - 1;
if(t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t== 10 || t == 12)
{
d = 31;
}
if(t == 4 || t == 6 || t == 9 || t == 11)
{
d = 30;
}
}
}
else
{
int t = 0;
while( d > 29)
{
d = d - run[t];
t = t + 1;
count = count + 1;
}
if(d == 0 )
{
count = count - 1;
if(t == 1 || t == 3 || t == 5 || t == 7 || t == 8 || t== 10 || t == 12)
{
d = 31;
}
if(t == 4 || t == 6 || t == 9 || t == 11)
{
d = 30;
}
}
}
printf("%d\n",count);
printf("%d",d);
return 0;
}