"BOJ 4673 셀프 넘버"의 두 판 사이의 차이

잔글 (봇: 자동으로 텍스트 교체 (-</source> +</syntaxhighlight>, -<source +<syntaxhighlight ))
11번째 줄: 11번째 줄:
==Bash==
==Bash==
동작은 하지만 성능 문제가 있음.
동작은 하지만 성능 문제가 있음.
<source lang='bash'>
<syntaxhighlight lang='bash'>
LANG=C
LANG=C
SELF_NUMBERS=""
SELF_NUMBERS=""
24번째 줄: 24번째 줄:
         SELF_NUMBERS="${SELF_NUMBERS} ${i} "
         SELF_NUMBERS="${SELF_NUMBERS} ${i} "
done
done
</source>
</syntaxhighlight>


==C++==
==C++==
<source lang='cpp'>
<syntaxhighlight lang='cpp'>
#include <iostream>
#include <iostream>
using namespace std;
using namespace std;
47번째 줄: 47번째 줄:
     }
     }
}
}
</source>
</syntaxhighlight>
<source lang='cpp'>
<syntaxhighlight lang='cpp'>
#include <iostream>
#include <iostream>
using namespace std;
using namespace std;
70번째 줄: 70번째 줄:
     }
     }
}
}
</source>
</syntaxhighlight>


==Java==
==Java==
<source lang='Java'>
<syntaxhighlight lang='Java'>
public class Main {
public class Main {
     private static final int SIZE = 10000;
     private static final int SIZE = 10000;
94번째 줄: 94번째 줄:
     }
     }
}
}
</source>
</syntaxhighlight>


==Perl==
==Perl==
<source lang='perl'>
<syntaxhighlight lang='perl'>
use List::Util qw(sum);
use List::Util qw(sum);
@self_numbers = ();
@self_numbers = ();
105번째 줄: 105번째 줄:
     $self_numbers[$v]=1;
     $self_numbers[$v]=1;
}
}
</source>
</syntaxhighlight>


==PHP==
==PHP==
<source lang='PHP'>
<syntaxhighlight lang='PHP'>
<?php
<?php
function d($n) { return $n + array_sum(str_split($n)); }
function d($n) { return $n + array_sum(str_split($n)); }
116번째 줄: 116번째 줄:
     $self_numbers[d($i)] = 1;
     $self_numbers[d($i)] = 1;
}
}
</source>
</syntaxhighlight>


==Python==
==Python==
<source lang='Python'>
<syntaxhighlight lang='Python'>
def d(n):
def d(n):
     return n + sum(map(int,list(str(n))))
     return n + sum(map(int,list(str(n))))
127번째 줄: 127번째 줄:
         print( i )
         print( i )
     self_numbers[d(i)] = 1
     self_numbers[d(i)] = 1
</source>
</syntaxhighlight>


==같이 보기==
==같이 보기==

2021년 7월 18일 (일) 05:46 판

1 개요

BOJ 4673 셀프 넘버

[[분류:BOJ {{{단계}}}단계]]

  • 자연수 n에 대해 d(n)의 값을 구하는 함수를 정의해 문제를 해결해봅니다
  • 알고리즘 분류: 에라토스테네스의 체, 입문용
BOJ 단계별로 풀어보기
순번 문제 풀이

틀:BOJ 5단계 틀:BOJ 단계 푸터

2 Bash

동작은 하지만 성능 문제가 있음.

LANG=C
SELF_NUMBERS=""

for i in `seq 1 10000`
do
        if [ ! `echo "$SELF_NUMBERS" | grep -cP " ${i} "` -ne 0 ]; then
                echo $i
        fi
        k=`echo $i | perl -pe 's/(\d)/$1 + /g'`
        i=`expr $i + ${k}0`
        SELF_NUMBERS="${SELF_NUMBERS} ${i} "
done

3 C++

#include <iostream>
using namespace std;
#define SIZE 10000
int main() {
    int d, temp;
    bool a[SIZE+1]={};
    for(int i=1;i<=SIZE;i++) {
    	temp=i;
    	d=i;
        while(temp){
    	    d += temp%10;
    	    temp /= 10;
    	}
    	if(d<=SIZE) a[d]=true;
    }
    for(int i=1;i<=SIZE;i++) {
    	if( !a[i] ) printf("%d\n", i);
    }
}
#include <iostream>
using namespace std;
#define SIZE 10000
bool a[SIZE+1]={};
int d(int n) {
	int temp = n;
	int res = n;
    while(temp){
	    res += temp%10;
	    temp /= 10;
	}
	return res;
}
int main() {
    int temp;
    for(int i=1;i<=SIZE;i++) {
    	if( !a[i] ) printf("%d\n", i);
    	temp = d(i);
    	if( temp<=SIZE ) a[temp] = true;
    }
}

4 Java

public class Main {
    private static final int SIZE = 10000;
    private static boolean[] flags = new boolean[SIZE+1];
    private static int d(int n) {
        int result = n;
        char[] digits = String.valueOf(n).toCharArray();
        for( int i=0; i<digits.length; i++ ) {
            result += digits[i] - '0';
        }
        return result;
    }
    public static void main(String args[]) {
        int temp;
        for(int i=1; i<=SIZE; i++) {
            if( !flags[i] ) System.out.println(i);
            temp = d(i);
            if( temp <= SIZE ) flags[temp] = true;
        }
    }
}

5 Perl

use List::Util qw(sum);
@self_numbers = ();
for $i (1..10000) {
    print "$i\n" unless ($self_numbers[$i]);
    $v=$i + sum(split //, $i);
    $self_numbers[$v]=1;
}

6 PHP

<?php
function d($n) { return $n + array_sum(str_split($n)); }
$self_numbers = [];
for($i=1; $i<=10000; $i++) {
    if( !array_key_exists($i,$self_numbers) ) echo $i . "\n";
    $self_numbers[d($i)] = 1;
}

7 Python

def d(n):
    return n + sum(map(int,list(str(n))))
self_numbers = {}
for i in range(1,10001):
    if i not in self_numbers:
        print( i )
    self_numbers[d(i)] = 1

8 같이 보기

9 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}