2019년 9월 1일 일요일

[JAVA] 별 찍기 여러 문제

- 실행시킬 때는 해당 testNUM을 main으로 이름 바꾸기.

import static java.lang.System.out;

public class Ch04Ex08 {

public static void test01(String[] args) {
// 별 5*5 출력
for(int cnt=0; cnt<5; cnt++){
for(int i=0; i<=cnt; i++){
out.print('*');
}
out.println();
}

}// end of test01

public static void test02(String[] args) {
// 별 삼각형 찍기
for(int i=0; i<5; i++){
for(int j=5; j>i; j--){
out.print('*');
}//j
out.println();
}//i
}// end of test02

// 역삼각형
public static void test03(String[] args) {
// 0 : *****
// 1 : ^****
// 2 : ^^***
// 3 : ^^^**
// 4 : ^^^^*
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
out.print(j<i ? " " : "*");
}
out.println();
}
} // end of test03 역삼각형 찍기

public static void test04(String[] args) {
// 0 ^^^^* 공백이 찍히는 횟수 4 (4-i)
// 1 ^^^** 3
// 2 ^^*** 2
// 3 ^**** 1
// 4 ***** 0
for(int i=0; i<5; i++){
for(int j=4; j>=0; j--){
out.print(j>i ? " " : "*");
}
out.println();
}// 조건 연산식 이용한 코드
}// end of test04

public static void test05(String[] args) {
// *****
// ****
// ***
// **
// *
// **
// ***
// ****
// *****
int end = 5;

for(int i=0; i<9; i++){
for(int j=0; j<end; j++){
out.print("*");
}//j

out.println();

if(i<4){
end--;
}else{
end++;
}
}//for


}// end of test05

public static void test06(String[] args) {
// ^^^^*
// ^^^**
// ^^***
// ^****
// *****
// ^****
// ^^***
// ^^^**
// ^^^^*
int end = 4;
int star = 1;

for(int i=0; i<9; i++){
for(int j=0; j<end; j++){
out.print("^");
}
for(int k=0; k<star; k++){
out.print("*");
}
if(i<4){
end--;
star++;
}else{
end++;
star--;
}
out.println();
}

}// end of test06

public static void test07(String[] args) {
// ^^^^*
// ^^^***
// ^^*****
// ^*******
// *********
//  *******
//   *****
//    ***
//     *
int end = 4;
int star = 1;

for(int i=0; i<9; i++){
for(int j=0; j<end; j++){
out.print(" ");
}
for(int k=0; k<star; k++){
out.print("*");
}
if(i<4){
end --;
star += 2;
}else{
end ++;
star -= 2;
}
out.println();
}//for

}// end of test07

public static void test08(String[] args) {
// ********* 
// **** ****  빈칸 1~7 (0,1,3,5,7,5,3,1칸 씩)
// ***   ***
// **     **
// *       *
// **     **
// ***   ***
// **** ****
// *********
int star = 5;
int star2 = 4;
int blank = -1;

for (int i = 0; i < 9; i++) {

for (int j = star; j > 0; j--) {
out.print("*");
} // j : 왼쪽 별 5,4,3,2,1,2,3,4,5 찍기

if (i > 0) {
for (int k = 0; k < blank; k++) {
out.print("^");
} // k : blank
} // 첫 줄 비우고, 두 번째 줄 부터 빈 칸 찍기

if (i < 4) {
star--;
blank += 2;
} else {
star++;
blank -= 2;
}

for (int x = star2; x > 0; x--) {
out.print("*");
} // x : 오른쪽 별 4,3,2,1,2,3,4 찍기

if (i > 0 && i < 7) {
star2 = (i < 4) ? (star2 -= 1) : (star2 += 1);

}// 오른쪽 별  : 찍히는 개수의 조건 연산자(첫째 줄과 마지막 줄 빼고)

out.println();
} // for : 10번 반복


}//end of test08
}

댓글 없음:

댓글 쓰기

[프로그래머스] 프린터 (자바/Java)

문제 설명 일반적인 프린터는 인쇄 요청이 들어온 순서대로 인쇄합니다. 그렇기 때문에 중요한 문서가 나중에 인쇄될 수 있습니다. 이런 문제를 보완하기 위해 중요도가 높은 문서를 먼저 인쇄하는 프린터를 개발했습니다. 이 새롭게 개발한 프린터는 아래와 같은...