티스토리 뷰


#include <stdio.h>
#define MAX 9999

int ddd[] = {31,28,31,30,31,30,31,31,30,31,30,31};
   
int is_leap_year(int y)
{
    return ( y % 4 == 0 )&& (( y % 100 != 0) || (y % 400 == 0) );
}
int hash(char * str)
{
    int val = 0;
    char *ptr;
    ptr = str;
   
    while(*ptr != '\0')
    {
               int tmp;
               val = (val << 4) + *ptr;
               if(tmp == (val & 0xf0000000))
               {
                      val = val ^ (tmp >> 24);
                      val = val^tmp;
               }
               ptr++;         
    }
    return val % MAX;
}

void getAdd(char *buff, int y, int m , int d, int i)
{
    char buf[9];
    memset(buf, 0x00, sizeof(buf));
    d += i;
    while(d > ddd[m - 1])
    {
            if(m == 2 && is_leap_year(y))
                 d -= 29;
            else
                 d -= ddd[m - 1];
            m++;
           
            if(m > 12)
            {
                 m -= 12;
                 y++;
            }
    }
    sprintf(buff, "%04d%02d%02d", y, m, d);
}

main()
{
      int yyyy,mm,dd;
      int i, ErrCnt = 0;
      char buff[9];
      int hash_t[MAX];
      memset(hash_t, 0x00, sizeof(hash_t));
      memset(buff, 0x00, sizeof(buff));
      yyyy = 2007;
      mm = 1;
      dd = 1;
     
      for(i = 0; i < 800; i++){
            getAdd(buff, 2007, 10, 29, i);
            if(hash_t[hash(buff)])
            {
                                  ErrCnt++;
            }
            hash_t[hash(buff)] = 1;
            //printf("hash[%d][%s]: %d\n", i, buff , hash(buff));
      }
      printf("ErrCount: %d\n", ErrCnt);
}


           

'Study > Language &gt;' 카테고리의 다른 글

전처리기  (0) 2010.04.30
__FILE__ , __LINE__ 의 활용  (0) 2010.04.30
getAddDay 함수 구현  (0) 2010.01.14
VI 명령어 총집합  (0) 2010.01.12
GCC의 -l 과 -L 옵션  (0) 2010.01.09
댓글