반복문 (23.04.22)

·2023년 4월 23일
0

Coding Test

목록 보기
6/39
post-thumbnail

✏️ 문제 1

사용자로부터 입력받은 숫자의 단을 출력하세요.
숫자 : 4
===== 4단 =====
4 \* 1 = 4
4 \* 2 = 8
4 \* 3 = 12
4 \* 4 = 16
4 \* 5 = 20
4 \* 6 = 24
4 \* 7 = 28
4 \* 8 = 32
4 \* 9 = 36

(1) 풀이

		Scanner sc = new Scanner(System.in);
		
		System.out.print("숫자 : ");
		int dan = sc.nextInt();
		System.out.println("===== " + dan + "단 =====");
		
		for(int i = 1; i <= 9; i++) {
			System.out.printf("%d * % d = % d\n", dan, i, dan * i);
		}

✏️ 문제 2

사용자로부터 입력받은 숫자의 단부터 9단까지을 출력하세요.
단, 2~9 사이가 아닌 수를 입력하면 "2~9 사이 숫자만 입력해 주세요."를 출력하세요.

숫자 : 4
===== 4단 =====
===== 5단 =====
===== 6단 =====
===== 7단 =====
===== 8단 =====
===== 9단 =====
(해당 단의 내용들은 길이 상 생략)

숫자 : 10
2~9 사이 숫자만 입력해 주세요.

(1) 풀이

		Scanner sc = new Scanner(System.in);
		
		System.out.print("숫자 : ");
		int dan = sc.nextInt();
		
		if(dan >=2 && dan <= 9) {
			for(int x = dan; x <= 9; x++) {
				System.out.println("===== " + x + "단 =====");
				
				for(int i = 1; i <= 9; i++) {
					System.out.printf("%d * %d = %d\n", x, i, x * i);
				}
	
			}
			
		} else {
			System.out.println("2~9 사이 숫자만 입력해 주세요.");
		}

✏️ 문제 3

다음과 같은 실행 예제를 구현하세요.

정수 입력 : 4

*
**
***
****

(1) 풀이

		Scanner sc = new Scanner(System.in);
		
		System.out.printf("정수 입력 : ");
		int input = sc.nextInt();

		for(int x = 1; x <= input; x++) {
			for(int i = 1; i <= x; i++) {
				System.out.print("*");
			}
			System.out.println();
		}

✏️ 문제 4

다음과 같은 실행 예제를 구현하세요.

정수 입력 : 4

****
***
**
*

(1) 풀이

		Scanner sc = new Scanner(System.in);

		System.out.print("정수 입력 : ");
		int input = sc.nextInt();
		
		for(int x = input; x >= 1; x--) {
			for(int i = x; i >= 1; i--) {
				System.out.print("*");
			}
			System.out.println();
		}

✏️ 문제 5

다음과 같은 실행 예제를 구현하세요.

정수 입력 : 4

   *
  **
 ***
****

(1) 내 풀이

		Scanner sc = new Scanner(System.in);
		
		System.out.print("정수 입력 : ");
		int input = sc.nextInt();
		
		for(int x = input; x >= 1; x--) {
			
			for(int i = 1; i <= input; i++) {
				if(x > i) {
					System.out.print(" ");
					
				}else {
					System.out.print("*");
					
				}
			}
			
			System.out.println();
		}

(2) 다른 풀이

		Scanner sc = new Scanner(System.in);
		
		System.out.print("정수 입력 : ");
		int input = sc.nextInt();
		
		for(int x = 1; x <= input; x++) {
			
			// 1) for문 하나 더 작성
			// * 1개 출력 전에 띄어쓰기 3번
			// * 2개 출력 전에 띄어쓰기 2번
			// * 3개 출력 전에 띄어쓰기 1번
			// * 4개 출력 전에 띄어쓰기 0번
			
			// y == 3 2 1 0
			
			/*
			for(int y = input - x; y >= 1; y--) {
				System.out.print(" ");
			}
			
			for(int i = 1; i <= x; i++) {
				System.out.print("*");
			}
			System.out.println(); // 줄바꿈
			*/
			
			// 2) for + if - else
			
			for(int i = 1; i <= input; i++) {
			
				if(i <= input - x) {
					System.out.print(" ");
				} else {
					System.out.print("*");
				}
			}
			System.out.println(); // 줄바꿈
		}

✏️ 문제 6

다음과 같은 실행 예제를 구현하세요.

정수 입력 : 3

*
**
***
**
*

(1) 풀이

		Scanner sc = new Scanner(System.in);
	      
	    System.out.print("정수 입력 : ");
	    int input = sc.nextInt();
	     
	    // 삼각형이 2개가 있다고 가정(위쪽 삼각형: 증가 / 아랫쪽 삼각형: 감소)
	    // 위쪽 삼각형
	    for(int x = 1; x <= input; x++) {
	    	
	    	for(int i = 1; i <= x; i++) {
	    		System.out.print("*");
	        }
	    	System.out.println(); // 줄바꿈
	    }

	    // 아랫쪽 삼각형
	    for(int y = (input - 1); y >= 1; y--) {
	    	
	    	for(int i = 1; i <= y; i++) {
	    		System.out.print("*");
	        }
	    	System.out.println(); // 줄바꿈
	    }

✏️ 문제 7 - 피라미드 별 찍기

다음과 같은 실행 예제를 구현하세요.

정수 입력 : 4

   *
  ***
 *****
*******

(1) 내 풀이

		Scanner sc = new Scanner(System.in);
		
		System.out.print("정수 입력 : ");
		int input = sc.nextInt();
		
		for(int x = 1; x <= input; x++) {
			
			for(int y = input; y > x; y--) {
				System.out.print(" ");
			}
			
			for(int y = 1; y <= 2 * x - 1; y++) {
				System.out.print("*");
			}
			
			System.out.println();
		}

(2) 다른 풀이

		Scanner sc = new Scanner(System.in);
	      
	    System.out.print("정수 입력 : ");
	    int input = sc.nextInt();
	      
	    for(int x = 1; x <= input; x++) { // 입력받은 input만큼 줄 출력
	    	
	    	// 공백 출력 for문
	    	for(int i = input - x; i >= 1; i--) {
	    		System.out.print(" ");
	    	}
	    	
	    	// * 출력 for문
	    	// 1, 3, 5, 7, 9
	    	for(int i = 1; i <= 2 * x - 1; i++) {
	    		System.out.print("*");
	    	}
	    	
	    	System.out.println(); // 줄바꿈
	    }

✏️ 문제 8 - 피라미드 네모 찍기

다음과 같은 실행 예제를 구현하세요.

정수 입력 : 5

*****
*   *
*   *
*   *
*****

(1) 풀이

		Scanner sc = new Scanner(System.in);
		
		System.out.print("정수 입력 : ");
		int input = sc.nextInt();
		
		for(int i = 1; i <= input; i++) {
			System.out.print("*");
		}
		System.out.println();

		for(int i = 1; i <= input - 2; i++) {
			
			for(int x = 1; x <= input; x++){
				
				if( x == 1 || x == input) {
					System.out.print("*");
				} else {
					System.out.print(" ");
				}
			}
			
			System.out.println();
		}
		
		for(int i = 1; i <= input; i++) {
			System.out.print("*");
		}

(2) 다른 풀이

		Scanner sc = new Scanner(System.in);
	      
		System.out.print("정수 입력 : ");
		int input = sc.nextInt();
	      
		// row : 행(줄)
		// col(column) : 열(칸)
		
		for(int row = 1; row <= input; row++) {
			
			for(int col = 1; col <= input; col++) {
				// row 또는 col이 1 또는 input인 경우 * 출력
				// == 테두리
				
				if(row == 1 || row == input || col == 1 || col == input) {
					System.out.print("*");
					
				} else { // 내부
					System.out.print(" ");
				}
				
			}
			System.out.println(); // 줄바꿈
		}
profile
풀스택 개발자 기록집 📁

0개의 댓글