중첩된 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

익명 사용자의 이미지

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

ㅇ의 이미지

어휴

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.