Problem Solving

[Java] Requirements Q070~: 배열 숫자 출력 모음

Isaac S. Lee 2023. 7. 31. 09:56

Q070 1번 문제

package com.test.question;

public class Q070 {
	public static void main(String[] args) {
		/*
		● 요구사항
		아래와 같이 출력하시오.
		
		● 출력
		1	2	3	4	5
		10	9	8	7	6
		11	12	13	14	15
		20	19	18	17	16
		21	22	23	24	25
		*/
		
		int[][] nums = new int[5][5];
		
		// 데이터 입력 > 문제
		int n = 1;
		
		for (int i=0; i<5; i++) {
			if (i%2 == 0) {
				for (int j=0; j<5; j++) {
					nums[i][j] = n;
					n++;
				}
			}
			else {
				for (int j=4; j>=0; j--) {
					nums[i][j] = n;
					n++;
				}
			}
		}
		
		// 출력 > 수정 없이 그대로 사용
		for (int i=0; i<5; i++) {
			for(int j=0; j<5; j++) {
				System.out.printf("%5d", nums[i][j]);
			}
			System.out.println();
		}
	}
}

 

Q071 2번 문제

package com.test.question;

public class Q071 {
	public static void main(String[] args) {
		/*
		● 요구사항
		아래와 같이 출력하시오.
		
		● 출력
		25	24	23	22	21
		20	19	18	17	16
		15	14	13	12	11
		10	9	8	7	6
		5	4	3	2	1
		*/
		
		int[][] nums = new int[5][5];
		
		// 데이터 입력 > 문제
		int n = 1;
		
		for (int i=4; i>=0; i--) {
			for (int j=4; j>=0; j--) {
				nums[i][j] = n;
				n++;
			}
		}
		
		// 출력 > 수정 없이 그대로 사용
		for (int i=0; i<5; i++) {
			for(int j=0; j<5; j++) {
				System.out.printf("%5d", nums[i][j]);
			}
			System.out.println();
		}
	}
}

 

Q072 3번 문제

package com.test.question;

public class Q072 {
	public static void main(String[] args) {
		/*
		● 요구사항
		아래와 같이 출력하시오.

		● 출력
		1	6	11	16	21
		2	7	12	17	22
		3	8	13	18	23
		4	9	14	19	24
		5	10	15	20	25
		*/
		
		int[][] nums = new int[5][5];
		
		// 데이터 입력 > 문제
		int n = 1;
		
		for (int i=0; i<5; i++) {
			for (int j=0; j<5; j++) {
				nums[j][i] = n;
				n++;
			}
		}
		
		// 출력 > 수정 없이 그대로 사용
		for (int i=0; i<5; i++) {
			for(int j=0; j<5; j++) {
				System.out.printf("%5d", nums[i][j]);
			}
			System.out.println();
		}
	}
}

 

Q073 4번 문제

package com.test.question;

public class Q073 {
	public static void main(String[] args) {
		/*
		● 요구사항
		아래와 같이 출력하시오.
		
		● 출력
		1	2	3	4	5
		6	7	8	9	0
		10	11	12	0	0
		13	14	0	0	0
		15	0	0	0	0
		*/
		
		int[][] nums = new int[5][5];

		// 데이터 입력 > 문제
		int n = 1;

		for (int i=0; i<5; i++) {
			for (int j=0; j<5-i; j++) {
				nums[i][j] = n;
				n++;
			}
		}

		// 출력 > 수정 없이 그대로 사용
		for (int i=0; i<5; i++) {
			for(int j=0; j<5; j++) {
				System.out.printf("%5d", nums[i][j]);
			}
			System.out.println();
		}
	}
}

 

Q074 5번 문제

package com.test.question;

public class Q074 {
	public static void main(String[] args) {
		/*
		● 요구사항
		아래와 같이 출력하시오.
		
		● 출력
		0	0	1	0	0
		0	2	3	4	0
		5	6	7	8	9
		0	10	11	12	0
		0	0	13	0	0
		
				0,2
			1,1 1,2 1,3
		2,0 2,1 2,2 2,3 2,4
			3,1 3,2 3,3
				4,2
		2
		1 2 3
		0 1 2 3 4
		1 2 3
		2
		*/

		int[][] nums = new int[5][5];

		// 데이터 입력 > 문제
		int n = 1; // 시작 숫자
		int stac = nums.length / 2; // 시작 열 인덱스
		int count = 1; // 중앙에서부터 시작하여 오른쪽으로 증가시킬 숫자 개수

		for (int i = 0; i < nums.length; i++) {
			for (int j = stac; j < stac + count; j++) {
				nums[i][j] = n; // 해당 위치에 숫자 채우기
				n++; // 숫자 증가
			}

			if (i < nums.length / 2) {
				stac--; // 중앙으로부터 시작 열 인덱스 왼쪽으로 이동
				count += 2; // 숫자를 오른쪽으로 증가시킬 개수 늘리기
			} else {
				stac++; // 중앙으로부터 시작 열 인덱스 오른쪽으로 이동
				count -= 2; // 숫자를 오른쪽으로 증가시킬 개수 줄이기
			}
		}
        
		// 다른 방법
		int[][] nums = new int[5][5];
		int n = 1;

		for (int i = 0; i < 5; i++) {
			if (i < 3) {
				for (int j = 2 - i; j < 3 + i; j++) {
					nums[i][j] = n;
					n++;
				}
			} else {
				for (int j = i - 2; j < 7 - i; j++) {
					nums[i][j] = n;
					n++;
				}
			}
		}

		// 출력 > 수정 없이 그대로 사용
		for (int[] row : nums) {
			for (int num : row) {
				System.out.printf("%5d", num);
			}
			System.out.println();
		}
	}
}

 

Q075 6번 문제

package com.test.question;

public class Q075 {
	public static void main(String[] args) {
		/*
        ● 요구사항
        주어진 규칙에 따라 아래와 같이 출력하시오.
        
        ● 출력 패턴
        1   2   3   4   10
        5   6   7   8   26
        9   10  11  12  42
        13  14  15  16  58
        28  32  36  40  136
        */

		int[][] nums = new int[5][5];

		// 데이터 입력 > 문제
		int n = 1; // 시작 숫자

		// 숫자 배열을 채우는 반복문
		for (int i = 0; i < nums.length - 1; i++) {
			for (int j = 0; j < nums.length - 1; j++) {
				nums[i][j] = n; // 현재 숫자를 배열에 채움
				n++; // 다음 숫자로 증가
				
				/*
				// 다른 방법
				nums[i][j] = n;
				nums[i][4] += n;
				nums[4][j] += n;
				nums[4][4] += n;
				n++;
				*/
			}
		}

		// 마지막 행과 마지막 열을 계산하여 채우는 반복문
		int rowSum, colSum;
		for (int i = 0; i < nums.length; i++) {
			rowSum = 0; // 행의 합을 초기화
			colSum = 0; // 열의 합을 초기화

			// 각 행과 열의 합을 구함
			for (int j = 0; j < nums.length - 1; j++) {
				rowSum += nums[i][j]; // 행의 합 계산
				colSum += nums[j][i]; // 열의 합 계산
			}

			// 행의 합을 배열에 채움
			nums[i][nums.length - 1] = rowSum;
			// 열의 합을 배열에 채움
			nums[nums.length - 1][i] = colSum;
		}

		// 마지막 행과 마지막 열의 합을 구하여 채우는 반복문
		int totalSum = 0;
		for (int i = 0; i < nums.length - 1; i++) {
			totalSum += nums[nums.length - 1][i]; // 마지막 행의 합 계산
		}
		// 마지막 행과 마지막 열의 합을 배열에 채움
		nums[nums.length - 1][nums.length - 1] = totalSum;

		/*
		// 다른 방법
		int[][] nums = new int[5][5];

		int n = 1;

		for (int i = 0; i < nums.length; i++) {
			for (int j = 0; j < nums[0].length; j++) {
				if (i < nums.length - 1 && j < nums[0].length - 1) {
					nums[i][j] = n;
					n++;
				} else {
					for (int k = 0; k < nums.length - 1; k++) {
						nums[i][j] += (i == nums.length - 1) ? nums[k][j] : nums[i][k];
					}
				}
			}
		}
		*/

		// 출력 > 수정 없이 그대로 사용
		for (int i = 0; i < nums.length; i++) {
			for (int j = 0; j < nums.length; j++) {
				System.out.printf("%5d", nums[i][j]); // 숫자 배열을 출력
			}
			System.out.println(); // 한 행이 끝나면 줄바꿈
		}
	}
}

 

Q077 7번 문제

package com.test.question;

public class Q077 {
	public static void main(String[] args) {
		/*
		● 요구사항
		아래와 같이 출력하시오.
		
		● 출력
		1	2	4	7	11
		3	5	8	12	16
		6	9	13	17	20
		10	14	18	21	23
		15	19	22	24	25
		*/
		
		int[][] nums = new int[5][5];
	    int n = 1;
		
	    // 데이터 입력
        for (int k = 0; k <= 8; k++) {
            for (int i = 0; i <= k; i++) {
                int j = k - i;
                if (i < 5 && j < 5) {
                    nums[i][j] = n++;
                }
            }
        }

        // 출력
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                System.out.printf("%5d", nums[i][j]);
            }
            System.out.println();
        }
    }
}