USACO-Section1.1 Friday the Thirteenth[日期]
2017-05-28 15:29
393 查看
2017-5-28
题目大意:
样例输入:
20
样例输出:
36 33 34 33 35 35 34
题解:
个人考虑了以下几点:
(1)用一个二维数组保存每年每个月的天数,第一维表示非闰年,第二维表示闰年,每一维有13位,月份从下标1开始;
(2)星期一~星期天用一维数组从下标1到7的单元表示,将累计天数模7时,如果余数为0,要计为7;
(3)1900年1月1号是星期一,那么1900年1月13号正好是星期六。
此外:输出是每两个数字之间才有空格,最后一个数字后面仅有回车。
代码如下:
题目大意:
输入为一个数值N(0<N<=400),求从1900年1月1号起到1900+N-1年12月31 号每个月的13号为星期一~星期天之间每个可能的累积数目。已知1900年1月1 号是星期一,闰年时二月有29天,其他年二月有28天。闰年就是年份可以整除4 且无法整除100,或者能够整除400的年份。
样例输入:
20
样例输出:
36 33 34 33 35 35 34
题解:
个人考虑了以下几点:
(1)用一个二维数组保存每年每个月的天数,第一维表示非闰年,第二维表示闰年,每一维有13位,月份从下标1开始;
(2)星期一~星期天用一维数组从下标1到7的单元表示,将累计天数模7时,如果余数为0,要计为7;
(3)1900年1月1号是星期一,那么1900年1月13号正好是星期六。
此外:输出是每两个数字之间才有空格,最后一个数字后面仅有回车。
代码如下:
/* ID: madara01 PROG: friday LANG: C++ */ #include <iostream> #include <fstream> #include <string> #define cin fin #define cout fout using namespace std; int* days = new int[8](); //一周的7天 int month[2][13] = {{0,31,28,31,30,31,30,31,31,30,31,30,31}, {0,31,29,31,30,31,30,31,31,30,31,30,31}}; //每月的天数 int isLeap(int year) { if((year % 4 == 0 && year % 100 != 0 )|| year % 400 ==0) { return 1; //是闰年 } return 0; //不是闰年 } void computeIt(int last) { int year = 1900; int leap; //是否为闰年 int monNum; //月份 int dayNum = 1; //总计天数,初值为1表示1900.1.1是星期一 for(; year<last; year++) { leap = isLeap(year); for(monNum = 1; monNum<=12; monNum++) { dayNum = dayNum % 7; if(dayNum == 0) dayNum = 7; //星期天 days[dayNum] ++; dayNum = dayNum + month[leap][monNum]; } } } int main() { int n,i; for(i = 1; i<8; i++) { days[i] = 0; } ofstream fout ("friday.out"); ifstream fin ("friday.in"); cin >> n; int last = 1900 + n; computeIt(last); cout << days[1] << " " << days[2] << " " << days[3] << " " << days[4] << " " << days[5] << " " << days[6] << " " << days[7] << endl; return 0; }
相关文章推荐
- USACO-Section1.1 Friday the Thirteenth [日期]
- USACO-Section1.1 Friday the Thirteenth [日期]
- USACO Section 1.1 Friday the Thirteenth
- USACO-Section1.1 Friday the Thirteenth
- USACO Section 1.1 Friday the Thirteenth
- USACO-Section1.1 Friday the Thirteenth【泰勒公式】
- USACO - Chapter1 Section 1.1 - Friday the Thirteenth
- USACO-Section1.1 Friday the Thirteenth
- USACO Section 1.1-3 Friday the Thirteenth
- USACO section 1.1 Friday the Thirteenth
- USACO:Section 1.1 Friday the Thirteenth
- USACO-Section1.2 Friday the Thirteenth (简单日期处理)
- 洛谷 [USACO1.1]黑色星期五Friday the Thirteenth
- [USACO1.1]黑色星期五Friday the Thirteenth-洛谷 1202
- USCAO-Section1.1 Friday the Thirteenth
- USACO 1.1 Friday the Thirteenth (friday)
- luogu1202 [USACO1.1]黑色星期五Friday the Thirteenth
- 【USACO-Chapter1-1.1】【模拟】Friday the Thirteenth
- USACO 1.1 Friday the Thirteenth
- USACO 1.1 Friday the Thirteenth