Problem Solving

[Java] Requirements Q067~: 배열에 요소 삽입, 삭제, 배열의 값 2자리씩 합산

Isaac S. Lee 2023. 7. 31. 09:33
package com.test.question;

import java.util.Arrays;
import java.util.Scanner;

public class Q067 {
	public static void main(String[] args) {
		/*
		● 요구사항
		배열에 요소를 삽입하시오.
		
		● 조건
		- 배열 길이: 10
		- 마지막 요소는 우측으로 옮겨질 공간이 없으면 삭제된다.
		
		● 입력 1
		삽입 위치: 2 
		값: 100 
		
		● 출력 1
		원본: [5, 6, 1, 3, 2, 0, 0, 0, 0, 0]
		결과: [5, 6, 100, 1, 3, 2, 0, 0, 0, 0]
		
		● 입력 2
		삽입 위치: 5 
		값: 100 
		
		● 출력 2
		원본: [5, 6, 1, 3, 2, 8, 7, 4, 10, 9]
		결과: [5, 6, 1, 3, 2, 100, 8, 7, 4, 10]
		*/
		
		Scanner scan = new Scanner(System.in);
		
		int[] list = new int [10]; // 난수 10개
		int[] copy;
		int insert; // 삽입 위치
		int number; // 삽입할 값
		
		// 난수 10개 값 할당 (0~10)
		for (int i = 0; i < list.length; i++) {
			list[i] = (int)(Math.random() * 10) + 0;
		}
		
		// 원본 배열 copy
		copy = Arrays.copyOfRange(list, 0, list.length);
		
		// 삽입 위치 입력	
		System.out.printf("삽입 위치: ");
		insert = scan.nextInt();
		
		// 삽입할 값 입력
		System.out.printf("값: ");
		number = scan.nextInt();
		
		// 삽입할 위치까지 숫자를 뒤로 옮김
		for (int i = list.length - 2; i >= insert; i--) {
			list[i + 1] = list[i];
			/*
			insert가 0일 경우 i: 8 7 6 5 4 3 2 1 0
			insert가 5일 경우 i: 8 7 6 5
			insert가 9일 경우 i:
			*/
		}
		
		// 삽입할 위치에 삽입할 값 할당
		list[insert] = number;
		
		// 출력
		System.out.printf("원본: %s\n", Arrays.toString(copy));
		System.out.printf("결과: %s\n", Arrays.toString(list));
		
		scan.close();
	}
}
package com.test.question;

import java.util.Arrays;
import java.util.Scanner;

public class Q068 {
	public static void main(String[] args) {
		/*
		● 요구사항
		배열의 요소를 삭제하시오.
		
		● 조건
		- 배열 길이: 10
		- 마지막 요소는 0으로 채우시오.
		
		● 입력 1
		삭제 위치: 2 
		
		● 출력 1
		원본: [5, 6, 1, 3, 2, 0, 0, 0, 0, 0]
		결과: [5, 6, 3, 2, 0, 0, 0, 0, 0, 0]
		
		● 입력 2
		삽입 위치: 5 
		
		● 출력 2
		원본: [5, 6, 1, 3, 2, 8, 7, 4, 10, 9]
		결과: [5, 6, 1, 3, 2, 7, 4, 10, 9, 0]
		*/
		
		Scanner scan = new Scanner(System.in);
		
		int[] list = new int [10]; // 난수 10개
		int[] copy;
		int erase; // 삭제 위치
		
		// 난수 10개 값 할당 (0~10)
		for (int i = 0; i < list.length; i++) {
			list[i] = (int)(Math.random() * 10) + 0;
		}
		
		// 원본 배열 copy
		copy = Arrays.copyOfRange(list, 0, list.length);
		
		// 삽입 위치 입력
		System.out.printf("삭제 위치: ");
		erase = scan.nextInt();
		
		// 삭제할 위치까지 숫자를 앞으로 옮김
		for (int i = erase; i < list.length - 1; i++) {
			list[i] = list[i + 1];
			/*
			insert가 0일 경우 i: 0 1 2 3 4 5 6 7 8
			insert가 5일 경우 i: 5 6 7 8
			insert가 9일 경우 i:
			*/
		}
		
		// 배열의 마지막 값으로 0 할당
		list[list.length - 1] = 0;
		
		// 출력
		System.out.printf("원본: %s\n", Arrays.toString(copy));
		System.out.printf("결과: %s\n", Arrays.toString(list));
		
		scan.close();
	}
}
package com.test.question;

import java.util.Arrays;
import java.util.Scanner;

public class Q069 {
	public static void main(String[] args) {
		/*
		● 요구사항
		배열의 요소를 순차적으로 2개씩 더한 결과를 배열로 생성한 뒤 출력하시오.
		
		● 조건
		- 원본 배열 길이: 사용자 입력
		- 원본 배열 요소: 난수(1~9)
		- 결과 배열 길이: 사용자 입력 / 2
		
		● 입력 1
		배열 길이: 10 
		
		● 출력 1
		원본: [ 1, 5, 3, 6, 2, 7, 8, 2, 2, 9 ]
		결과: [ 6, 9, 9, 10, 11 ]
		
		● 입력 2
		배열 길이: 5
		
		● 출력 2
		원본: [ 1, 5, 3, 2, 7 ]
		결과: [ 6, 5, 7 ]
		*/
		
		Scanner scan = new Scanner(System.in);
		
		int number; // 숫자 개수
		
		// 난수 개수 입력
		System.out.printf("난수 개수: ");
		number = scan.nextInt();
		// scan.skip("\r\n"); // 버퍼 개행 제거
		
		// 난수 개수만큼 배열 생성
		int[] list = new int[number];
		int[] listPlus = new int[number % 2 == 0 ? number / 2 : number / 2 + 1];
		
		// 난수 값 할당 (1~9)
		for (int i = 0; i < list.length; i++) {
			list[i] = (int)(Math.random() * 9) + 1;
		}
		
		// 순차적으로 숫자 2개씩 덧셈 연산
		for (int i = 0; i < list.length; i += 2) {
			if(number % 2 == 1 && i == list.length - 1){
				// java.lang.ArrayIndexOutOfBoundsException 오류 발생 방지
				// 난수 개수가 홀수 && 덧셈할 수 있는 난수가 마지막 1개만 남음
				listPlus[listPlus.length - 1] = list[list.length - 1];
				break;
			}
			
			// 숫자 2개씩 덧셈
			listPlus[i / 2] = list[i] + list[i + 1];
		}

		// 출력
		System.out.printf("원본: %s\n", Arrays.toString(list));
		System.out.printf("결과: %s\n", Arrays.toString(listPlus));
		
		scan.close();
	}
}