중첩된 for문 재귀함수로 해결하는 방법

wednesday의 이미지

중첩된 for문을 재귀함수로 구현하려고 합니다. (전체 원소 갯수와 for문의 depth를 입력받아
출력하는 프로그램 입니다)
현재까지 구현한 소스는 다음과 같습니다.

 
/*
출력: 3x2                    3x3
(0,0) (0,1) (0,2)   (0,0,0) (0,0,1) (0,0,2)
(1,0) (1,1) (1,2)   (0,1,0) (0,1,1) (0,1,2)
(2,0) (2,1) (2,2)   (0,2,0) (0,2,1) (0,2,2)
                    (1,0,0) (1,0,1) (1,0,2)
                    (1,1,0) (1,1,1) (1,1,2)
                    (1,2,0) (1,2,1) (1,2,2)
                    (2,0,0) (2,0,1) (2,0,2)
                    (2,1,0) (2,1,1) (2,1,2)
                    (2,2,0) (2,2,1) (2,2,2)
*/
 
#define SIZE 3
#define SEL 3
 
void bufprint(int cnt, int *pp)
{
    printf("%d ",cnt);
}
void superposition(int size, int *p, int sel)
{
    int i, tmp=0;
    for(i=0; i<SIZE; i++){
        if(sel==1) bufprint(i, p);
        else {
            superposition(size, p, sel-1);
        }
    }
    printf("\n");
}
int main()
{
    int i, buf[SIZE];
    for(i=0; i<SIZE; i++) buf[i]=i;
 
    superposition(SIZE, buf, SEL);
    return 0;
}

출력부 함수를 어떻게 구현해야 될까요? 도움 부탁드립니다 ㅠㅠ

shint의 이미지

코드는 엉터리 입니다. ㅇ_ㅇ;;

http://codepad.org/ox9rCH5P

#define SIZE 3
#define SEL 3
 
 
void bufprint(int cnt, int *pp)
{
    printf("[ %d] ",cnt);
}
void bufprinta(int cnt, int *pp)
{
    printf("a[ %d] ",cnt);
}
void bufprintb(int cnt, int *pp)
{
    printf("b[ %d] ",cnt);
}
void bufprintc(int cnt, int *pp)
{
    printf("c[ %d] ",cnt);
}
void superposition(int size, int *p, int sel)
{
    int i, tmp=0;
    for(i=0; i < SIZE; i++)
    {
        //세로
          if(sel == 2)
        {
            bufprinta(i, p);
        }
 
        if(sel == 1 && i%sel == 0)
        {
            bufprintc(i, p);
        }
 
        if(sel == 1 && i%2 == 0)
        {
            bufprintb(i, p);
        }
 
        if(sel==1)
        {
          //가로
            bufprint(i, p);
        }
        else
        {
            superposition(size, p, sel-1);
        }
    }
    printf("\n");
}
 
int main()
{
    int i, buf[SIZE];
    for(i=0; i < SIZE; i++) buf[i]=i;
 
    superposition(SIZE, buf, SEL);
    return 0;
}

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

shint의 이미지

http://codepad.org/snniENVz

#define SIZE 3
#define SEL 3
 
 
void bufprint(int cnt, int *pp)
{
    printf(" [ %d] ",cnt);
}
void bufprint_sero(int cnt, int *pp)
{
    printf("세로[ %d] - ",cnt);
}
void bufprint_x(int cnt, int *pp)
{
    printf("x[ %d] ",cnt);
}
void bufprint_y(int cnt, int *pp)
{
    printf("y[ %d]   ",cnt);
}
 
void superposition(int size, int *p, int sel)
{
    int i, tmp=0;
    for(i=0; i < SIZE; i++)
    {
 
        if(sel == 1 && i%2 == 0)
        {
            //bufprint(i, p);
        }
 
 
        //세로
                if(sel == 2)
        {
            p[0]=i;
            bufprint_sero(i, p);
        }
 
 
        if(sel==1)
        {
            //가로
                        bufprint_x(p[0], p);    bufprint_y(i, p);
        }
        else
        {
            superposition(size, p, sel-1);
        }
    }
    printf("\n");
}
 
int main()
{
    int i, buf[SIZE];
    for(i=0; i < SIZE; i++) buf[i]=i;
 
    superposition(SIZE, buf, SEL);
    return 0;
}
 
 
세로[0] - x[0] y[0]   x[0] y[1]   x[0] y[2]   
세로[1] - x[1] y[0]   x[1] y[1]   x[1] y[2]   
세로[2] - x[2] y[0]   x[2] y[1]   x[2] y[2]   
 
세로[0] - x[0] y[0]   x[0] y[1]   x[0] y[2]   
세로[1] - x[1] y[0]   x[1] y[1]   x[1] y[2]   
세로[2] - x[2] y[0]   x[2] y[1]   x[2] y[2]   
 
세로[0] - x[0] y[0]   x[0] y[1]   x[0] y[2]   
세로[1] - x[1] y[0]   x[1] y[1]   x[1] y[2]   
세로[2] - x[2] y[0]   x[2] y[1]   x[2] y[2]   

----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.

매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.

각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com

익명 사용자의 이미지

숙제는 자기 힘으로,
질문은 학교에서.

ㅇ의 이미지

어휴