Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.06 KB | None | 0 0
  1. #ifndef __PROGTEST__
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <math.h>
  5. #include <assert.h>
  6. #endif /* __PROGTEST__ */
  7.  
  8. int prestuprok(int y)
  9. {
  10.     if((y%4==0&&y%100!=0)||(y%400==0&&y%4000!=0))
  11.         return 1;
  12.     else
  13.         return 0;
  14. }
  15.  
  16. int DenvTydnu(int den, int mesic, int rok, int d1, int m1, int y1)
  17. {
  18.     den = d1;
  19.     mesic = m1;
  20.     rok = y1;
  21.     int a = ( 14 - mesic ) / 12;
  22.     int y = rok - a;
  23.     int m = mesic + 12 * a - 2;
  24.     return (( 7000 + ( den + y + y / 4 - y/100 + y / 400 + ( 31 * m ) / 12 )) + 1 ) % 7;
  25. }
  26. int CountFriday13 ( int y1, int m1, int d1,int y2, int m2, int d2, long long int * cnt ) {
  27.     int c=(y2-y1-1)/28000;
  28.     int i,j;
  29.     int den=6;
  30.     int mesicDny[]={31,28,31,30,31,30,31,31,30,31,30,31};
  31.     int mesicDnyPrestupRok[]={31,29,31,30,31,30,31,31,30,31,30,31};
  32.  
  33.     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)
  34.         return 0;
  35.     if(prestuprok(y1)){
  36.         if(mesicDnyPrestupRok[m1-1]<d1)
  37.             return 0;
  38.     }
  39.     else if(!prestuprok(y1)){
  40.         if(mesicDny[m1-1]<d1)
  41.             return 0;
  42.     }
  43.     if(prestuprok(y2)){
  44.         if(mesicDnyPrestupRok[m2-1]<d2)
  45.             return 0;
  46.     }
  47.     else if(!prestuprok(y2)){
  48.         if(mesicDny[m2-1]<d2)
  49.             return 0;
  50.     }
  51.  
  52.     *cnt=0;
  53.     if(y1==y2&&m1==m2){
  54.         if(d1>13)
  55.             return 1;
  56.         else if(d2<13)
  57.             return 1;
  58.         else if(d1<=13&&d2>=13){
  59.             if(DenvTydnu(13,i,y1,d1,m1,y1)==den){
  60.                 *cnt+=1;}
  61.         }
  62.         return 1;
  63.     }
  64.  
  65.     if(y1==y2){
  66.         if(d1>13&&d2<13){
  67.             if(m1+1<=m2-1){
  68.                 for(i=m1+1;i<=m2-1;i++){
  69.                     if(DenvTydnu(13,i,y1,d1,m1,y1)==den){
  70.                         *cnt+=1;}
  71.                 }
  72.             }
  73.         }
  74.         else if(d1<=13&&d2>=13){
  75.             for(i=m1;i<=m2;i++){
  76.                 if(DenvTydnu(13,i,y1,d1,m1,y1)==den){
  77.                     *cnt+=1;}
  78.             }
  79.         }
  80.         else if(d1<=13&&d2<13){
  81.             for(i=m1;i<=m2-1;i++){
  82.                 if(DenvTydnu(13,i,y1,d1,m1,y1)==den){
  83.                     *cnt+=1;}
  84.             }
  85.         }
  86.         else if(d1>13&&d2>=13){
  87.             for(i=m1+1;i<=m2;i++){
  88.                 if(DenvTydnu(13,i,y1,d1,m1,y1)==den){
  89.                     *cnt+=1;}
  90.             }
  91.         }
  92.         return 1;
  93.     }
  94.  
  95.  
  96.     for(i=m1;i<=12;i++){
  97.         if(DenvTydnu(13,i,y1,d1,m1,y1)==den){
  98.             *cnt+=1;
  99.         }
  100.     }
  101.     *cnt+=c*48000;
  102.     for(i=y1+1+c*28000;i<=y2;i++){
  103.         if(i!=y2){
  104.             for(j=1;j<=12;j++){
  105.                 if(DenvTydnu(13,j,i,d1,m1,y1)==den){
  106.                     *cnt+=1;
  107.                 }
  108.             }
  109.         }
  110.         else{
  111.             for(j=1;j<=m2;j++){
  112.                 if(j!=m2){
  113.                     if(DenvTydnu(13,j,i,d1,m1,y1)==den){
  114.                         *cnt+=1;
  115.  
  116.                     }
  117.                 }
  118.                 else{
  119.                     if(d2>=13){
  120.                         if(DenvTydnu(13,j,i,d1,m1,y1)==den){
  121.                             *cnt+=1; }
  122.                     }
  123.                 }
  124.             }
  125.         }
  126.     }
  127.  
  128.     return 1;
  129. }
  130.  
  131. #ifndef __PROGTEST__
  132. int main ( int argc, char * argv [] )
  133. {
  134.     long long int cnt;
  135.  
  136.     assert ( CountFriday13 ( 1900,  1,  1,
  137.                              2015,  1,  1, &cnt ) == 1
  138.              && cnt == 197LL );
  139.     assert ( CountFriday13 ( 1900,  1,  1,
  140.                              2015,  2,  1, &cnt ) == 1
  141.              && cnt == 197LL );
  142.     assert ( CountFriday13 ( 1900,  1,  1,
  143.                              2015,  1, 13, &cnt ) == 1
  144.              && cnt == 197LL );
  145.     assert ( CountFriday13 ( 1900,  1,  1,
  146.                              2015,  2, 13, &cnt ) == 1
  147.              && cnt == 198LL );
  148.     assert ( CountFriday13 ( 1904,  1,  1,
  149.                              2015,  1,  1, &cnt ) == 1
  150.              && cnt == 189LL );
  151.     assert ( CountFriday13 ( 1904,  1,  1,
  152.                              2015,  2,  1, &cnt ) == 1
  153.              && cnt == 189LL );
  154.     assert ( CountFriday13 ( 1904,  1,  1,
  155.                              2015,  1, 13, &cnt ) == 1
  156.              && cnt == 189LL );
  157.     assert ( CountFriday13 ( 1904,  1,  1,
  158.                              2015,  2, 13, &cnt ) == 1
  159.              && cnt == 190LL );
  160.     assert ( CountFriday13 ( 1905,  2, 13,
  161.                              2015,  1,  1, &cnt ) == 1
  162.              && cnt == 187LL );
  163.     assert ( CountFriday13 ( 1905,  2, 13,
  164.                              2015,  2,  1, &cnt ) == 1
  165.              && cnt == 187LL );
  166.     assert ( CountFriday13 ( 1905,  2, 13,
  167.                              2015,  1, 13, &cnt ) == 1
  168.              && cnt == 187LL );
  169.     assert ( CountFriday13 ( 1905,  2, 13,
  170.                              2015,  2, 13, &cnt ) == 1
  171.              && cnt == 188LL );
  172.     assert ( CountFriday13 ( 1905,  1, 13,
  173.                              2015,  1,  1, &cnt ) == 1
  174.              && cnt == 188LL );
  175.     assert ( CountFriday13 ( 1905,  1, 13,
  176.                              2015,  2,  1, &cnt ) == 1
  177.              && cnt == 188LL );
  178.     assert ( CountFriday13 ( 1905,  1, 13,
  179.                              2015,  1, 13, &cnt ) == 1
  180.              && cnt == 188LL );
  181.     assert ( CountFriday13 ( 1905,  1, 13,
  182.                              2015,  2, 13, &cnt ) == 1
  183.              && cnt == 189LL );
  184.     assert ( CountFriday13 ( 2015, 11,  1,
  185.                              2015, 10,  1, &cnt ) == 0 );
  186.     assert ( CountFriday13 ( 2015, 10, 32,
  187.                              2015, 11, 10, &cnt ) == 0 );
  188.     assert ( CountFriday13 ( 2090,  2, 29,
  189.                              2090,  2, 29, &cnt ) == 0 );
  190.     assert ( CountFriday13 ( 2096,  2, 29,
  191.                              2096,  2, 29, &cnt ) == 1
  192.              && cnt == 0LL );
  193.     assert ( CountFriday13 ( 2100,  2, 29,
  194.                              2100,  2, 29, &cnt ) == 0 );
  195.     assert ( CountFriday13 ( 2000,  2, 29,
  196.                              2000,  2, 29, &cnt ) == 1
  197.              && cnt == 0LL );
  198.     return 0;
  199. }
  200. #endif /* __PROGTEST__ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement