본문 바로가기
ALGORITHM/푼 문제들&과정

[JAVA] 백준 4673번

by sjs_2215 2019. 2. 26.

https://www.acmicpc.net/problem/4673

첫 번째 시도:

문제를 쪼개서 풀고자 일단 d(n)을 구하는 함수는 만듦

문제점:

d(n)에 들어가지 않는 수를 어떻게 뽑아내야할지 모르겠음

//1부터 10000까지 함수의 파라메터에 넣어가며 리턴값이 1~10000중 없는 값을 출력하면 됨.

-> 일단 이렇게 생각함. 근데 그러면 for문을 너무 많이 돌고,, 함수를 아예 다시 만들거나 해야하나 생각 중.

package till;


public class Main {
private static final int[] Kaprekar = null;
public static void main(String[] args){


//1부터 10000까지 함수의 파라메터에 넣어가며 리턴값이 1~10000중 없는 값을 출력하면 됨.
}



//들어오는 수를 d(n)연산함.
public static int Kaprekar(int x) {
int result=0;
String s="";
String[] array_word;

if(x<10){
result=x+x;
} else if(x>=10&&x<100){
s=Integer.toString(x);
array_word=s.split("");
result=x+Integer.parseInt(array_word[0])+Integer.parseInt(array_word[1]);
} else if(x>=100&&x<1000){
s=Integer.toString(x);
array_word=s.split("");
result=x+Integer.parseInt(array_word[0])+Integer.parseInt(array_word[1])+Integer.parseInt(array_word[2]);
} else {
s=Integer.toString(x);
array_word=s.split("");
result=x+Integer.parseInt(array_word[0])+Integer.parseInt(array_word[1])+Integer.parseInt(array_word[2])+Integer.parseInt(array_word[3]);
}
return result;
}
}


두 번째 시도:

숫자만 보고 규칙있나 찾아보았음. 각 숫자가 2차이/11차이?인가 그랬는데 이건 규칙이 아닌듯


세 번째 시도: (해답 봄) 해답

d(n)을 구하는 함수 만드는 것은 맞았음.

그러나 d(n)의 return값을 가지고 해당되지 않는 수를 어떻게 뽑아내야 했는데

for문 돌려서 true/false 체크해주고

false값만 있는 것을 출력해주는 것으로 하면 됨.

알고 있는 숫자를 제외하고 출력하는 법->각 숫자에 false/true값으로 구분해주자

package till;

public class Main {
private static final int[] Kaprekar = null;
public static void main(String[] args){
boolean self[] = new boolean[15000];
int temp=0;

for(int i=1;i<10000;i++){
temp=Kaprekar(i);
self[temp]=false;

if(self[temp]!=false){
System.out.println(temp);
}
}


}



//들어오는 수를 d(n)연산함.
public static int Kaprekar(int x) {
int result=0;
String s="";
String[] array_word;

if(x<10){
result=x+x;
} else if(x>=10&&x<100){
s=Integer.toString(x);
array_word=s.split("");
result=x+Integer.parseInt(array_word[0])+Integer.parseInt(array_word[1]);
} else if(x>=100&&x<1000){
s=Integer.toString(x);
array_word=s.split("");
result=x+Integer.parseInt(array_word[0])+Integer.parseInt(array_word[1])+Integer.parseInt(array_word[2]);
} else {
s=Integer.toString(x);
array_word=s.split("");
result=x+Integer.parseInt(array_word[0])+Integer.parseInt(array_word[1])+Integer.parseInt(array_word[2])+Integer.parseInt(array_word[3]);
}
return result;
}
}


네 번째 시도: 세 번째 시도에서는 좀 아무생각없이 문제 풀어서 원하는 결과값이 안나옴.

좀만 생각해서 false/true값으로 구분할 수 있게 코딩 함

package till;

public class Main {
private static final int[] Kaprekar = null;
public static void main(String[] args){
boolean self[] = new boolean[10200];
int temp=0;

for(int i=1;i<=10100;i++){
temp=Kaprekar(i);
self[temp]=true;
}
for(int i=1;i<10000;i++){
if(self[i]!=true){
System.out.println(i);
}
}

}



//들어오는 수를 d(n)연산함.
public static int Kaprekar(int x) {
int result=0;
String s="";
String[] array_word;

if(x<10){
result=x+x;
} else if(x>=10&&x<100){
s=Integer.toString(x);
array_word=s.split("");
result=x+Integer.parseInt(array_word[0])+Integer.parseInt(array_word[1]);
} else if(x>=100&&x<1000){
s=Integer.toString(x);
array_word=s.split("");
result=x+Integer.parseInt(array_word[0])+Integer.parseInt(array_word[1])+Integer.parseInt(array_word[2]);
} else {
s=Integer.toString(x);
array_word=s.split("");
result=x+Integer.parseInt(array_word[0])+Integer.parseInt(array_word[1])+Integer.parseInt(array_word[2])+Integer.parseInt(array_word[3]);
}
return result;
}
}


'ALGORITHM > 푼 문제들&과정' 카테고리의 다른 글

[JAVA] 백준 1065번  (0) 2019.03.03
[JAVA] 백준 4344번  (0) 2019.02.27
[JAVA] 백준 10817번  (0) 2019.02.01
[JAVA] 백준 1924번  (0) 2019.01.31
[JAVA] 백준 11720번  (0) 2019.01.30

Comments