2차원 배열 (23.05.15)

·2023년 5월 16일
0

Coding Test

목록 보기
15/39
post-thumbnail

✏️ 문제 1

1차원 문자열 배열에 학생 이름 초기화되어 있다.
3행 2열 짜리 2차원 문자열 배열 2개를 새로 선언 및 할당하여
학생 이름을 2차원 배열에 순서대로 저장하고 아래와 같이 출력하시오.
(첫 번째 2차원 배열이 모두 저장된 경우 두 번째 2차원 배열에 저장 진행)
String[] students = {"강건강", "남나나", "도대담", "류라라", "문미미", "박보배",
"송성실", "윤예의", "진재주", "차천축", "피풍표", "홍하하"};

[실행 화면]
== 1분단 ==
강건강 남나나
도대담 류라라
문미미 박보배
== 2분단 ==
송성실 윤예의
진재주 차천축
피풍표 홍하하
	```
  • 풀이
	public void practice15() {
		
		String[] student = {"강건강", "남나나", "도대담", "류라라", "문미미", "박보배",
							"송성실", "윤예의", "진재주", "차천축", "피풍표", "홍하하"};
		
		String[][] studentArr = new String[3][2];
		String[][] studentArr2 = new String[3][2];
		
		int index = 0;
		
		System.out.println("== 1분단 ==");
		
		if(index < studentArr.length) {
			
			for(int row=0; row<studentArr.length; row++) {
				
				for(int col=0; col<studentArr[row].length; col++) {
					
					studentArr[row][col] = student[index];
					index++;
				}
				
				System.out.printf("%s  %s\n", studentArr[row][0], studentArr[row][1]);
			}
		}
		
		System.out.println("== 2분단 ==");

		if(index > studentArr.length) {
			
			for(int row=0; row<studentArr2.length; row++) {
				
				for(int col=0; col<studentArr2[row].length; col++) {
					
					studentArr2[row][col] = student[index];
					index++;
				}
				
				System.out.printf("%s  %s\n", studentArr2[row][0], studentArr2[row][1]);
			}
		}
	}

✏️ 문제 2

위 문제에서 자리 배치한 것을 가지고 학생 이름을 검색하여
해당 학생이 어느 자리에 앉았는지 출력하세요.

  • 풀이
	public void practice16() {
		
		String[] student = {"강건강", "남나나", "도대담", "류라라", "문미미", "박보배",
							"송성실", "윤예의", "진재주", "차천축", "피풍표", "홍하하"};
		
		String[][] studentArr = new String[3][2];
		String[][] studentArr2 = new String[3][2];
		
		int index = 0;
		
		System.out.println("== 1분단 ==");
		
		if(index < studentArr.length) {
			
			for(int row=0; row<studentArr.length; row++) {
				
				for(int col=0; col<studentArr[row].length; col++) {
					
					studentArr[row][col] = student[index];
					index++;
				}
				
				System.out.printf("%s  %s\n", studentArr[row][0], studentArr[row][1]);
			}
		}
		
		System.out.println("== 2분단 ==");

		if(index > studentArr.length) {
			
			for(int row=0; row<studentArr2.length; row++) {
				
				for(int col=0; col<studentArr2[row].length; col++) {
					
					studentArr2[row][col] = student[index];
					index++;
				}
				
				System.out.printf("%s  %s\n", studentArr2[row][0], studentArr2[row][1]);
			}
		}
		
		System.out.println("====================================");
		
		Scanner sc = new Scanner(System.in);
		
		System.out.print("검색할 학생 이름을 입력하세요 : ");
		String name = sc.nextLine();
		
		boolean flag = true;
		
		for(int row=0; row<studentArr.length; row++) {
			
			for(int col=0; col<studentArr[row].length; col++) {
				
				if(name.equals(studentArr[row][col])) {
					if(col %2 == 0) {
						System.out.printf("검색하신 %s 학생은 %d분단 %d번째 줄 %s쪽에 있습니다.", studentArr[row][col], 1, row + 1, "왼");
						flag = false;
					} else {
						System.out.printf("검색하신 %s 학생은 %d분단 %d번째 줄 %s쪽에 있습니다.", studentArr[row][col], 1, row + 1, "오른");
						flag = false;
					}
						
				} else if(name.equals(studentArr2[row][col])){
					if(col %2 == 0) {
						System.out.printf("검색하신 %s 학생은 %d분단 %d번째 줄 %s쪽에 있습니다.", studentArr2[row][col], 2, row + 1, "왼");
						flag = false;
					} else {
						System.out.printf("검색하신 %s 학생은 %d분단 %d번째 줄 %s쪽에 있습니다.", studentArr2[row][col], 2, row + 1, "오른");
						flag = false;
					}
				}

			}
		}
		if(flag) System.out.println("검색 결과가 없습니다.");
	}
  • 출력 화면

profile
풀스택 개발자 기록집 📁

0개의 댓글