Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __PROGTEST__
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include <assert.h>
- #endif /* __PROGTEST__ */
- int prestuprok(int y)
- {
- if((y%4==0&&y%100!=0)||(y%400==0&&y%4000!=0))
- return 1;
- else
- return 0;
- }
- int DenvTydnu(int den, int mesic, int rok, int d1, int m1, int y1)
- {
- den = d1;
- mesic = m1;
- rok = y1;
- int a = ( 14 - mesic ) / 12;
- int y = rok - a;
- int m = mesic + 12 * a - 2;
- return (( 7000 + ( den + y + y / 4 - y/100 + y / 400 + ( 31 * m ) / 12 )) + 1 ) % 7;
- }
- int CountFriday13 ( int y1, int m1, int d1,int y2, int m2, int d2, long long int * cnt ) {
- int c=(y2-y1-1)/28000;
- int i,j;
- int den=6;
- int mesicDny[]={31,28,31,30,31,30,31,31,30,31,30,31};
- int mesicDnyPrestupRok[]={31,29,31,30,31,30,31,31,30,31,30,31};
- if(y1>y2||y1<1900||(y1==y2&&m1>m2)||(y1==y2&&m1==m2&&d1>d2)||m1>12||m1<1||m2<1||m2>12||d1<1||d2<1)
- return 0;
- if(prestuprok(y1)){
- if(mesicDnyPrestupRok[m1-1]<d1)
- return 0;
- }
- else if(!prestuprok(y1)){
- if(mesicDny[m1-1]<d1)
- return 0;
- }
- if(prestuprok(y2)){
- if(mesicDnyPrestupRok[m2-1]<d2)
- return 0;
- }
- else if(!prestuprok(y2)){
- if(mesicDny[m2-1]<d2)
- return 0;
- }
- *cnt=0;
- if(y1==y2&&m1==m2){
- if(d1>13)
- return 1;
- else if(d2<13)
- return 1;
- else if(d1<=13&&d2>=13){
- if(DenvTydnu(13,i,y1,d1,m1,y1)==den){
- *cnt+=1;}
- }
- return 1;
- }
- if(y1==y2){
- if(d1>13&&d2<13){
- if(m1+1<=m2-1){
- for(i=m1+1;i<=m2-1;i++){
- if(DenvTydnu(13,i,y1,d1,m1,y1)==den){
- *cnt+=1;}
- }
- }
- }
- else if(d1<=13&&d2>=13){
- for(i=m1;i<=m2;i++){
- if(DenvTydnu(13,i,y1,d1,m1,y1)==den){
- *cnt+=1;}
- }
- }
- else if(d1<=13&&d2<13){
- for(i=m1;i<=m2-1;i++){
- if(DenvTydnu(13,i,y1,d1,m1,y1)==den){
- *cnt+=1;}
- }
- }
- else if(d1>13&&d2>=13){
- for(i=m1+1;i<=m2;i++){
- if(DenvTydnu(13,i,y1,d1,m1,y1)==den){
- *cnt+=1;}
- }
- }
- return 1;
- }
- for(i=m1;i<=12;i++){
- if(DenvTydnu(13,i,y1,d1,m1,y1)==den){
- *cnt+=1;
- }
- }
- *cnt+=c*48000;
- for(i=y1+1+c*28000;i<=y2;i++){
- if(i!=y2){
- for(j=1;j<=12;j++){
- if(DenvTydnu(13,j,i,d1,m1,y1)==den){
- *cnt+=1;
- }
- }
- }
- else{
- for(j=1;j<=m2;j++){
- if(j!=m2){
- if(DenvTydnu(13,j,i,d1,m1,y1)==den){
- *cnt+=1;
- }
- }
- else{
- if(d2>=13){
- if(DenvTydnu(13,j,i,d1,m1,y1)==den){
- *cnt+=1; }
- }
- }
- }
- }
- }
- return 1;
- }
- #ifndef __PROGTEST__
- int main ( int argc, char * argv [] )
- {
- long long int cnt;
- assert ( CountFriday13 ( 1900, 1, 1,
- 2015, 1, 1, &cnt ) == 1
- && cnt == 197LL );
- assert ( CountFriday13 ( 1900, 1, 1,
- 2015, 2, 1, &cnt ) == 1
- && cnt == 197LL );
- assert ( CountFriday13 ( 1900, 1, 1,
- 2015, 1, 13, &cnt ) == 1
- && cnt == 197LL );
- assert ( CountFriday13 ( 1900, 1, 1,
- 2015, 2, 13, &cnt ) == 1
- && cnt == 198LL );
- assert ( CountFriday13 ( 1904, 1, 1,
- 2015, 1, 1, &cnt ) == 1
- && cnt == 189LL );
- assert ( CountFriday13 ( 1904, 1, 1,
- 2015, 2, 1, &cnt ) == 1
- && cnt == 189LL );
- assert ( CountFriday13 ( 1904, 1, 1,
- 2015, 1, 13, &cnt ) == 1
- && cnt == 189LL );
- assert ( CountFriday13 ( 1904, 1, 1,
- 2015, 2, 13, &cnt ) == 1
- && cnt == 190LL );
- assert ( CountFriday13 ( 1905, 2, 13,
- 2015, 1, 1, &cnt ) == 1
- && cnt == 187LL );
- assert ( CountFriday13 ( 1905, 2, 13,
- 2015, 2, 1, &cnt ) == 1
- && cnt == 187LL );
- assert ( CountFriday13 ( 1905, 2, 13,
- 2015, 1, 13, &cnt ) == 1
- && cnt == 187LL );
- assert ( CountFriday13 ( 1905, 2, 13,
- 2015, 2, 13, &cnt ) == 1
- && cnt == 188LL );
- assert ( CountFriday13 ( 1905, 1, 13,
- 2015, 1, 1, &cnt ) == 1
- && cnt == 188LL );
- assert ( CountFriday13 ( 1905, 1, 13,
- 2015, 2, 1, &cnt ) == 1
- && cnt == 188LL );
- assert ( CountFriday13 ( 1905, 1, 13,
- 2015, 1, 13, &cnt ) == 1
- && cnt == 188LL );
- assert ( CountFriday13 ( 1905, 1, 13,
- 2015, 2, 13, &cnt ) == 1
- && cnt == 189LL );
- assert ( CountFriday13 ( 2015, 11, 1,
- 2015, 10, 1, &cnt ) == 0 );
- assert ( CountFriday13 ( 2015, 10, 32,
- 2015, 11, 10, &cnt ) == 0 );
- assert ( CountFriday13 ( 2090, 2, 29,
- 2090, 2, 29, &cnt ) == 0 );
- assert ( CountFriday13 ( 2096, 2, 29,
- 2096, 2, 29, &cnt ) == 1
- && cnt == 0LL );
- assert ( CountFriday13 ( 2100, 2, 29,
- 2100, 2, 29, &cnt ) == 0 );
- assert ( CountFriday13 ( 2000, 2, 29,
- 2000, 2, 29, &cnt ) == 1
- && cnt == 0LL );
- return 0;
- }
- #endif /* __PROGTEST__ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement