JAVA_Learn/Do it! _ Algorithm Study

#2. 1주차 과제 2 [ Chap02_ 1 ~ 6 ]

CEJ_0929 2021. 12. 28. 18:55

12월 28일 화요일.

DBMS 시험도 보고... ㅋㅋ

스터디 과제도 합니다 ㅎㅎㅎㅎ

집에가서 꼬막무침 먹어야지!!ㅋ


 

2-1 (IntArray)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package practice;
 
public class IntArray_2_1 {
    
    public static void main(String[] args) {
        
        int [] a = new int[5];
        
        a[1= 37;
        a[2= 51;
        a[4= a[1]*2;
        
        for (int i = 0; i < a.length; i++
            System.out.println("a["+i+"]= "+a[i]);
            
    }
}
cs

 

 

 

 


 

 

2-2 (IntArrayInit)

1
2
3
4
5
6
7
8
9
10
11
12
package practice;
 
public class IntArrayInit_2_2 {
    public static void main(String[] args) {
        
        int [] a = {1,2,3,4,5};
        
        for (int i = 0; i < a.length; i++
            System.out.println("a["+i+"] = "+a[i]);
        
    }
}
cs

 

 


 

 

2-3 (CloneArray)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package practice;
 
public class CloneArray_2_3 {
    public static void main(String[] args) {
        
        int[]a = {1,2,3,4,5};
        int[]b = a.clone();
        
        b[3= 0;
        
        System.out.print("a = ");
        for (int i = 0; i < a.length; i++
            System.out.print(" "+a[i]);
        
        System.out.print("\nb = ");
        for (int i = 0; i < b.length; i++
            System.out.print(" "+b[i]);
 
    }
}
cs

 


 

 

2-4 (MaxOfArray)

 
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
package practice;
 
import java.util.Scanner;
 
public class MaxOfArray_2_4 {
    
    static int maxOf(int[]a) {
        
        int max = a[0];
        for (int i = 1; i < a.length; i++) {
            if(a[i]>max) max = a[i];
        }
        return max;        
    }
    
    public static void main(String[] args) {
        
        Scanner stdIn = new Scanner(System.in);
        System.out.println("키의 최댓값을 구합니다.");
        System.out.print("사람 수: ");
        int num = stdIn.nextInt();
        
        int[]height = new int[num];
        
        for (int i = 0; i < num; i++) {
            System.out.print("height["+i+"] : ");
            height[i] = stdIn.nextInt();
        }
        System.out.println("최댓값은 "+maxOf(height)+" 입니다.");
        
    }
 
}
 
cs

 


 

 

2-5 (MaxOfArrayRand)

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
package practice;
 
import java.util.Random;
import java.util.Scanner;
 
public class MaxOfArrayRand_2_5 {
    
    static int maxOf(int[]a) {
        int max = a[0];
        for (int i = 1; i < a.length; i++
            if(a[i]>max) max = a[i];
        
        return max;    
    }
    
    public static void main(String[] args) {
        
        Random rand = new Random();
        Scanner stdIn = new Scanner(System.in);
        
        System.out.println("키의 최댓값을 구합니다.");
        System.out.print("사람 수: ");
        int num  =stdIn.nextInt();
        
        int[]height = new int[num];
        
        System.out.println("키 값은 아래와 같습니다.");
        
        for (int i = 0; i < num; i++) {
            height[i] = 100+rand.nextInt(90);
            System.out.println("height["+i+"]: "+height[i]);
        }
        
        System.out.println("최댓값은 "+maxOf(height)+"입니다.");
        
    }
}
 
 
cs

 

 

2-6 (ReverseArray)

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 practice;
 
import java.util.Scanner;
 
public class ReverseArray_2_6 {
    
    static void swap(int[]a, int idx1, int idx2) {
        int t = a[idx1];
        a[idx1] = a[idx2];
        a[idx2] = t;
        
    }
    
    static void reverse(int[]a) {
        
        for (int i = 0; i < a.length/2; i++
            swap(a,i,a.length-i-1);
    }
 
    //=======================================================
    public static void main(String[] args) {
        Scanner stdIn = new Scanner(System.in);
        
        System.out.print("요솟수: ");
        int num = stdIn.nextInt();
        
        int[]x = new int[num];
        
        for (int i = 0; i < num; i++) {
            System.out.print("x["+i+"]: ");
            x[i] = stdIn.nextInt();
        }
        
        reverse(x);
        
        System.out.println("요소를 역순으로 정렬하였습니다.");
        
        for (int i = 0; i < num; i++
            System.out.println("x["+i+"] = "+x[i]);
 
    }
 
}
 
 
 
cs