어느쪽 코드가 더 좋다고 할수있을까요?
글쓴이: gurumong / 작성시간: 월, 2007/06/18 - 10:02오후
어느쪽 코드가 더 좋다고 할수있을까요?
미리 정해진 숫자 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; }
Forums:
두번째 code 에 한표.
반복문 중첩시키는게 싫으시면 재귀를 쓰심이 좋겠삼.
int main() -> int main(void)
LENGHT -> LENGTH
첫번째 code 에서 k 는 사용되지 않았군요.
성능이 중요하면
성능이 중요하면 추가 optimization이 필요하겠지만 일단은 가독성을 최우선으로 해서 코드를 짜보았습니다. 도움이 되었으면 좋겠네요.
goto의 단점에 속하는
goto의 단점에 속하는 경우가 아니라면 goto를 쓰는 것도 좋습니다.
이 경우는 goto를 사용하는게 가독성이 더 좋으니 사용하는게 더 좋습니다.
껄끄럽게 생각하지 마세요.
헉~ 잘못 붙었군요. :(
헉~ 잘못 붙었군요. :(
첫번째 분 말대로
첫번째 분 말대로 recursive하게 짜는 게
코딩 자체는 편하고 좋습니다. ;;
( 속도 문제는 뭐 차치하고요. -_-a )
Haskell로 짜면
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,6,8,7],[2,4,3,7,6,8],[2,4,3,7,8,6],
[2,4,3,8,6,7],[2,4,3,8,7,6],[2,4,6,3,7,8],[2,4,6,3,8,7],[2,4,6,7,3,8],[2,4,6,7,8,3],[2,4,6,8,3,7],
[2,4,6,8,7,3],[2,4,7,3,6,8],[2,4,7,3,8,6],[2,4,7,6,3,8],[2,4,7,6,8,3],[2,4,7,8,3,6],[2,4,7,8,6,3],
[2,4,8,3,6,7],[2,4,8,3,7,6],[2,4,8,6,3,7],[2,4,8,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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
http://mentalese.net
http://functional.or.kr
openlook 에서 Python 으로 한 실험.
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)
댓글 달기