[KOSTA] Spring 기반 Cloud 6일차 - 배열 실습

JUNBEOM PARK·2022년 2월 7일
0
post-thumbnail

🎲 큰 수 출력

📃 풀이

package kosta.mission2;

import java.util.ArrayList;
import java.util.Scanner;

public class Solution2_1 {
	
	public ArrayList<Integer> solution(int n, int arr[]) {
		ArrayList<Integer> answer = new ArrayList<Integer>();
		
		answer.add(arr[0]);
		for(int i=1; i < n; i++) {
			if(arr[i] > arr[i-1]) 
				answer.add(arr[i]);
			
		}
		
		return answer;
	}
	public static void main(String[] args) {
		Solution2_1 s = new Solution2_1();
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] arr = new int[n];//배열 생성
		
		for(int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}
		
		for(int x : s.solution(n, arr)) {
			System.out.print(x + ", ");
		}
		
	}
}

🎲 보이는 학생

📃 풀이

package kosta.mission2;

import java.util.Scanner;

public class Solution2_2 {
	
	//리턴형:int , 파라미터: 배열, 배열개수
	public int solution(int n, int arr[]) {
		int answer = 1;
		int max = arr[0];
		
		for(int i = 1; i < n; i++) {
			if(arr[i] > max) {
				max = arr[i];
				answer++;
			}
		}
		return answer;
		
		
	}
	public static void main(String[] args) {
		Solution2_2 s = new Solution2_2();
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();//8
		int[] arr = new int[n];// 배열에 8개의 방 생성
		
		for(int i = 0; i < n; i++) {
			//130 135 148 140 145 150 150 153
			arr[i] = sc.nextInt(); //배열에 초기화
		}
		
		System.out.println(s.solution(n, arr));
	}
}

🎲 가위 바위 보

📃 풀이

package kosta.mission2;

import java.util.ArrayList;
import java.util.Scanner;

public class Solution2_3 {

	public String solution(int n, int A[], int B[]) {
		
		String answer = "";
		
		//가위1 바위2 보3
		for(int i = 0; i < n; i++) {
			if(A[i] == B[i]) {
				answer += "D";
			}else if(A[i] == 1 && B[i]==3) {
				answer += "A";
			}
			else if(A[i] == 2 && B[i]==1) {
				answer += "A";
			}
			else if(A[i] == 3 && B[i]==2) {
				answer += "A";
			}else {
				answer += "B";
			}
		}
		return answer; //ABABD => A B A B D => char[] 
		               //String => char[] : toCharArray()
	}
	public static void main(String[] args) {
		Solution2_3 s = new Solution2_3();
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int[] A = new int[n];
		int[] B = new int[n];
		//2 3 3 1 3
		//1 1 2 2 3
		
		for(int i = 0; i < n; i++) {
			A[i] = sc.nextInt();
		}
		for(int i = 0; i < n; i++) {
			B[i] = sc.nextInt();
		}
		
		for(char x : s.solution(n, A, B).toCharArray()) {
			System.out.println(x);
		}
	}

}


🎲 점수 계산

📃 풀이

package kosta.mission2;

import java.util.Scanner;

public class Solution2_7 {
	
	public int solution(int n, int arr[]) {
		int answer = 0;
		int count = 0;
		
		for(int i = 0; i < n; i++) {
			if(arr[i] == 1) {
				count++;
			}else {
				count = 0;
			}
			answer += count; 
		}
		return answer;
	}
	public static void main(String[] args) {
		Solution2_7 s = new Solution2_7();
		Scanner sc = new Scanner(System.in);
		
		int n = sc.nextInt();
		int arr[] = new int[n];
		
		for(int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}
		
		System.out.println(s.solution(n, arr));
	}
}

🎲 격자판


📃 풀이

package kosta.mission2;

import java.util.Scanner;

public class Solution2_9 {
	
	//int re = Math.max(a,b);
	public int solution(int n, int arr[][]) {
		int answer = 0;
		
		int raw = 0, col = 0; //각 행과 각 열의 합 구하기
		
		for(int i = 0; i < n; i++) {
			raw = 0; //내부 for문에서 더해진 값을 초기화
			col = 0;
			
			for(int j = 0; j < n; j++) {
				raw += arr[i][j];
				col += arr[j][i];
						
			}
			answer = Math.max(answer, raw); 
			answer = Math.max(answer, col);
		}
		
		int line = 0, reline = 0; // 대각선 합 구하기
		
		for(int i = 0; i < n; i++) {
			line += arr[i][i];
			reline += arr[i][n-i-1];
		}
		answer = Math.max(answer, line); 
		answer = Math.max(answer, reline);
		
		return answer;
	}
	
	public static void main(String[] args) {
		Solution2_9 s = new Solution2_9();
		Scanner sc = new Scanner(System.in);
		
		/*
		 5
		 10 13 10 12 15
		 12 39 30 23 11
		 11 25 50 53 15
		 19 27 29 37 27
		 19 13 30 13 19
		 
		 */
		int n = sc.nextInt();
		int[][] arr = new int[n][n];
		
		for(int i = 0; i < n; i++) {
			for(int j = 0; j < n; j++) {
				arr[i][j] = sc.nextInt();
			}
		}
		
		System.out.println(s.solution(n, arr));
	}
}
profile
DB 엔지니어👍

0개의 댓글