*Practice 1 : 124 World Practice

 

124 나라가 있습니다. 124 나라에서는 10진법이 아닌 다음과 같은 자신들만의 규칙으로 수를 표현합니다.

  1. 124 나라에는 자연수만 존재합니다.
  2. 124 나라에는 모든 수를 표현할 때 1, 2, 4만 사용합니다.

예를 들어서 124 나라에서 사용하는 숫자는 다음과 같이 변환됩니다.

10진법124 나라10진법124 나라

1 1 6 14
2 2 7 21
3 4 8 22
4 11 9 24
5 12 10 41

 

*Solution : 

public class WorldBy124_2 {
	public static void main(String[] args) {
		WorldBy124_2 wd = new WorldBy124_2();
		int number = 10;
		System.out.println(wd.solution(number));
	}

	private String solution(int number) {
	    String answer = "";
	    
	    while(number != 0 ) {
	    	int lastDigit = number % 3;
	    	number = number / 3;
	    	
	    	if(lastDigit == 0) {
	    		number = number - 1;
	    		lastDigit = 4;
	    	}
	    	answer = lastDigit + answer;
	    }
	    
		return answer;
	}
}

 

 

*Practice2 : Competition Practice

 

수많은 마라톤 선수들이 마라톤에 참여하였습니다. 단 한 명의 선수를 제외하고는 모든 선수가 마라톤을 완주하였습니다. 마라톤에 참여한 선수들의 이름이 담긴 배열 participant와 완주한 선수들의 이름이 담긴 배열 completion이 주어질 때, 완주하지 못한 선수의 이름을 return 하도록 solution 함수를 작성해주세요.

 

[leo, kiki, eden] [eden, kiki] leo
[marina, josipa, nikola, vinko, filipa] [josipa, filipa, marina, nikola] vinko
[mislav, stanko, mislav, ana] [stanko, ana, mislav] mislav

 

*Solution : 

public class Competitions {

	public static void main(String[] args) {
		String [] participant = {"leo", "kiki", "eden"};
		String [] completion = {"eden", "filpa"};
		
		System.out.println(solution(participant, completion));
	}

	private static String solution(String[] participant, String[] completion) {
		
		Arrays.sort(participant);
		Arrays.sort(completion);
		
		for(int inx=0; inx < completion.length; inx++) {
			if(!participant[inx].equals(completion[inx])) {
				return participant[inx];
			}
		}
		
		return participant[participant.length-1];
		
	}

}

 

*Practice3 : Find K Number Practice

배열 array의 i번째 숫자부터 j번째 숫자까지 자르고 정렬했을 때, k번째에 있는 수를 구하려 합니다.

예를 들어 array가 [1, 5, 2, 6, 3, 7, 4], i = 2, j = 5, k = 3이라면

  1. array의 2번째부터 5번째까지 자르면 [5, 2, 6, 3]입니다.
  2. 1에서 나온 배열을 정렬하면 [2, 3, 5, 6]입니다.
  3. 2에서 나온 배열의 3번째 숫자는 5입니다.

배열 array, [i, j, k]를 원소로 가진 2차원 배열 commands가 매개변수로 주어질 때, commands의 모든 원소에 대해 앞서 설명한 연산을 적용했을 때 나온 결과를 배열에 담아 return 하도록 solution 함수를 작성해주세요.

[1, 5, 2, 6, 3, 7, 4] [[2, 5, 3], [4, 4, 1], [1, 7, 3]] [5, 6, 3]

*Solution : 

public class FindKNumber {

	public static void main(String[] args) {
		FindKNumber fn = new FindKNumber();
		
		int [] inputArray = {1, 5, 2, 6, 3, 7, 4};
		int [][] commands = {{2, 5, 3}, {4, 4, 1}, {1, 7, 3}};
		
		System.out.println(Arrays.toString(fn.solution(inputArray, commands)));
		
	}
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length];
//        List<Integer> list = new ArrayList<Integer>();
        
        for(int inx=0; inx<commands.length; inx++) {
        	int [] tempArr = Arrays.copyOfRange(array, commands[inx][0] - 1, commands[inx][1]);
        	Arrays.sort(tempArr);
        	answer[inx] = tempArr[commands[inx][2] - 1];
        }
//        answer = new int[list.size()];
//        
//        for(int inx = 0 ; inx<answer.length; inx++) {
//        	answer[inx] = list.get(inx).intValue();
//        }
//        
        return answer;
        
    }
}

 

*Practice4 : Find Local Date 

 

2016년 1월 1일은 금요일입니다. 2016년 a월 b일은 무슨 요일일까요? 두 수 a ,b를 입력받아 2016년 a월 b일이 무슨 요일인지 리턴하는 함수, solution을 완성하세요. 요일의 이름은 일요일부터 토요일까지 각각 SUN,MON,TUE,WED,THU,FRI,SAT

입니다. 예를 들어 a=5, b=24라면 5월 24일은 화요일이므로 문자열 TUE를 반환하세요.

제한 조건

  • 2016년은 윤년입니다.
  • 2016년 a월 b일은 실제로 있는 날입니다. (13월 26일이나 2월 45일같은 날짜는 주어지지 않습니다)
a                                                  b result
5 24 TUE

 

*Solution 

public class LocalDatePractice {

	public static void main(String[] args) {
		LocalDatePractice lc = new LocalDatePractice();
		int month = 5;
		int days = 24;
		System.out.println(lc.solution(month, days));
	}

	  public String solution(int a, int b) {
	     LocalDate ld1 = LocalDate.of(2016,a,b);
	     String str = ld1.getDayOfWeek().toString().substring(0,3);
	     return str; 
	  }
}

 

*Practice5 : Hate Same Number Practice

 

배열 arr가 주어집니다. 배열 arr의 각 원소는 숫자 0부터 9까지로 이루어져 있습니다. 이때, 배열 arr에서 연속적으로 나타나는 숫자는 하나만 남기고 전부 제거하려고 합니다. 배열 arr에서 제거 되고 남은 수들을 return 하는 solution 함수를 완성해 주세요. 단, 제거된 후 남은 수들을 반환할 때는 배열 arr의 원소들의 순서를 유지해야 합니다.
예를들면

  • arr = [1, 1, 3, 3, 0, 1, 1] 이면 [1, 3, 0, 1] 을 return 합니다.
  • arr = [4, 4, 4, 3, 3] 이면 [4, 3] 을 return 합니다.

배열 arr에서 연속적으로 나타나는 숫자는 제거하고 남은 수들을 return 하는 solution 함수를 완성해 주세요.

 

arr answer
[1,1,3,3,0,1,1] [1,3,0,1]
[4,4,4,3,3] [4,3]

 

*Solution1 :

public class HateSameNumber {

	public static void main(String[] args) {
		HateSameNumber hsn = new HateSameNumber();
		int [] inputData = {};
		System.out.println(Arrays.toString(hsn.solution(inputData)));
	}
	
	public int[] solution(int []arr) {
        List<Integer> list = new ArrayList();
  
        for(int inx=0; inx<arr.length; inx++){
            if(inx==arr.length-1){
                list.add(arr[inx]);
                break;
            }
            if(arr[inx] != arr[inx+1]){
                list.add(arr[inx]);
            }
        }
        int [] answer = new int[list.size()];
        
        int counter = 0;
        
        for(int number : list){
            answer[counter++] = number;
        }
        return answer;

	}
}

*Solution2 :

public class HateSameNumber {

	public static void main(String[] args) {
		HateSameNumber hsn = new HateSameNumber();
		int [] inputData = {};
		System.out.println(Arrays.toString(hsn.solution(inputData)));
	}
	
	public int[] solution(int []arr) {
	        ArrayList<Integer> tempList = new ArrayList<Integer>();
	        int preNum = 10;
	        for(int num : arr) {
	            if(preNum != num){
	                tempList.add(num);
                   }
	            preNum = num;
	        }       
	        int[] answer = new int[tempList.size()];
	        for(int i=0; i<answer.length; i++) {
	            answer[i] = tempList.get(i).intValue();
	        }
	        return answer;
	}
}

+ Recent posts