본문 바로가기
JAVA

6일차//[JAVA][실습] 2차원 배열의 합 + 거스름돈 + 암호화(복구화)(ASCII)

by aesup 2021. 1. 18.
728x90

1. 실행결과 sum = 150 이 나오도록 함

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public class report1 {
 
    
        // 실행결과  sum = 150 이 나오도록함
    
        public static void main(String[] args){
        
 
        int[] arr = {1020304050};
        int sum = 0;
        for(int i = 0; i < arr.length; i++) {
            
        sum = arr[i] + sum; // sum += arr[i]; 이랑 똑같다.
        
 
        }
        System.out.println("sum="+sum);
 
        
 
        }
cs

2.  2차원 배열의 합을 구하시오

-출력은 항상 for 문 밖에 써야지 반복실행이 안된다.

 

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
 
public class report2 {
    public static void main(String[] args) {
        
        //2차원배열의 합
        
        int[][] arr = {
 
                { 55555},
                {10,10,10,10,10},
                {20,20,20,20,20},
                {30,30,30,30,30}
 
                };
                
                int total = 0;
                float average = 0;
                
                
                //총합을 구한다.
                // 복습!!!!!!!!!!
                for(int i = 0; i < arr.length; i++) {
                    for(int j = 0; j < arr[i].length; j++) {
                        total =  total + arr[i][j] ;
                        
                    }
                    
                    }
                    
                
                average =(float)total / (arr.length * arr[0].length);
                    
                    System.out.println("total = " + total);
                    System.out.println("average = " + average);
                }
        
cs

3.  큰 금액의 동전을 우선적으로 거슬러 준다( 거스름/ 잔돈 문제)

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 
 
public class report3 {
    public static void main(String[] args) {
        //큰 금액의 동전을 우선적으로 거슬러 줘야한다.
        int[] coinUnit = {500,100,50,10};
        int money = 2680;
        System.out.println("money = " + money);
        
        for(int i = 0; i < coinUnit.length; i++) {
            System.out.println(coinUnit[i] + "원 :" + money/coinUnit[i]+"개 필요합니다.");
            money= money%coinUnit[i];
        }
    }
}
cs

 


 

 

4. 암호화 문제

 

  • .charAt(변수);    -> String  숫자를 입력하면 문자를 돌려준다.
  • (int)c      -> 문자인 c변수에 int 를 붙이면 정수로 변환.
  • 아스키코드를 알고 있어야한다.
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
public class report4 {
 
    public static void main(String[] args) {
        // abcdefghijklmnopqrstuvwxyz
 
        // a b c-> 암호화
        char[] abcCode =
 
                { '`''~''!''@''#''$''%''^''&',
                        '*''('')''-''_''+''=''|''['']''{',
                        '}'';'':'',''.''/' };
 
        // 0 1 2 3 4 5 6 7 8 9
 
        char[] numCode = { 'q''w''e''r''t''y''u''i''o''p' };
 
        String src = "abc123";
        String result = "";
 
        // 문자열 src의 문자를
        // charAt()으로 하나씩 읽는다
        // 그리고 변환 후 result에 저장
 
        // charAt() : 숫자를 입력하면 문자를 준다.
 
        for (int i = 0; i < src.length(); i++) { // src를 하나의 배열로 본다.
            char c = src.charAt(i);
            int asc = (int) c;
 
            // System.out.println(asc);
            // asc 출력하면 (abc123) -> 97, 98, 99, 49, 50, 51
 
            // 알파벳의 경우
            if (asc >= 97 && asc <= 122) {
                asc = asc - 97// 97 ->0
                result = result + abcCode[asc];
            }
 
            // 숫자의 경우
            if (asc >= 48 && asc <= 57) {
                asc = asc - 48// 48 ->0 48은 q
                result = result + numCode[asc];
 
            }
 
            /*
             * 
             * (1) 알맞은 코드를 넣어 완성하시오.
             * 
             */
 
        }
        System.out.println("src:" + src);
        System.out.println("result:" + result);
 
        // 복호화
        //////////////////////////////////////////////////////
        //////////////////////////////////////////////////////
        String baseCode = "";
 
        for (int i = 0; i < result.length(); i++) {
            char c = result.charAt(i);
            int asc = (int) c;
 
            // 알파벡 -> 숫자가 암호화
            int index = 0;
            if (asc >= 97 && asc <= 122) {
                for (int j = 0; j < numCode.length; j++) {
 
                    if (c == numCode[j]) { // c == 'w'
                        index = j;
                        break;
                    }
                }
                index = index + 48// 0 ->48
                baseCode = baseCode + (char) index;
 
            } else {// 기호 > 알파벳이 암호화
                for (int j = 0; j < abcCode.length; j++) {
                    if (c == abcCode[j]) {
                        index = j;
                        break;
                    }
                }
                index = index + 97;
                baseCode = baseCode + (char) index;
            }
        }
        System.out.println("복호화" + baseCode);
        // [실행결과]
        // src:abc123
        // result:`~!wer ----> a가 `
    }
}
 
 
 
 
cs

 

 

 

 

 

728x90