알고리즘

김덕근·2022년 12월 12일
0

algorithm

목록 보기
1/12
		public void example2() {
			
			Scanner sc = new Scanner(System.in);
			
			System.out.print("정수 입력 : ");
			int input = sc.nextInt();
			
			for(int row = 1; row <= input; row++) {
				for(int col = 1; col <= input * 2 - 1; col++) {
					// 마지막 줄의 별 갯수: input에 2를 곱한값의 -1
					//          row    col ||         row    col
					// 1) 4   -  1  >=  1  ||   4   +  1  <=  1 
					// 1) 4   -  1  >=  2  ||   4   +  1  <=  2 
					// 1) 4   -  1  >=  3  ||   4   +  1  <=  3 
					// 1) 4   -  1  >=  4  ||   4   +  1  <=  4  
                    // 1) 4   -  1  >=  5  ||   4   +  1  <=  5
                    // 1) 4   -  1  >=  6  ||   4   +  1  <=  6
                    // 1) 4   -  1  >=  7  ||   4   +  1  <=  7
                    
					if( input - row >= col || input + row <= col) {
						System.out.print(" ");
					} else {
						System.out.print("*");
					}
				}
				System.out.println();
			}
		}

중요!

input - row >= col || input + row <= col

profile
안녕하세요!

0개의 댓글