알고리즘/기타퀴즈
Quiz16, Quiz17 (중첩반복문)
pitang
2021. 8. 26. 23:18
반응형
안녕하세요. pitang입니다.
중첩 반복문에 관한 퀴즈 바로 가볼게요!
Quiz16
console을 보고 그대로 출력하세요.
<console>
1번 문제
*
**
***
****
*****
------------------------
2번 문제
*****
****
***
**
*
------------------------
3번 문제
*
***
*****
*******
**********
⬇️⬇️⬇️ 정답은 더보기를 눌러주세요 ⬇️⬇️⬇️
더보기
public class Quiz16 {
public static void main(String[] args) {
System.out.println("1번 문제");
for(int i = 1; i <= 5; i++) {
System.out.println();
for(int j = 1; j <= i; j++) {
System.out.print("*");
}
}
System.out.println("\n-----------------------");
System.out.println("2번 문제");
for(int i = 1; i <= 5; i++) {
System.out.println();
for(j = 5; j >= i; j--) {
System.out.print("*");
}
}
System.out.println("\n-----------------------");
System.out.println("3번 문제");
for(int i = 1; i <= 5; i++) {
System.out.println();
for(int j = 1; j <= 5-i; j++) {
System.out.print(" ");
}
for(int j = 1; j < 2*i - 1; j++) {
System.out.print("*");
}
}
}
}
5를 int star = 5; 로 선언해 변수를 써서 사용할 수도 있다.
Quiz17
3x3구구단을 출력하세요.
3x3구구단
1 x 1 = 1 2 x 1 = 2 3 x 1 = 3
1 x 2 = 2 2 x 2 = 4 3 x 2 = 6
1 x 3 = 3 2 x 3 = 6 3 x 3 = 9
1 x 4 = 4 2 x 4 = 8 3 x 4 = 12
1 x 5 = 5 2 x 5 = 10 3 x 5 = 15
1 x 6 = 6 2 x 6 = 12 3 x 6 = 18
1 x 7 = 7 2 x 7 = 14 3 x 7 = 21
1 x 8 = 8 2 x 8 = 16 3 x 8 = 24
1 x 9 = 9 2 x 9 = 18 3 x 9 = 27
4 x 1 = 4 5 x 1 = 5 6 x 1 = 6
4 x 2 = 8 5 x 2 = 10 6 x 2 = 12
4 x 3 = 12 5 x 3 = 15 6 x 3 = 18
4 x 4 = 16 5 x 4 = 20 6 x 4 = 24
4 x 5 = 20 5 x 5 = 25 6 x 5 = 30
4 x 6 = 24 5 x 6 = 30 6 x 6 = 36
4 x 7 = 28 5 x 7 = 35 6 x 7 = 42
4 x 8 = 32 5 x 8 = 40 6 x 8 = 48
4 x 9 = 36 5 x 9 = 45 6 x 9 = 54
7 x 1 = 7 8 x 1 = 8 9 x 1 = 9
7 x 2 = 14 8 x 2 = 16 9 x 2 = 18
7 x 3 = 21 8 x 3 = 24 9 x 3 = 27
7 x 4 = 28 8 x 4 = 32 9 x 4 = 36
7 x 5 = 35 8 x 5 = 40 9 x 5 = 45
7 x 6 = 42 8 x 6 = 48 9 x 6 = 54
7 x 7 = 49 8 x 7 = 56 9 x 7 = 63
7 x 8 = 56 8 x 8 = 64 9 x 8 = 72
7 x 9 = 63 8 x 9 = 72 9 x 9 = 81
⬇️⬇️⬇️ 정답은 더보기를 눌러주세요 ⬇️⬇️⬇️
더보기
public class Quiz17 {
public static void main(String[] args) {
System.out.println("3x3 구구단");
for(int i = 1; i <= 9; i += 3) {
for(int j = 1; j <= 9; j++) {
System.out.printf("%d x %d = %d", i, j, i*j + "\t");
System.out.printf("%d x %d = %d", (i+1), j, (i+1)*j + "\t");
System.out.printf("%d x %d = %d", (i+2), j, (i+2)*j);
System.out.println();
}
System.out.println();
}
}
}
\t는 8칸을 띄울 수 있다.
감사합니다.
*m1맥북을 사용 중입니다.*
728x90
반응형