Friday the Thirteenth 黑色星期五ios
13號又是一個星期五。13號在星期五比在其餘日子少嗎?爲了回答這個問題,寫一個程序,要求計算每一個月的十三號落在週一到週日的次數。函數
給出N年的一個週期,要求計算1900年1月1日至1900+N-1年12月31日中十三號落在週一到週日的次數,N爲正整數且不大於400.spa
注意,開始今年是一千九百年,不是1990code
這裏有一些你要知道的:blog
一、1900年1月1日是星期一.string
二、4,6,11和9月有30天.其餘月份除了2月都有31天.閏年2月有29天,平年2月有28天.it
三、年份能夠被4整除的爲閏年(1992=4*498 因此 1992年是閏年,可是1990年不是閏年).io
四、以上規則不適合於世紀年。能夠被400整除的世紀年爲閏年,不然爲平年。因此,1700,1800,1900和2100年是平年,而2000年是閏年.class
請不要調用現成的函數stream
請不要預先算好數據(就是叫不許打表)!
PROGRAM NAME: friday
INPUT FORMAT:
(friday.in)
一個正整數n.
OUTPUT FORMAT:
(friday.out)
七個在一行且相分開的整數,它們表明13日是星期六,星期日,星期一...星期五的次數..
20
36 33 34 33 35 35 34
依舊模擬...一天一天的模擬下去
注意處理幾號星期幾就行了~~~
1 /* 2 ID: jvxie1 3 PROG: friday 4 LANG: C++ 5 */ 6 #include<cstdio> 7 #include<cstring> 8 #include<iostream> 9 #include<algorithm> 10 using namespace std; 11 int month[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}}; 12 int day[10]; 13 int leap(int year) 14 { 15 if((year%4==0&&year%100!=0)||(year%400==0)) 16 return 1; 17 return 0; 18 } 19 int work(int x,int year,int k) 20 { 21 int date=1; 22 for(int i=0;i<12;i++) 23 for(int j=1;j<=month[x][i];j++) 24 { 25 date++; 26 date%=month[x][i]; 27 k++;k%=7; 28 if(date==13) 29 day[k]++; 30 } 31 return k; 32 } 33 int main() 34 { 35 freopen("friday.in","r",stdin); 36 freopen("friday.out","w",stdout); 37 int n,k=1; 38 scanf("%d",&n); 39 for(int i=1900;i<=1900+n-1;i++) 40 k=work(leap(i),i,k); 41 printf("%d %d %d %d %d %d %d\n",day[6],day[0],day[1],day[2],day[3],day[4],day[5]); 42 return 0; 43 }