Gidhub BE Developer

# [BOJ] 분해합

2018-03-26
goodGid

## Problem

Problem URL : 분해합

## [1] Answer Code (18. 03. 26)

``````
#include<iostream>
#include<algorithm>
using namespace std;

int arr[1000005];

int main(){
int n;
cin >> n;

int ans = 987654321;
for(int i=1; i<=n; i++){
arr[i] = i;

if( i < 10 ){
continue;
}else if( i < 100){
arr[i] = arr[i] + ( arr[i] / 10 % 10 );
}else if( i < 1000){
arr[i] = arr[i] + ( arr[i] / 100 % 10 ) + ( arr[i] / 10 % 10 ) + ( arr[i] % 10 );
}else if ( i < 10000){
arr[i] = arr[i] + ( arr[i] / 1000 % 10) +( arr[i] / 100 % 10 ) + ( arr[i] / 10 % 10 ) + ( arr[i] % 10 );
}else if ( i < 100000 ){
arr[i] = arr[i] + (arr[i] / 10000 % 10 ) + ( arr[i] / 1000 % 10) +( arr[i] / 100 % 10 ) + ( arr[i] / 10 % 10 ) + ( arr[i] % 10 );
}else if( i < 1000000){
arr[i] = arr[i] + (arr[i] / 100000 % 10) + (arr[i] / 10000 % 10 ) + ( arr[i] / 1000 % 10) +( arr[i] / 100 % 10 ) + ( arr[i] / 10 % 10 ) + ( arr[i] % 10 );
}else {
arr[i] = arr[i] + (arr[i] / 1000000 % 10) + (arr[i] / 100000 % 10) + (arr[i] / 10000 % 10 ) + ( arr[i] / 1000 % 10) +( arr[i] / 100 % 10 ) + ( arr[i] / 10 % 10 ) + ( arr[i] % 10 );
}

if( arr[i] == n){
ans = min(ans , i);
}
}

if( ans == 987654321)
cout << "0" << endl;
else
cout << ans << endl;
return 0;
}

``````

## [2] Answer Code (18. 03. 26)

``````

#include <iostream>
using namespace std;

int cal(int num) {
int sum = num;
while (num!=0) {
sum += num % 10;
num /= 10;
}
return sum;
}

int main() {
int n;
cin >> n;
for (int i = 1; i <= 1000000; i++) {
if (cal(i) == n) {
cout << i << endl;
return 0;
}
}
cout << "0" << endl;
return 0;
}

``````

## Code Review

### [1],[2] Answer Code (18. 03. 26)

• Hard Coding으로 [1]처럼 풀었다.

• 일단 풀고 분명 Clean한 Code를 찾아 보았고 [2]을 찾았다.

• 자릿수마다 더하는 Code 정리해 놓자 !