Coding/CPP 삽질기2014. 10. 27. 00:56

#include <stdio.h>


#define UP 0

#define RIGHT 1

#define DOWN 2

#define LEFT 3


#define MIRROR_1 1

#define MIRROR_2 2


#define SIZE 5


int mirror[SIZE][SIZE] = {

    { 0, 0, 0, 0, 2},

    { 0, 1, 0, 2, 0},

    { 0, 2, 0, 0, 1},

    { 0, 0, 0, 1, 0},

    { 0, 0, 0, 0, 1},    

};


int process(int x, int y, int direction);


int main (int argc, char **argv) {

    

    int count = process(0, 0, RIGHT);

       

    printf ("%d\n", count);

    

    return 0;

}



int process(int x, int y, int direction) {

    int count = 0;

    int isEnd = 0;

    

    while ( isEnd == 0 && x >= 0 && x < SIZE && y >= 0 && y < SIZE) {

        switch(direction) {

            case UP:

                 while (y >= 0 && mirror[y][x] == 0) {

                     y--;

                 }

                 // printf ("Up %d %d\n", x, y);                   

                 break;

            case RIGHT:

                 while (x < SIZE && mirror[y][x] == 0) {

                     x++;

                 }

                 // printf ("Right %d %d\n", x, y);

                 break;

            case DOWN:

                 while (y < SIZE && mirror[y][x] == 0) {

                     y++;

                 }

                 // printf ("Down %d %d\n", x, y);

                 break;

            case LEFT:

                 while (x >= 0 && mirror[y][x] == 0) {

                     x--;

                 }

                 // printf ("LEFT %d %d\n", x, y);

                 break;

            default:

                 break;

        }

        

        if ( x >= 0 && x < SIZE && y >= 0 && y < SIZE ){

            if ( mirror[y][x] == MIRROR_1 ) {

               direction ^= 1;      

               count++;              

            } else if ( mirror[y][x] == MIRROR_2 ) {

               direction ^= 3;

               count++;

            }

                  

            switch (direction) {

                case UP : y--; break;

                case RIGHT : x++; break;

                case LEFT : x--; break;

                case DOWN : y++; break;

            }

            //printf ("%d %d %d\n",direction, x, y);

        } else {

            isEnd = 1;    

        }              

    };

    

    return count;

}


'Coding > CPP 삽질기' 카테고리의 다른 글

사각형 개수 구하기  (0) 2014.11.12
대칭행렬  (0) 2014.10.24
Programming 실습 예제  (0) 2014.03.28
Posted by chobocho
Coding/CPP 삽질기2014. 10. 24. 01:49

#include <iostream>

using namespace std;


int main (int argc, char **argv) {

    

    int i = 0;

    int j = 0;

    

    for ( i = 0; i < 7; i++ ) {

        for ( j = 0; j < 7; j++ ) {

            cout << int(i^j) << " ";

        }

        cout << endl;

    }

    

    return 0;

}



Hi-iMac:CPP chobocho$ ./matrix.o 

0 1 2 3 4 5 6 

1 0 3 2 5 4 7 

2 3 0 1 6 7 4 

3 2 1 0 7 6 5 

4 5 6 7 0 1 2 

5 4 7 6 1 0 3 

6 7 4 5 2 3 0 

'Coding > CPP 삽질기' 카테고리의 다른 글

Mirror  (0) 2014.10.27
Programming 실습 예제  (0) 2014.03.28
피보나치 수열  (0) 2013.06.06
Posted by chobocho
Coding/CPP 삽질기2014. 3. 28. 04:02

 Programming 실습 예제


[ 기초 ]

1. Array 기반의 스택

2. Array 기반의 큐

3. Array 기반의 환형 큐

4. Double Linked List

5. Linked list를 이용한 스택

6. Linked list를 이용한 큐

7. Array 한 개로, 스택과 큐를 동시에 쓸수 있게 하기


'Coding > CPP 삽질기' 카테고리의 다른 글

대칭행렬  (0) 2014.10.24
피보나치 수열  (0) 2013.06.06
함수 실행 시간 측정  (0) 2013.05.24
Posted by chobocho
Coding/Java 삽질기2014. 1. 3. 02:26

* 맥에서 이클립스 개발환경을 만드는 제일 간단한 방법


1) 아래 사이트에서 안드로이드 개발툴을 다운 받아서 설치한다.

http://developer.android.com/sdk/index.html

2) 이클립스를 실행하면 자동으로 자바를 다운 받는다.

3) 자바 설치 후 이클립스 실행



Next-> http://chobocho.tistory.com/2461355

'Coding > Java 삽질기' 카테고리의 다른 글

Tetris  (0) 2016.03.17
초간단 보이스 레코더  (0) 2013.03.08
ChoboSMS  (0) 2013.02.26
Posted by chobocho
Coding/Python 삽질기2013. 12. 15. 04:02

Cython 설치 ( Mac )

  1. python download ( http://www.cython.org )
  2. sudo python setup.py install
Cython 실습

factorial_100000.pyx ]

def Factorial(int N):
    cdef int checkSize = 5000
    cdef int fi
    cdef int endN
    endN = N / checkSize
    facResult = 1
    for fi in range(1, endN + 1):
        facResult = facResult * reduce(lambda x, y : x * y, range((fi-1)*checkSize+1,fi * checkSize+1))
   
    if ( (endN * checkSize + 1) != (N+1) ):   
        facResult = facResult * reduce(lambda x, y : x * y, range(endN * checkSize + 1, N+1))  
    return facResult


[ setup.py ]

from distutils.core import setup

from Cython.Build import cythonize

setup(
  name = 'Factorial app',
  ext_modules = cythonize("factorial_100000.pyx"),


[ Build ]
python setup.py build_ext —inplace

# 이러면 SO 파일이 생김

[ Sandbox.py ]

import time

from factorial_100000 import Factorial

startTime = time.time()
factorialN = Factorial(10000)

print "runtime is %s"%(time.time() - startTime) 


[ 실행결과 ]



'Coding > Python 삽질기' 카테고리의 다른 글

[Notepad++] Python 실행하기  (0) 2016.08.28
100000! ( 100000 팩토리얼 ) & 1000000! ( 1000000 팩토리얼 )  (0) 2013.06.22
fibonacci 수열  (0) 2013.06.20
Posted by chobocho
Coding/Python 삽질기2013. 6. 22. 04:48

import sys

import time


def Factorial(N):

    checkSize = 5000

    endN = N / checkSize

    facResult = 1

    for fi in range(1, endN + 1):

        facResult = facResult * reduce(lambda x, y : x * y, range((fi-1)*checkSize+1,fi * checkSize+1))

    

    if ( (endN * checkSize + 1) != (N+1) ):    

        facResult = facResult * reduce(lambda x, y : x * y, range(endN * checkSize + 1, N+1))   

    return facResult



number = 100000

startTime = time.time()

factorialN = Factorial(number)


print "runtime is %s"%(time.time() - startTime) 

#print "Factorial %d is %d"%(number, factorialN)


인터넷을 검색 중 100000! 계산에 관한 글을 보고 실습해 본 코드.

2008년 구매한 노트북에서도 3.2초면 계산이 끝난다. ( * 결과 출력 할 때는 "젠틀맨" 뮤비 한 편 보고 나면 된다. )

math.factorial() 함수를 사용하는 것보다, 분할해서 계산하는게 3배 이상 빠르다.



* 참고 : 100000! 관련 KLDP 링크


http://kldp.org/node/129900

http://kldp.org/node/129379

http://kldp.org/node/129116

http://kldp.org/node/129044

http://kldp.org/node/107470



100000! 결과 : 100000.txt


심심해서 추가로 돌려본 1000000! ( 백만 팩토리얼 )

계산은 5분 만에 끝났으나, 출력이 한 시간도 넘게 걸린 듯...


1000000! 결과 : 1000000!.zip





'Coding > Python 삽질기' 카테고리의 다른 글

Cython 설치  (4) 2013.12.15
fibonacci 수열  (0) 2013.06.20
KMP algorithm  (0) 2013.05.06
Posted by chobocho
Coding/Python 삽질기2013. 6. 20. 01:06

tempResult = [0, 1, 1];


def fibonacci (number):

   if ( number == 1 or number == 2):

      return 1

   if ( tempResult[number] == -1 ): 

      result = fibonacci ( number-1 ) + fibonacci ( number-2 )

      tempResult[number] = result

   

   return tempResult[number]

   

      

for idx in range(3,1001):

    tempResult.insert(idx, -1)   

      

for idx in range(1,1001):

    print idx," : ",fibonacci(idx)


위와 같이 사용하면, 재귀일지라도 매우 빠른 속도로 1부터 1000까지의 피보나치 수열의 값을 구할 수 있다.

더 빠르게 계산하는 방법은 아래와 같다.  


def fibonacci2 (number):

   if ( number == 1 or number == 2):

      return 1

   

   previous =  1

   result   =  1

   sum      =  0

   

   for i in range(3, number+1):

       sum = result + previous

       previous = result

       result = sum

       print i," : ",result

       

   return result

 

fibonacci2(1000)    




1-1000까지 피보나치 수열의 값 : finbonacci_1000.txt


Posted by chobocho
Coding/CPP 삽질기2013. 6. 6. 22:46

동적알고리즘을 이용한 피보나치 수열 


int arrFibonacci[100000] = { 0, };


int fibonacci ( int number ) {

    int ret = 0;

    

    if ( number <= 0 ) {

        ret = -1;

    }

    else if ( number == 1 || number == 2  ) {

          ret = 1;    

    } else {

    if ( arrFibonacci[number] == 0 ) { 

            arrFibonacci[number] = fibonacci(number-1) + fibonacci(number-2);  

         }

         ret = arrFibonacci[number];

    }

    

    return ret;

}


'Coding > CPP 삽질기' 카테고리의 다른 글

Programming 실습 예제  (0) 2014.03.28
함수 실행 시간 측정  (0) 2013.05.24
fall-through  (0) 2013.05.13
Posted by chobocho
Coding/CPP 삽질기2013. 5. 24. 01:13

#include <ctime>

using namespace std;

void ellaspedTimeTest() { clock_t start_time; clock_t end_time; start_time = clock();


//Do something ...

end_time = clock(); ellasped_time = (double)(end_time - start_time)/CLOCKS_PER_SEC;

}

'Coding > CPP 삽질기' 카테고리의 다른 글

피보나치 수열  (0) 2013.06.06
fall-through  (0) 2013.05.13
Programming quiz  (0) 2012.10.25
Posted by chobocho
Coding/Math2013. 5. 19. 22:51




[ 참고 사이트 ]

http://ko.wikipedia.org/wiki/%EC%98%A4%EC%9D%BC%EB%9F%AC%EC%9D%98_%EB%93%B1%EC%8B%9D

http://navercast.naver.com/contents.nhn?rid=22&contents_id=2420


'Coding > Math' 카테고리의 다른 글

23000000000000000 ( 2경 3천조 )  (0) 2013.01.31
홀수의 합으로 제곱근 구하기  (0) 2012.06.01
[Math] 사선 공식  (0) 2011.09.12
Posted by chobocho