☞ 문제

 

 

 

☞ 코드

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package algo02;
 
import java.util.Scanner;
 
public class Country124 {
    
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        System.out.println("원하는 숫자를 입력해주세요.");
        int n = sc.nextInt();
        System.out.println(solution(n));
    }
    
    public static String solution(int n) {
        
        String answer = "";
        int remainder = 0;        //나머지
        
        while(n > 0) {            
            remainder = n % 3;    //3으로 나눈 나머지
            n = n / 3;            //3으로 나눈 몫
            
            if(remainder == 0) {        // ex) 3일 때
                n -= 1;
                remainder = 4;
            }
            answer = remainder + answer;
        }
        
        return answer;
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 

 

 

생각만 잘 하면 괜찮은 문제인데 생각하는 게 넘 어려웠다....

 

그나저나....

지금 jre 파일 때문에 컴파일이 안 돼서 실행을 못 한다.....

그래서 프로그래머스에서 돌려봤는데 잘 된다.....

근데 컴파일이 아예 안 돼서 빨리 고쳐야겠다.... 큰일이다...............

'Algorithm' 카테고리의 다른 글

모의고사  (1) 2019.05.12
예산  (1) 2019.05.03
콜라츠 추측  (0) 2019.04.28
시저 암호  (1) 2019.04.25
나누어 떨어지는 숫자 배열  (2) 2019.04.22

넘나 오랜만의 포스팅...

좀 여러가지 사정이 있었는데 어쨌든 제대로 못 올린 건 내 잘못 ㅠㅠ....

이제 다시 꾸준히 올려야겠당....

 

 

 

☞ 문제

 

 

 

☞ 코드

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package algo01;
 
 
public class mockTest {
    
    public static void main(String[] args) {
        
        int[] answers = {13242};
        solution(answers);
    }
    
    
    public static int[] solution(int[] answers) {
    
        int[] one = {12345};                    //1번 수포자의 답
        int[] two = {21232425};            //2번 수포자의 답
        int[] three = {3311224455};    //3번 수포자의 답
        int cnt1 = 0;            //1번 수포자가 맞혔을 때 증가하는 변수
        int cnt2 = 0;            //2번 수포자가 맞혔을 때 증가하는 변수
        int cnt3 = 0;            //3번 수포자가 맞혔을 때 증가하는 변수
        
 
        //입력한 숫자와 세 수포자의 답 비교한 후 변수 증가
        for(int i = 0; i < answers.length; i++) {
            if(answers[i] == one[i%one.length]) {
                cnt1++;
            }
            if(answers[i] == two[i%two.length]) {
                cnt2++;
            }
            if(answers[i] == three[i%three.length]) {
                cnt3++;
            }
        }
        
       
        int[] cntNum = {cnt1, cnt2, cnt3};
        Arrays.sort(cntNum);
        
//세 수포자 중 가장 많이 맞힌 문제의 갯수 (cnt1 / cnt2 / cnt3 중 가장 높은 숫자)
        int max = cntNum[2];
        
        List<Integer> maxList = new ArrayList<>();
        if(cnt1 == max) {
            maxList.add(1);
        } 
        if(cnt2 == max) {
            maxList.add(2);
        } 
        if(cnt3 == max) {
            maxList.add(3);
        }
        
        int[] answer = new int[maxList.size()];
        int i = 0;
        for(int temp : maxList) {
            answer[i++= temp;
            System.out.println("answer = " + temp);
        }  
        
        return answer;
    }
    
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 

 

 

 

☞ 실행결과

 

 

'Algorithm' 카테고리의 다른 글

124 나라의 숫자  (0) 2019.05.27
예산  (1) 2019.05.03
콜라츠 추측  (0) 2019.04.28
시저 암호  (1) 2019.04.25
나누어 떨어지는 숫자 배열  (2) 2019.04.22

이번 문제는 기존에 풀었던 문제보다 어려울 거 같아서 풀어보자고 한 건데 생각보다 넘 쉬웠당.......ㅋ

처음엔 또 문제를 잘못 읽어서 겁나 어렵겠다~ 했는데 잘못 읽은 거였다 ㅎㅎ

 

암튼~! 그래서!

 

 

 

☞ 문제

 

 

 

 

☞ 코드

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package algo01;
 
 
public class budget {
    
    public static void main(String[] args) {
    
        int[] d = {2233};
        solution(d, 10);
    }
    
    
    public static int solution(int[] d, int budget) {
        
        int hap = 0;
        int answer = 0;
        
        Arrays.sort(d);
 
        for(int i = 0; i < d.length; i++) {
            hap += d[i];
            System.out.println(hap);
            
            if(hap <= budget) {
                answer = i + 1;
            }
        }
        
        System.out.println("answer = " + answer);
        
        return answer;
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 

 

우선 배열에 있는 값들을 오름차순으로 정렬을 하고 나서 for문을 돌리는 게 더 효율적이라고 생각돼서 Arrays.sort() 메서드를 썼다. <완주하지 못한 선수> 를 풀 때도 Arrays.sort() 메서드를 사용했었는데, 이 메서드는 진짜 좋은 메서드다....

Arrays.sort() 최고....

 

 

 

☞ 실행결과

 

 

 

 

 

(정확성은 100이니까 참고해도 됩니당...)

 

 

'Algorithm' 카테고리의 다른 글

124 나라의 숫자  (0) 2019.05.27
모의고사  (1) 2019.05.12
콜라츠 추측  (0) 2019.04.28
시저 암호  (1) 2019.04.25
나누어 떨어지는 숫자 배열  (2) 2019.04.22

☞ 문제

 

 

 

 

☞ 코드

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package algo01;
 
import java.util.Scanner;
 
public class Collatz {
//    1-1. 입력된 수가 짝수라면 2로 나눕니다. 
//    1-2. 입력된 수가 홀수라면 3을 곱하고 1을 더합니다.
//    2. 결과로 나온 수에 같은 작업을 1이 될 때까지 반복합니다.
    
//    예를 들어, 입력된 수가 6이라면 6→3→10→5→16→8→4→2→1 이 되어 총 8번 만에 1이 됩니다. 
//    위 작업을 몇 번이나 반복해야하는지 반환하는 함수, solution을 완성해 주세요. 
//    단, 작업을 500번을 반복해도 1이 되지 않는다면 –1을 반환해 주세요.
    
    
    public static int solution(int num) {
        long lnum = num;
        int answer = 0;
          
        while(lnum != 1) {
            if(lnum % 2 == 0) {
                lnum = lnum / 2;
                answer++;
            } else {
                lnum = lnum * 3 + 1;
                answer++;
            }
                        
            if(answer >= 500) {
                answer = -1;
                
                break;
            } 
        }
            
        System.out.println("answer = " + answer);
        return answer;
    }
    
    
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        System.out.println("숫자를 입력해주세요.");
        int num = sc.nextInt();
        solution(num);
        
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 

 

 

 

☞ 실행결과

 

 

☞ 정확성

 

 

 

생각보다 금방 푼 문제였당~~

조건문을 잘못 줘서 정확성에서 계속 점수가 낮았는데 고쳐서 뿌듯...

그리고 처음에 입력받은 값을 int로 받아서 오류가 났었다ㅠㅠ

숫자의 길이가 커져서 long으로 받았어야 됐는데 ㄲㅂ

어쨌든 생각보다 쉬웠다 땅땅땅-! 

 

일요일 오후까지 안 올렸으니까 오늘은 노사람~~!

 

'Algorithm' 카테고리의 다른 글

모의고사  (1) 2019.05.12
예산  (1) 2019.05.03
시저 암호  (1) 2019.04.25
나누어 떨어지는 숫자 배열  (2) 2019.04.22
두 정수 사이의 합  (4) 2019.04.18

☞ 문제

 

 

 

 

☞ 코드

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package algo01;
 
import java.util.Scanner;
 
public class CaesarCode {
//    어떤 문장의 각 알파벳을 일정한 거리만큼 밀어서 다른 알파벳으로 바꾸는 암호화 방식을 시저 암호라고 합니다. 
//    예를 들어 AB는 1만큼 밀면 BC가 되고, 3만큼 밀면 DE가 됩니다. z는 1만큼 밀면 a가 됩니다. 
//    문자열 s와 거리 n을 입력받아 s를 n만큼 민 암호문을 만드는 함수, solution을 완성해 보세요.
    
    public static String solution(String s, int n) {
        
        String answer = "";
        char[] charAs = s.toCharArray();    
        char ch = 0;
        int a = 0;                //아스키코드 값과 입력 받은 값을 더한 변수
      
        for(int i = 0; i < charAs.length; i++) {
            a = (int)charAs[i];
    
            if(a > 64 && a < 91) {
                a += n;
                if(a > 90) {
                    a -= 26;            //ex) z, 2를 입력했을 때, 92가 되지만 66이 나와야 하기 때문에 -26
                }
            } else if(a > 96 && a < 123) {
                a = a + n;
                if(a > 122) {
                    a = a - 26;
                }
            }
            
            ch = (char)a;
            charAs[i] = ch;
    
        }
        
        answer = String.valueOf(charAs);
          
        return answer;
    }
    
    
    public static void main(String[] args) {
    
        String s;
        int n;
        
        Scanner sc = new Scanner(System.in);
        System.out.println("문자열을 입력해주세요.");
        s = sc.nextLine();
        Scanner sc2 = new Scanner(System.in);
        System.out.println("숫자를 입력해주세요.");
        n = sc.nextInt();
         
        System.out.println(solution(s,n));
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 

 

 

 

☞ 실행결과

 

 

 

 

 

음 조건문만 잘 주면 쉬운 문제였당^^.....

다음 문제부터는 for문을 활용할 때 향상된 for문을 써봐야겠다~!

 

아 그리고 이제 정확성은 귀찮아서 스샷 안 찍으련다....ㅎ....

 

 

 

'Algorithm' 카테고리의 다른 글

예산  (1) 2019.05.03
콜라츠 추측  (0) 2019.04.28
나누어 떨어지는 숫자 배열  (2) 2019.04.22
두 정수 사이의 합  (4) 2019.04.18
가운데 글자 가져오기  (1) 2019.04.15

☞ 문제

 

 

 

 

☞ 코드

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package algo01;
 
 
public class array {
    //array의 각 element 중 divisor로 나누어 떨어지는 값을 오름차순으로 정렬한 배열을 반환하는 함수, solution을 작성해주세요.
    //divisor로 나누어 떨어지는 element가 하나도 없다면 배열에 -1을 담아 반환하세요.
    
    public static int[] solution(int[] arr, int divisor) {    
        
        int count = 0;
        int c = 0;
        int[] answer = {};
        
        for(int i = 0; i < arr.length; i++) {
            if(arr[i] % divisor == 0) {
                count++;
            } 
        }
    
        answer = new int[count];
 
        for(int i = 0; i < arr.length; i++) {
            if(arr[i] % divisor == 0) {
                answer[c++= arr[i];
            }
        }
        
        if(answer.length == 0) {
            answer = new int[] {-1};
        }
        
        Arrays.sort(answer);
        return answer;
      }
    
    
    
    public static void main(String[] args) {
        
        int[] arr = {23613};
        System.out.println(Arrays.toString(solution(arr, 3)));
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 

 

 

 

☞ 실행결과

 

 

 

☞ 정확성

 

 

 

너무 졸려서 영혼없이 풀었다........ 미리미리 풀어둘 걸........

앞으로 일요일 오후까지 글 안 올리면 노사람........

 

알고리즘 자체는 어렵지 않은데 배열에 또 담으라고 하는 게 짱나넵 ㅎ_ㅎ

 

어쨌든 끄읏~~~

'Algorithm' 카테고리의 다른 글

콜라츠 추측  (0) 2019.04.28
시저 암호  (1) 2019.04.25
두 정수 사이의 합  (4) 2019.04.18
가운데 글자 가져오기  (1) 2019.04.15
완주하지 못한 선수  (1) 2019.04.15

☞ 문제

 

 

 

☞ 코드

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package algo01;
 
public class intHap {
    
    public static long solution(int a, int b) {
        
        long answer = 0;
        int i;
        
        if(a < b) {    
            for(i = a; i <= b ; i++) {
                answer += i;
            }        
            
        } else if(a > b) {
            for(i = b; i <= a; i++) {
                answer += i;
            }
        } else {
            answer = a;
        }
 
        System.out.println("숫자들을 합한 값은 : " + answer);
        return answer;
 
    }
    
    
    
    public static void main(String[] args) {
        
//        두 정수 a, b가 주어졌을 때 a와 b 사이에 속한 모든 정수의 합을 리턴하는 함수, solution을 완성하세요. 
//        예를 들어 a = 3, b = 5인 경우, 3 + 4 + 5 = 12이므로 12를 리턴합니다.
        
//        • a와 b가 같은 경우는 둘 중 아무 수나 리턴하세요.
//        • a와 b는 -10,000,000 이상 10,000,000 이하인 정수입니다.
//        • a와 b의 대소관계는 정해져있지 않습니다.
 
        solution(15);
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 

 

 

 

☞ 실행결과

 

 

 

음... 쉬운 문제여서 금방 풀었다..... 문제를 잘못 읽어서 다시 풀긴 했지만.......ㅎ..............

연속되는 숫자만 해당되는 줄....ㅎㅎㅎㅎㅎ

그럴 수도 있지 뭐~~~~~~

'Algorithm' 카테고리의 다른 글

콜라츠 추측  (0) 2019.04.28
시저 암호  (1) 2019.04.25
나누어 떨어지는 숫자 배열  (2) 2019.04.22
가운데 글자 가져오기  (1) 2019.04.15
완주하지 못한 선수  (1) 2019.04.15

☞ 문제

프로그래머스 문제

 

 

☞ 코드

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package algo01;
 
import java.util.Scanner;
 
public class StringExercise {
//        단어 s의 가운데 글자를 반환하는 함수, solution을 만들어 보세요. 단어의 길이가 짝수라면 가운데 두글자를 반환하면 됩니다.
//        예)
//        abcde    c
//        qwer    we
    
    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        System.out.println("원하는 글자를 입력해주세요.");
        
        String[] strArray = new String[100];
        String str = sc.nextLine();
        String res;
 
        strArray = str.split("");                    //한 글자씩 저장하기
        
        if(strArray.length % 2 == 0) {                //배열의 길이가 짝수면 길이를 2로 나누고 그 앞에 글자와 2로 나눈 값의 글자를 가져온다.
            res = strArray[strArray.length/2 - 1+ strArray[strArray.length/2];
            System.out.println(res);
        } else {                                    //배열의 길이가 홀수면 길이를 2로 나눈 값만 가져온다.
            res = strArray[strArray.length/2];
            System.out.println(res);
        }
            
    }
 
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4f; text-decoration:none">Colored by Color Scripter
 

 

 

 

☞ 실행결과

 

 

 

프로그래머스에서 하라는 대로 코드 짜서 컴파일 시켰는데 프로그래머스가 멍청하게 이상한 오류를 계속 내서 프로그래머스에서 테스트를 못했당...

42번째 줄에서 오류라는데 내 코드는 33줄이 끝인데 ㅎㅎ......

 

그래서 그냥 내가 문제 보지도 않고 짰던 코드로 올리기로 했당~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

'Algorithm' 카테고리의 다른 글

콜라츠 추측  (0) 2019.04.28
시저 암호  (1) 2019.04.25
나누어 떨어지는 숫자 배열  (2) 2019.04.22
두 정수 사이의 합  (4) 2019.04.18
완주하지 못한 선수  (1) 2019.04.15

+ Recent posts