๊ตญ๋น์ง์ D+10
- ์ฝ๋ฉ๋ฐ์ด -
์ฝ๋ฉ๋ฐ์ด
Q) ์ด์ค for๋ฌธ์ 2๋จ๋ถํฐ 9๋จ๊น์ง ์ถ๋ ฅํ๋ ์ฝ๋๋ฅผ ์์ฑํ์์ค.
public class Gugudan {
public static void main(String[] args)
{
// Q) ์ด์ค for๋ฌธ์ 2๋จ๋ถํฐ 9๋จ๊น์ง ์ถ๋ ฅํ๋ ์ฝ๋๋ฅผ ์์ฑํ์์ค.
int dan = 2; //๋จ์
int mul = 1; // ๊ณฑ์
// 2๋จ ~ 9๋จ ๊น์ง ์ถ๋ ฅํ๊ธฐ ์ํ for
for(int i = 0; i < 8; i++)
{
System.out.println(dan + "๋จ");
// ๊ณฑํ๋์ 9๋ฒ ๋๊ธฐ์ํ for
for(int j = 0; j < 9; j++)
{
System.out.println(dan + " x " + mul + " = " + (dan * mul));
mul++;
}
System.out.println("-------------");
dan++;
mul = 1;
}
}
}
์ถ๋ ฅ๊ฒฐ๊ณผ
2๋จ
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
2 x 6 = 12
2 x 7 = 14
2 x 8 = 16
2 x 9 = 18
-------------
3๋จ
3 x 1 = 3
3 x 2 = 6
3 x 3 = 9
3 x 4 = 12
3 x 5 = 15
3 x 6 = 18
3 x 7 = 21
3 x 8 = 24
3 x 9 = 27
-------------
4๋จ
4 x 1 = 4
4 x 2 = 8
4 x 3 = 12
4 x 4 = 16
4 x 5 = 20
4 x 6 = 24
4 x 7 = 28
4 x 8 = 32
4 x 9 = 36
-------------
5๋จ
5 x 1 = 5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
-------------
6๋จ
6 x 1 = 6
6 x 2 = 12
6 x 3 = 18
6 x 4 = 24
6 x 5 = 30
6 x 6 = 36
6 x 7 = 42
6 x 8 = 48
6 x 9 = 54
-------------
7๋จ
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
-------------
8๋จ
8 x 1 = 8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
-------------
9๋จ
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
-------------
Q) ๋ก๋๋ฒํธ๋ฅผ ์ถ๋ ฅํ๋ ํจ์๋ฅผ ๋ง๋ค์ด ๋ณด์์ค.
public class Lotto {
public static void genLotto(int[] lotto)
{
int index = 0; // ์ง์ด๋ฃ์ ์์น์
int random;
boolean overlapFlag = false;
while(index < 6)
{
// ๋๋ค๋๋ฒ ๊ตฌํ
random = (int)(Math.random() * 45) + 1;
overlapFlag = false;
// ๊ฒน์น๋๊ฒ ์๋์ง ํ์ธํ๋ค.
for(int i = 0; i < 6; i ++)
{
if(random == lotto[i])
{
// ๊ฒน์น๋ ๊ฐ์ด ์กด์ฌํ๋ค.
overlapFlag = true;
break;
}
}
// ๊ฒน์น๋๊ฒ ์๋ค.
if(overlapFlag == false)
{
lotto[index] = random;
index++;
}
}
}
public static void main(String[] args)
{
// Q) ๋ก๋๋ฒํธ๋ฅผ ์ถ๋ ฅํ๋ ํจ์๋ฅผ ๋ง๋ค์ด ๋ณด์์ค.
// ๋ก๋๋ฒํธ๋ฅผ ์ ์ฅํ ๋ฐฐ์ด์ ์ ์ธ.
int[] lotto = new int[6];
genLotto(lotto); // ๋น๋ฐฐ์ด 6๊ฐ ์ ์ฅ์ํ
for(int i = 0; i < 6; i++)
{
System.out.println(lotto[i]);
}
}
}
์ถ๋ ฅ๊ฒฐ๊ณผ
3
29
30
18
25
1
Q) ์คํ์ ํํํ์์ค.
public class Stack {
public static boolean push(int[] stack, int[] si, int value)
{
// 1. ์คํ์ค๋ฒํ๋ก์ฐ ์ํ๋ผ๋ฉด return false;
if(si[1] == 10)
{
System.out.println("Stack-overflow");
return false;
}
// 2. ์คํํฌ์ธํฐ ์์น์ ๋ฐ์ดํฐ ์ง์ด๋ฃ๋๋ค.
stack[si[0]] = value;
//3. ์คํํฌ์ธํฐ, ์คํ์นด์ดํธ ์ฆ๊ฐ
si[0]++;
si[1]++;
return true;
}
public static boolean pop(int[] stack, int[] si)
{
// 1. ์คํ์ธ๋ํ๋ก์ฐ ์ํ๋ผ๋ฉด return false;
if(si[1] == 0) {
System.out.println("Stack-underflow");
return false;
}
// 2. si[2] ๋ฐ์ดํฐ ์ ์ฅํ (์คํํฌ์ธํฐ - 1) ์์น์ ๋ฐ์ดํฐ๋ฅผ ๋บธ๋ค.
si[2] = stack[si[0] - 1];
stack[si[0] - 1] = 0;
//3. ์คํํฌ์ธํฐ, ์คํ์นด์ดํธ ๊ฐ
si[0]--;
si[1]--;
return true;
}
public static void printStack(int[] stack, int[] si)
{
if(si[1] == 0) {
System.out.println("์ถ๋ ฅํ ์ ์๋ ๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.");
}
int index = si[0] - 1;
for(int i = 0; i < si[1]; i++)
{
System.out.println(stack[index]);
System.out.println("-----");
index--;
}
}
public static void main(String[] args) {
// Q) ์คํ์ ํํํ์์ค.
// 10๊ฐ์ง๋ฆฌ ๋ฐฐ์ด ์ ์ธ
int[] stack = new int[10];
// [0] : stack potiner
// [1] : stack counter
// [2] : poped data
int[] stackInfo = new int[3];
push(stack, stackInfo, 1);
push(stack, stackInfo, 3);
printStack(stack, stackInfo);
}
}
์ถ๋ ฅ๊ฒฐ๊ณผ
3
------
1
------
๋ฐ์ํ
'์๋ > ๊ตญ๋น์ง์' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[D+12] class ํด๋์ค (1) | 2022.10.05 |
---|---|
[D+11] ์ฐธ์กฐํ์ (0) | 2022.10.04 |
[D+09] ์ปดํจํฐ์์ ๋ฉ๋ชจ๋ฆฌ๋ฅผ ์ด๋ป๊ฒ ์ฌ์ฉํ๋๊ฐ? (0) | 2022.09.30 |
[D+08] ํจ์, ๋๋ฒ๊ฑฐ (0) | 2022.09.28 |
[D+07] ๊ฐ๋ฐํ๊ฒฝ ๊ตฌ์ถ (0) | 2022.09.28 |
๋๊ธ