public class Test {
static void triangleLB(int a) { // Left-Bottom
for (int i = 1; i <= a; i++) {
for (int j = 1; j <= i; j++) {
System.out.printf("%3s", "*");
}
System.out.println();
}
}
static void triangleLU(int a) {
for (int i = a; i > 0; i--) {
for (int j = 1; j <= i; j++) {
System.out.printf("%3s", "*");
}
System.out.println();
}
}
static void triangleRU(int a) {
for (int i = 0; i < a; i++) {
for (int j = 0; j < a; j++) {
if( i > j ) {
System.out.print(" ");
}else {
System.out.print(" *");
}
}
System.out.println();
}
}
static void triangleRB(int a) {
for (int i = a; i > 0; i--) {
for (int j = 1; j <= a; j++) {
if(j >= i) {
System.out.print(" *");
}else {
System.out.print(" ");
}
}
System.out.println();
}
}
static void spira(int a) {
for (int i = 1; i <= a; i++) {
int x = (i-1)*2+1;
int y = (a-1)*2+1;
if(i == a) {
for (int j = 0; j < y; j++) {
System.out.printf("%3s", "*");
}
}else {
for (int j = 0; j < a-i; j++) {
System.out.printf("%3s", " ");
}
for (int j = 0; j < x; j++) {
System.out.printf("%3s", "*");
}
}
System.out.println();
}
}
static void spira2(int a) {
int n = 0;
int j = 0;
int x = 0;
for (int i = 1; i <= a; i++) {
n = i + j;
j++;
x = a - i;
for (int k = 0; k < x; k++) {
System.out.print(" ");
}
for (int k = 0; k < n; k++) {
System.out.printf("%3s","*");
}
System.out.println();
}
}
public static void main(String[] args) {
triangleLB(10);
System.out.println();
triangleLU(10);
System.out.println();
triangleRU(10);
System.out.println();
triangleRB(10);
System.out.println();
spira(5);
System.out.println();
spira2(5);
}
}
'Main > Algorithm' 카테고리의 다른 글
[알고리즘 초급] 10진수를 2진수, 8진수, 10진수, 16진수로 변환 (0) | 2020.01.06 |
---|---|
[알고리즘 초급] 구구단 (0) | 2020.01.03 |
드모르간 법칙 (0) | 2020.01.03 |
논리곱, 논리합 (0) | 2020.01.03 |
가우스의 덧셈 "1 부터 100까지의 합" (0) | 2020.01.03 |