본문 바로가기
Algorithm/BaekJoon[Java]

백준 2438, 2439, 2440, 2441, 2442, 2445, 2446 (별찍기)

by 씨니 2022. 3. 17.
728x90

▶2438번 문제

 

▶풀이방법

import java.util.*;

public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int num = sc.nextInt();
		
		for(int i = 0; i < num; i++) {
			for(int j = 0 ; j <= i; j++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

 

 

▶2439번 문제

 

▶풀이방법

import java.util;

public class Main{
	public static void main(String[] args) {
		int num = new Scanner(System.in).nextInt();
		for(int i = 0; i < num; i++) {
			for(int z = 1; z < num - i; z++) {
				System.out.print(" ");
			}
			for(int j = 0; j <= i; j++) {
				System.out.print("*");
			}
			
			System.out.println();
		}
	}
}

 

 

▶2440번 문제

 

▶풀이방법

import java.util.*;

public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int num = sc.nextInt();
		for(int i = 0; i <= num; i++) {
			for(int j = num; j > i; j-- ) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

 

▶2441번 문제

 

▶풀이방법

import java.util.*;

public class Main{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int num = sc.nextInt();
		
		for(int i = 0; i < num; i++) {
			for(int j = 0; j < i; j++ ) {
				System.out.print(" ");
			}
			for(int z = num; z > i; z--) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

 

 

▶2442번 문제

 

▶풀이방법

import java.util.*;

public class Main{
	public static void main(String[] args) {
		int num = new Scanner(System.in).nextInt();
		
		for(int i = 0; i < num; i++ ) {
			for(int j = num; j > i+1; j--) {
				System.out.print(" ");
			}
			for(int z = 0; z < i*2+1; z++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

 

 

▶2445번 문제

 

▶풀이방법1

import java.util.*;

public class Main{
	public static void main(String[] args) {
		int num = new Scanner(System.in).nextInt();
		//윗줄
		for(int i = 1; i <= num; i++) {
			for(int j = 1; j <= i; j++) {
				System.out.print("*");
			}
			for(int z = 0; z < 2*(num-i); z++) {
				System.out.print(" ");
			}
			for(int k = 1; k <= i; k++) {
				System.out.print("*");
			}
			System.out.println();
		}
		//아래줄
		for(int i = 1; i <= num-1; i++) {
			for(int j = 1; j <= num-i; j++) {
				System.out.print("*");
			}
			for(int z = 0; z < 2*i; z++) {
				System.out.print(" ");
			}
			for(int k = 1; k <= num-i ; k++) {
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

 

▶풀이방법2

import java.util.*;

public class Main{
	static void printstars(int a, int b){ //반복문 함수
		for(int j = 1; j <= a; j++) {
			System.out.print("*");
		}
		for(int z = 0; z < b; z++) {
			System.out.print(" ");
		}
		for(int k = 1; k <= a; k++) {
			System.out.print("*");
		}
		System.out.println();
	}
	
	public static void main(String[] args) {
		int num = new Scanner(System.in).nextInt();
		for(int i = 1; i <= num; i++) printstars(i, 2*(num-i)); //윗줄
		for(int i = 1; i <= num-1; i++) printstars(num-i, 2*i); //아래줄
	}
}

 

 

▶2446번 문제

 

▶풀이방법

import java.util.*;

public class Main{
	static void printstars(int a, int b) {
		for(int j = 1; j < a ; j++) {
			System.out.print(" ");
		}
		for(int z = 1; z < b; z++) {
			System.out.print("*");
		}
		System.out.println();
	}
	public static void main(String[] args) {
		int num = new Scanner(System.in).nextInt();
		
		for(int i = 0; i < num; i++) printstars(i+1, 2*(num-i));
		for(int i = 1; i < num; i++) printstars(num-i, (i*2)+2);
	}
}
728x90

'Algorithm > BaekJoon[Java]' 카테고리의 다른 글

백준 1463(Dynamic Programming)  (0) 2022.03.19
백준 10991, 10992 (별찍기)  (0) 2022.03.18
백준 10818  (0) 2022.03.17
백준 8393  (0) 2022.03.17
백준 1924  (0) 2022.03.17