试题编号: 201509-2
试题名称: 日期计算
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
给定一个年份y和一个整数d,问这一年的第d天是几月几日?
注意闰年的2月有29天。满足下面条件之一的是闰年:
1) 年份是4的整数倍,而且不是100的整数倍;
2) 年份是400的整数倍。
输入格式
输入的第一行包含一个整数y,表示年份,年份在1900到2015之间(包含1900和2015)。
输入的第二行包含一个整数d,d在1至365之间。
输出格式
输出两行,每行一个整数,分别表示答案的月份和日期。
样例输入
2015
80
样例输出
3
21
样例输入
2000
40
样例输出
2
9
代码:
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
int months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
void flagRun(int year){
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
months[1] = 29;
}else{
months[1] = 28;
}
}
int main()
{
int year ,days;
while(cin >> year >> days){
flagRun(year);
int month = 0;
for(int i = 0 ; i < 12; i++){
if(days <= months[i]){
month = i;
break;
}
days -= months[i];
}
cout << (month + 1) << endl;
cout << days << endl;
}
return 0;
}
/* 测试用例
2015 80
2000 40
*/
/* 每次做的还不太一样
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
int months[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
void runnian(int year){
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
months[1] = 29;
}else{
months[1] = 28;
}
}
int main()
{
int year , day;
while(cin >> year >> day){
runnian(year);
int days = 0;
int index = 0;
for(int i = 0 ; i < 12 ; i++){
days += months[i];
if(days >= day){
index = i;
break;
}
}
days -= months[index];
int nowday = day - days;
cout << index + 1 << endl;
cout << nowday << endl;
}
return 0;
}
*/
6311

被折叠的 条评论
为什么被折叠?



