본문 바로가기

코딩 테스트/Project Euler @ kr

38) 어떤 수에 (1, 2, ... )를 곱해서 이어붙여 얻을 수 있는 가장 큰 1 ~ 9 팬디지털 숫자

반응형

http://euler.synap.co.kr/prob_detail.php?id=38


Skillist 설명---------------------------------------------------------------------------------


public static void main(String[] args){

int max = 0;

for(int i=1;i<10000;i++){

String now = "";

int j;

for(j=1;j<10;j++){

now += String.valueOf(i*j);

if(now.length()>=9){

break;

}

}

if(now.length()==9 && j!=1){

int total = 0;

for(int k=0;k<9;k++){

if(now.charAt(k) == '0'){

break;

}else if(now.charAt(k)=='1'){

total+=1;

}else if(now.charAt(k)=='2'){

total+=10;

}else if(now.charAt(k)=='3'){

total+=100;

}else if(now.charAt(k)=='4'){

total+=1000;

}else if(now.charAt(k)=='5'){

total+=10000;

}else if(now.charAt(k)=='6'){

total+=100000;

}else if(now.charAt(k)=='7'){

total+=1000000;

}else if(now.charAt(k)=='8'){

total+=10000000;

}else if(now.charAt(k)=='9'){

total+=100000000;

}

}

if(total == 111111111 && max<Integer.parseInt(now)){

max = Integer.parseInt(now);

}

}

}

System.out.println(max);

}


다시 공부---------------------------------------------------


public static void main(String arg[]){

int max = 0;

for(int i=1; i<10000;i++){

StringBuffer now = new StringBuffer();

int j;

for(j=1;;j++){

now.append(String.valueOf(i*j));

if(now.length() >= 9){

break;

}

}

if(now.length() == 9 && confirm(Integer.valueOf(now.toString()))){

if(max < Integer.valueOf(now.toString())){

max = Integer.valueOf(now.toString());

}

}

}

System.out.println(max);

}

public static boolean confirm(int n){

HashSet<Integer> set = new HashSet<Integer>();

int temp = n;

while(temp>0){

if(temp%10!=0 && set.add(temp%10)){

temp/=10;

}else{

return false;

}

}

if(set.size()==9){

return true;

}

return false;

}

반응형