USACO Section1.1 Friday the Thirteenth
来源:互联网 发布:股票指标软件 编辑:程序博客网 时间:2024/06/09 19:26
题目:
13号又是一个星期五。13号在星期五比在其他日子少吗?为了回答这个问题,写一个程序,要求计算每个月的十三号落在周一到周日的次数。给出N年的一个周期,要求计算1900年1月1日至1900+N-1年12月31日中十三号落在周一到周日的次数,N为正整数且不大于400.
注意,开始今年是1900年,不是1990年.
这里有一些你要知道的:
1、1900年1月1日是星期一.
2、4,6,11和9月有30天.其他月份除了2月都有31天.闰年2月有29天,平年2月有28天.
3、年份可以被4整除的为闰年(1992=4*498 所以 1992年是闰年,但是1990年不是闰年).
4、以上规则不适合于世纪年。可以被400整除的世纪年为闰年,否则为平年。所以,1700,1800,1900和2100年是平年,而2000年是闰年.
请不要调用现成的函数.
请不要预先算好数据(打表).
格式:
输入:
仅一行,一个正整数n。
输出:
仅一行,七个在一行且相分开的整数,它们代表13日是星期六,星期日,星期一…星期五的次数。
题解:
定义一个一维数组来存储每个周几的出现次数,在每一年的循环中,大月小月的判断不变,只有闰年与否需要注意,用k来表示每个月的天数,用t表示周几 。
代码:
/* ID: a4556762PROG: friday LANG: C++*/#include<cstdio>#include<iostream>#include<string> #include<cstring>#include<cstdlib>using namespace std;int main(){ freopen("friday.in","r",stdin); freopen("friday.out","w",stdout); int n,t=0,b[8]={0},i; cin>>n; for(i=1;i<=n;i++) { int k,j,m; for(j=1;j<=12;j++) { if(j==1||j==3||j==5||j==7||j==8||j==10||j==12) k=31; if(j==4||j==6||j==9||j==11) k=30; if(j==2) { if(((1900+i-1)%4==0&&(1900+i-1)%100!=0)||(1900+i-1)%400==0) k=29; else k=28; } for(m=1;m<=k;m++) { if(t>=7) t=0; t++; if(m==13) b[t]++; } } } for(i=6;i<=7;i++) cout<<b[i]<<" "; for(i=1;i<=4;i++) cout<<b[i]<<" "; cout<<b[i]<<endl; return 0;}
阅读全文
0 0
- USACO-Section1.1 Friday the Thirteenth [日期]
- USACO-Section1.1 Friday the Thirteenth [日期]
- USACO-Section1.1 Friday the Thirteenth
- USACO-Section1.1 Friday the Thirteenth
- USACO-Section1.1 Friday the Thirteenth
- USACO-Section1.1 Friday the Thirteenth[日期]
- USACO Section1.1 Friday the Thirteenth
- USACO-Section1.1 Friday the Thirteenth【泰勒公式】
- USACO之Section1.1.3 Friday the Thirteenth
- Friday the Thirteenth-section1.1
- USCAO-Section1.1 Friday the Thirteenth
- Friday the Thirteenth(USACO)
- usaco--Friday the Thirteenth
- [USACO] Friday the Thirteenth
- usaco Friday the Thirteenth
- USACO Friday the Thirteenth
- USACO:Friday the Thirteenth
- USACO -Friday the Thirteenth
- Hibernate入门02_Session
- 学习·书目
- JavaScript面向对象和高级02
- 栈和堆
- 为什么父类引用可以指向子类对象 子类引用不能指向父类对象 泛型
- USACO Section1.1 Friday the Thirteenth
- Linux 学习(七)--- 用户和组账户管理
- go语言部分runtime库函数
- Junit4参数化设置以及测试套件使用
- Android 文件中断续传
- 字节流和字符流
- AOJ0033 Ball【贪心+序列处理】
- [Nmap渗透测试指南]第三章(探索网络)
- 【算法题】统计字符串单词个数