어느쪽 코드가 더 좋다고 할수있을까요?

gurumong의 이미지

어느쪽 코드가 더 좋다고 할수있을까요?
미리 정해진 숫자 6개를(2,3,4,6,7,8)사용해서
6자리의 숫자를 모두 출력하는 문제가 있는데요
총 6^6번을 출력하게되는데

이 문제의 풀이로 두개의 코드가 있거든요
어느쪽 코드가 더 좋다고 할수있을까요?

아무래도 6개의 루프문을 사용한 두번째 코드가
구현도 쉽고 빠르기도하겠지만 굉장히 무식하다는 생각이 들구요
첫번째 코드는 복잡성이 증가하고
중첩루프를 탈출하기위해 goto문을 사용하려다가
다르게 한것이 편법!?을 쓴거 같아서 좀 껄끄럽기도하고;;
하지만 루프문을 최소한으로 사용했고 나중을 위해 정해진 숫자의 갯수와 자리수를 쉽게 바꿀수있는데
조언 좀 부탁드립니다 ^^;

#include <stdio.h>
#define BASE 6
#define LENGHT 6
 
int main()
{
    int num[] = {2, 3, 4, 6, 7, 8};
    int box[LENGHT]= {0, };
    int i, j, k, goon;
 
    goon = 1;
    while(goon) {
        for(i = 0; i < LENGHT; i++) printf("%d", num[box[i]]);
        printf(" ");
 
        j = 1;
        while( 0 < ++box[LENGHT - j] / BASE) {
            box[LENGHT - j] = 0;
            j++;
            if(j > LENGHT) {
                goon = 0;
                break;
            }
        }
    }
 
    getc(stdin);getc(stdin);
    return 0;
}

#include <stdio.h>
 
int main()
{
    int num[] = {2, 3, 4, 6, 7, 8};
    int i,j,k,l,m,n;
 
    for(i=0; i<6; i++) {
     for(j=0; j<6; j++) {
      for(k=0; k<6; k++) {
       for(l=0; l<6; l++) {
        for(m=0; m<6; m++) {
         for(n=0; n<6; n++) {
          printf("%d %d %d %d %d %d ", num[i], num[j], num[k], num[l], num[m], num[n]);
         }
        }
       }
      }
     }
    }
 
    getc(stdin);getc(stdin);
    return 0;
}
winner의 이미지

반복문 중첩시키는게 싫으시면 재귀를 쓰심이 좋겠삼.
int main() -> int main(void)
LENGHT -> LENGTH
첫번째 code 에서 k 는 사용되지 않았군요.

clhitter의 이미지

성능이 중요하면 추가 optimization이 필요하겠지만 일단은 가독성을 최우선으로 해서 코드를 짜보았습니다. 도움이 되었으면 좋겠네요.

#include <stdio.h>
#include <math.h>
 
#define BASE 6
#define LENGTH 6
 
int main()
{
  int num[BASE] = { 2, 3, 4, 6, 7, 8 };
  int i, exp, digit;
 
  for (i = 0; i < (int)pow(BASE, LENGTH); ++i)
  {
    for (exp = LENGTH - 1; exp >= 0; --exp)
    {
      digit = i/((int)pow(BASE, exp))%BASE;
      printf("%d ", num[digit]);
    }
    printf("\n");
  }
 
  return 0;
}

익명 사용자의 이미지

goto의 단점에 속하는 경우가 아니라면 goto를 쓰는 것도 좋습니다.
이 경우는 goto를 사용하는게 가독성이 더 좋으니 사용하는게 더 좋습니다.

껄끄럽게 생각하지 마세요.

익명 사용자의 이미지

헉~ 잘못 붙었군요. :(

vacancy의 이미지


첫번째 분 말대로 recursive하게 짜는 게

코딩 자체는 편하고 좋습니다. ;;

( 속도 문제는 뭐 차치하고요. -_-a )

semmal의 이미지

select [] _ = []
select (n:ns) [] = [n]:select ns []
select (n:ns) ss' = [ n:s' | s' <- ss', not (elem n s')] ++ select ns ss'

selects :: Eq a => [[a] -> [[a]] -> [[a]]]
selects = select:selects

selectAll ns = foldr (\x y -> x ns y) [] (take (length ns) selects)

실행 : > selectAll [2,3,4,6,7,8]

[[2,3,4,6,7,8],[2,3,4,6,8,7],[2,3,4,7,6,8],[2,3,4,7,8,6],[2,3,4,8,6,7],[2,3,4,8,7,6],[2,3,6,4,7,8],
[2,3,6,4,8,7],[2,3,6,7,4,8],[2,3,6,7,8,4],[2,3,6,8,4,7],[2,3,6,8,7,4],[2,3,7,4,6,8],[2,3,7,4,8,6],
[2,3,7,6,4,8],[2,3,7,6,8,4],[2,3,7,8,4,6],[2,3,7,8,6,4],[2,3,8,4,6,7],[2,3,8,4,7,6],[2,3,8,6,4,7],
[2,3,8,6,7,4],[2,3,8,7,4,6],[2,3,8,7,6,4],[2,4,3,6,7,8],[2,4,3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,7],[8,4,2,3,7,6],[8,4,2,6,3,7],
[8,4,2,6,7,3],[8,4,2,7,3,6],[8,4,2,7,6,3],[8,4,3,2,6,7],[8,4,3,2,7,6],[8,4,3,6,2,7],[8,4,3,6,7,2],
[8,4,3,7,2,6],[8,4,3,7,6,2],[8,4,6,2,3,7],[8,4,6,2,7,3],[8,4,6,3,2,7],[8,4,6,3,7,2],[8,4,6,7,2,3],
[8,4,6,7,3,2],[8,4,7,2,3,6],[8,4,7,2,6,3],[8,4,7,3,2,6],[8,4,7,3,6,2],[8,4,7,6,2,3],[8,4,7,6,3,2],
[8,6,2,3,4,7],[8,6,2,3,7,4],[8,6,2,4,3,7],[8,6,2,4,7,3],[8,6,2,7,3,4],[8,6,2,7,4,3],[8,6,3,2,4,7],
[8,6,3,2,7,4],[8,6,3,4,2,7],[8,6,3,4,7,2],[8,6,3,7,2,4],[8,6,3,7,4,2],[8,6,4,2,3,7],[8,6,4,2,7,3],
[8,6,4,3,2,7],[8,6,4,3,7,2],[8,6,4,7,2,3],[8,6,4,7,3,2],[8,6,7,2,3,4],[8,6,7,2,4,3],[8,6,7,3,2,4],
[8,6,7,3,4,2],[8,6,7,4,2,3],[8,6,7,4,3,2],[8,7,2,3,4,6],[8,7,2,3,6,4],[8,7,2,4,3,6],[8,7,2,4,6,3],
[8,7,2,6,3,4],[8,7,2,6,4,3],[8,7,3,2,4,6],[8,7,3,2,6,4],[8,7,3,4,2,6],[8,7,3,4,6,2],[8,7,3,6,2,4],
[8,7,3,6,4,2],[8,7,4,2,3,6],[8,7,4,2,6,3],[8,7,4,3,2,6],[8,7,4,3,6,2],[8,7,4,6,2,3],[8,7,4,6,3,2],
[8,7,6,2,3,4],[8,7,6,2,4,3],[8,7,6,3,2,4],[8,7,6,3,4,2],[8,7,6,4,2,3],[8,7,6,4,3,2]]

------------------------------
How many legs does a dog have?

쓴귤의 이미지

원문에 있는 것처럼 중복순열을 구하는 코드는 이거 한 줄로 끝입니다. ^^


sequence $ replicate 6 [2,3,4,6,7,8]

그리고 그냥 순열을 구하는 경우는 중복을 배제하면 됩니다.


filter ((==6).length.nub) $ sequence $ replicate 6 [2,3,4,6,7,8]

자세한 설명은 http://functional.or.kr/node/156 에 있습니다.


http://mentalese.net
http://functional.or.kr
winner의 이미지

http://openlook.org/blog/1160

꽤 재미있답니다. 읽어보세요.

익명 사용자의 이미지

def permutation(msg, start):
end = len(msg)
if start == end:
print msg
else:
for i in range(start, end):
msg[start], msg[i] = msg[i], msg[start]
permutation(msg, start+1)
msg[start], msg[i] = msg[i], msg[start]

if __name__ == '__main__':
msg = [2, 3, 4, 6, 7, 8]
permutation(msg, 0)

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.