ํ์ ๊ฐฏ์๋ฅผ ์
๋ ฅ๋ฐ์์ ๋ค์ํ ๋ชจ์์ผ๋ก ์ฝ์์ ๋ณ์ ์ฐ์ด ๋ณด์!
1. ๊ธฐ๋ณธ ๋ณ์ฐ๊ธฐ
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("ํ์ ๊ฐ์๋ฅผ ์
๋ ฅํ์ธ์: ");
int rows = scanner.nextInt();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
2. ์ผ์ชฝ ์ผ๊ฐํ ๋ณ ์ฐ๊ธฐ
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("ํ์ ๊ฐ์๋ฅผ ์
๋ ฅํ์ธ์: ");
int rows = scanner.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = 0; j < i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
3. ์ค๋ฅธ์ชฝ ์ผ๊ฐํ ๋ณ ์ฐ๊ธฐ
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("ํ์ ๊ฐ์๋ฅผ ์
๋ ฅํ์ธ์: ");
int rows = scanner.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = rows; j > 0; j--) {
if (i < j) {
System.out.print(" ");
} else {
System.out.print("*");
}
}
System.out.println();
}
}
}
4. ์ญ์ผ๊ฐํ ๋ณ ์ฐ๊ธฐ
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("ํ์ ๊ฐ์๋ฅผ ์
๋ ฅํ์ธ์: ");
int rows = scanner.nextInt();
for (int i = 0; i < rows; i++) {
for (int j = rows; j > 0; j--) {
if (i < j) {
System.out.print("*");
} else {
System.out.print("");
}
}
System.out.println();
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("ํ์ ๊ฐ์๋ฅผ ์
๋ ฅํ์ธ์: ");
int rows = scanner.nextInt();
for (int i = 1; i <= rows; i++) {
for (int j = rows; j > i; j--) {
System.out.print("*");
}
System.out.println();
}
}
}
5. ์ค๋ฅธ์ชฝ ์ญ์ผ๊ฐํ ๋ณ ์ฐ๊ธฐ(๊ณต๋ฐฑ ํฌํจ ๋ณ์ฐ๊ธฐ)
์ฌ๊ธฐ์๋ถํฐ ๋์ด๋ ์ค์ โ
โ
โ
์ญ์ผ๊ฐํ ๋ณ์ฐ๊ธฐ์์ ์์ ๊ณต๋ฐฑ ๋ถ๋ถ์ ์๋ณด์ฌ์ ๋์('-')๋ก ๋์ฒดํ์ต๋๋ค.
package Baekjoon_test;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("ํ์ ๊ฐ์๋ฅผ ์
๋ ฅํ์ธ์: ");
int rows = scanner.nextInt();
for (int i = 0; i < rows; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("-");
}
for (int k = 0; k < rows - i; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
5. ํผ๋ผ๋ฏธ๋(๊ณต๋ฐฑ ํฌํจ ๋ณ์ฐ๊ธฐ)
๋์ด๋ ์ โ
โ
โ
โ
์์ ๊ณต๋ฐฑ์ ์ ์๋ณด์ฌ์ ๋์('-')๋ก ๋์ฒดํ์ต๋๋ค.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("ํ์ ๊ฐ์๋ฅผ ์
๋ ฅํ์ธ์: ");
int rows = scanner.nextInt();
for (int i = 0; i < rows; i++) {
for (int j = 0; j < rows - i - 1; j++) { // ๊ณต๋ฐฑ
System.out.print("-");
}
for (int k = 0; k < i * 2 + 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
6. ๋ค์ด์๋ชฌ๋ ๋ชจ์(๊ณต๋ฐฑ ํฌํจ ๋ณ์ฐ๊ธฐ)
๋๋ง์ ์๋ฆ๋ค์ด ๋ณ์ฐ๊ธฐ ใ
ใ
ใ
ใ
ใ
์ฌ๊ธฐ์๋ถํฐ ๋์ด๋ ์ต์ โ
โ
โ
โ
โ
์์ ๊ณต๋ฐฑ์ ์ ์๋ณด์ฌ์ ๋์('-')๋ก ๋์ฒดํ์ต๋๋ค.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
for( int i = 0; i < 5; i++) {
for (int j = 1; j<5-i ; j++) {
System.out.print(" ");
}
for (int k = 0; k < i*2+1 ; k++) {
System.out.print("*");
}
System.out.println("");
}
for (int a = 1; a<5; a++) {
for (int b = 5; b > 5-a; b--) {
System.out.print(" ");
}
for (int c = 10; c > a*2+1; c--) {
System.out.print("*");
}
System.out.println("");
}
}
}
ํ ์(์ค ์) ์
๋ ฅ๋ฐ์์ ๋ค์ด์๋ชฌ๋ ๋ณ์ฐ๊ธฐ โผ
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("ํ์ ๊ฐ์๋ฅผ ์
๋ ฅํ์ธ์: ");
int rows = scanner.nextInt();
// ์ ๋ถ๋ถ ์ถ๋ ฅ
for (int i = 0; i < rows; i++) {
for (int j = 1; j < rows - i; j++) {
System.out.print(" ");
}
for (int k = 0; k < i * 2 + 1; k++) {
System.out.print("*");
}
System.out.println("");
}
// ์๋ซ ๋ถ๋ถ ์ถ๋ ฅ
for (int a = 1; a < rows; a++) {
for (int b = 0; b < a; b++) {
System.out.print(" ");
}
for (int c = 0; c < (rows - a) * 2 - 1; c++) {
System.out.print("*");
}
System.out.println("");
}
}
}
7. ๊ทธ ์ธ ๋ค์ํ ๋ชจ์์ผ๋ก ๋ณ์ฐ๊ธฐ(๋ค๋ฅธ ๋ธ๋ก๊ทธ ํฌ์คํ ๋ถ๋งํฌ)
โผ ํํธ ๋ชจ์, ์ก๊ฐ ๋ณ ๋ชจ์, ์ค๊ฐ ๋ณ ๋ชจ์ ๋ณ ์ฐ๊ธฐ โผ
โผ ์ฐฝ๋ฌธ ๋ชจ์ ๋ณ์ฐ๊ธฐ โผ
'Algorithm > BaekJoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค] 10952๋ฒ : A+B - 5 ( ์๋ฐ / java ) (0) | 2024.05.08 |
---|---|
[๋ฐฑ์ค] 2438๋ฒ : ๋ณ ์ฐ๊ธฐ - 2 ( ์๋ฐ / java ) (0) | 2024.04.23 |
[๋ฐฑ์ค] 2438๋ฒ : ๋ณ ์ฐ๊ธฐ - 1 ( ์๋ฐ / java ) (0) | 2024.04.23 |
[๋ฐฑ์ค] 11022๋ฒ : A+B-8 ์๋ฐ / java (1) | 2024.04.23 |
[๋ฐฑ์ค] 11021๋ฒ : A+B-7 ์๋ฐ / java (0) | 2024.04.23 |