알고리즘 질문 - 0~4까지 중복없이 찍어보자

koku_ma의 이미지

0 1 2 3 4 이렇게 5개의 숫자가 있습니다.

이걸 아래의 형식과 같이 중복없이 전부 찍어보고 싶은데 잘 안되네요...

아래와 같은 형식으로 찍을 수 있는 알고리즘을 좀 알려주세요.

0
1
2
3
4

01
02
03
04
05
10
11
12
13
14
20
21
22
23
24
30
31
32
33
34
40
41
42
43
44

001
002
003
004
005
010
011
012
013
014
020
021
022
023
024
030
031
032
033
034
040
041
042
043
044
101
102
103
104

이런식으로 해서

끝은

44402
44403
44404
44405
44410
44411
44412
44413
44414
44420
44421
44422
44423
44424
44430
44431
44432
44433
44434
44440
44441
44442
44443
44444

이런식으로 ....

고수님들 부탁드립니다.

rein의 이미지

속도가 중요하지 않다면(...)

단순히 저걸 5진법으로 보고, 0~44444(=십진법 3124)까지 변수하나를 증가시키면서
5진법으로 변환하고 찍어주면 되지 않을까요

jiee의 이미지

BackTracking을 사용하면 다음과 같이 짜볼 수 있네요.

public class PrintNums {
	char[] numA = {'0', '1', '2', '3', '4'};
	char[] memA = new char[numA.length+1];
 
	public void print(int totalDigits) {
		BackTracking(0, totalDigits);
	}
	private void BackTracking(int ix, int depth) {
		if (ix == depth) {
			for (int i=1; i<=ix; i++)
				System.out.print(memA[i]);
			System.out.println();
		} else {
			for (int j=0; j < numA.length; j++) {
				memA[ix+1] = numA[j];
				BackTracking(ix+1, depth);
			}
		}
	}
}

위에서 totalDigits수를 변경하면 원하시는 자릿수가 나옵니다.
하지만, 이 방법은 자릿수가 커지면 무척 느려지죠..-_-a
(다른 분이 더 우아한 방법을 알려주실거라..)

ps. 안타깝게 잘 쓰지도 못하는 자바로 짤 수 밖에 없는 환경에서...

토나오게...

시지프스의 이미지

루프가 이중이라 별로 효율적이지는 않지만, 이런 방법도 있어요.

void print(const int width)
{
	const char num[] = {'0','1','2','3','4'};
	const int size=sizeof(num);
	char a[width+1];
	memset(a,'0',width);
	a[width]='\0';
 
	while (1) {
		int i;
		printf("%s\n",a);
		for (i=width-1; i>=0; i--) {
			(a[i])++;
			if (a[i] == (num[size-1]+1))
				a[i] = '0';
			else
				break;
		}
		if (i<0)
			break;
	}
}

begin{signature}
THIS IS SPARTA!!!!!n.
end{signature}

댓글 달기

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