๐Ÿ“ Coding Test Study/C++

[10์ฃผ ์™„์„ฑ C++ ์ฝ”๋”ฉํ…Œ์ŠคํŠธ] ๊ธฐ๋ณธ๊ฐœ๋… :: ๋น„ํŠธ๋งˆ์Šคํฌ

ibelieveinme 2024. 5. 6. 14:32
728x90

1. ์ด์ง„์ˆ˜. Boolean ๋ฐฐ์—ด/๋ฒกํ„ฐ.

<ใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…กใ…ก

128 64 32 16 8 4 2 1

  1    0   1   1  0 0 0 0(176)

0001 0010 0011 0100 0101 0110 0111 1000
1 2 3 4 5 6 7 8

 

 

2. ๋น„ํŠธ ์—ฐ์‚ฐ์ž

& ๋น„ํŠธ๋‹จ์œ„๋กœ AND ์—ฐ์‚ฐ
| ๋น„ํŠธ๋‹จ์œ„๋กœ OR ์—ฐ์‚ฐ
^ ๋น„ํŠธ๋‹จ์œ„๋กœ XOR ์—ฐ์‚ฐ(๊ฐ™์œผ๋ฉด 0, ๋‹ค๋ฅด๋ฉด 1)
~ ํ”ผ์—ฐ์‚ฐ์ž์˜ ๋ชจ๋“  ๋น„ํŠธ ๋ฐ˜์ „
<< ํ”ผ์—ฐ์‚ฐ์ž์˜ ๋น„ํŠธ ์—ด์„ ์™ผ์ชฝ์œผ๋กœ ์ด๋™
>> ํ”ผ์—ฐ์‚ฐ์ž์˜ ๋น„ํŠธ ์—ด์„ ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™

 

* 10์ง„์ˆ˜ -> 2์ง„์ˆ˜ ๋ณ€ํ™˜ ์‚ฌ์ดํŠธ

 

Decimal to Binary Converter

Divide by the base 2 to get the digits from the remainders: Divisionby 2 Quotient Remainder(Digit) Bit #

www.rapidtables.com

 

* ๋น„ํŠธ ์—ฐ์‚ฐ ๊ณต์‹

a >> b = (int) a * (1/2)^b

a << b = a * 2^b

 

 

3. ๋น„ํŠธ์˜ ์Œ์ˆ˜ํ‘œํ˜„

: ๋น„ํŠธ ๋ฐ˜์ „ ํ•˜๊ณ  +1 ํ•œ ๊ฐ’

 

*๋น„ํŠธ ์Œ์ˆ˜์—ฐ์‚ฐ ๊ณต์‹

~a = -(a + 1)


ex) 0001000(8) ์Œ์ˆ˜๋งŒ๋“ค๊ธฐ:  0001000 --(๋น„ํŠธ๋ฐ˜์ „)--> 1110111 --(1๋”ํ•˜๊ธฐ)--> 1111000(120)

 

 

4. ๋น„ํŠธ ์—ฐ์‚ฐ์ž ํ™œ์šฉ๋ฒ•

idx ๋ฒˆ์งธ ๋น„ํŠธ๋„๊ธฐ S &= ~(1 << idx)
idx ๋ฒˆ์งธ ๋น„ํŠธ XOR ์—ฐ์‚ฐ S ^= ~(1 << idx)
์ตœํ•˜์œ„ ์ผœ์ ธ์žˆ๋Š” ๋น„ํŠธ ์ฐพ๊ธฐ idx = (S & -S)
ํฌ๊ธฐ๊ฐ€ n ์ธ ์ง‘ํ•ฉ์˜ ๋ชจ๋“  ๋น„ํŠธ ์ผœ๊ธฐ (1 << n) - 1
idx ๋ฒˆ์งธ ๋น„ํŠธ์ผœ๊ธฐ S |= (1 << idx)
idx ๋ฒˆ์งธ ๋น„ํŠธ๊ฐ€ ์ผœ์ ธ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ if(S & (1 << idx))

 

1) idx ๋ฒˆ์งธ ๋น„ํŠธ๋„๊ธฐ

ex. 10010 ์˜ 1๋ฒˆ์งธ ๋น„ํŠธ๋„๊ธฐ. 

10010 & ~(1 << 1) =  10010 & ~(10) = 10010 & 01 = 10000

 

2) idx ๋ฒˆ์งธ ๋น„ํŠธ๋ฅผ XOR ์—ฐ์‚ฐ์ž ์ทจํ•˜๊ธฐ

ex. 10010 ์˜ 0๋ฒˆ์งธ ๋น„ํŠธ๊ฐ€ 1์ด๋ฉด 0, 0์ด๋ฉด 1๋กœ ๋ฐ”๊พธ๊ธฐ(XOR ์—ฐ์‚ฐ)

10010 ^ ~(1 << 1) = 10010 ^ ~(10) = 10010 ^ 01 = 10011

 

3) ์ตœํ•˜์œ„ ์ผœ์ ธ์žˆ๋Š” ๋น„ํŠธ ์ฐพ๊ธฐ

cf. ์ตœํ•˜์œ„ ๋น„ํŠธ: 10010 ์ด๋ฉด 1๋ฒˆ์งธ 1, 10001 ์ด๋ฉด 0๋ฒˆ์งธ 1.

cf. ~S = -(S+1)

 

ex. 10010 ์—์„œ ์ตœํ•˜์œ„ ์ผœ์ ธ ์žˆ๋Š” ๋น„ํŠธ ์ฐพ๊ธฐ.

10010 & -10010 = 10010 & (~10010 + 1) = 10010 & (01101 + 1) = 10010 & 01110 = 00010

 

4) ํฌ๊ธฐ๊ฐ€ n์ธ ์ง‘ํ•ฉ์˜ ๋ชจ๋“  ๋น„ํŠธ์ผœ๊ธฐ

ex. n์ด 4์ธ ๋ชจ๋“  ๋น„ํŠธ์ผœ๊ธฐ.

(1 << 4) - 1 = 10000 - 1 = 1111

 

ex. n์ด 5์ธ ๋ชจ๋“  ๋น„ํŠธ์ผœ๊ธฐ.

(1 << 5) - 1 = 100000 - 1 = 11111

 

5) idx ๋ฒˆ์งธ ๋น„ํŠธ์ผœ๊ธฐ

ex. 10010 ์˜ 0๋ฒˆ์งธ ๋น„ํŠธ์ผœ๊ธฐ.

10010 | (1 << 0) = 10010 | (00001) = 10011

 

6) idx ๋ฒˆ์งธ ๋น„ํŠธ๊ฐ€ ์ผœ์ ธ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ

ex. 10010 ์˜ 3๋ฒˆ์งธ ๋น„ํŠธ๊ฐ€ ์ผœ์ ธ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ

if(10010 & (1 << 3)) = if(10010 & 1000) = if(00000) = 0 = ๊บผ์ ธ ์žˆ์Œ.

 

ex. 10010 ์˜ 1๋ฒˆ์งธ ๋น„ํŠธ๊ฐ€ ์ผœ์ ธ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ

if(10010 & (1 << 1)) = if(10010 & 10) = if(00010) = 1 = ์ผœ์ ธ ์žˆ์Œ.

 

 

5. ๋น„ํŠธ๋งˆ์Šคํ‚น, ๊ฒฝ์šฐ์˜ ์ˆ˜, ๋งค๊ฐœ๋ณ€์ˆ˜

*๋น„ํŠธ๋งˆ์Šคํ‚น

: boolean ๋ฐฐ์—ด ์—ญํ• ์„ ํ•˜๋Š” ํ•˜๋‚˜์˜ ์ˆซ์ž๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋น„ํŠธ ์—ฐ์‚ฐ์ž๋ฅผ ํ†ตํ•ด ํƒ์ƒ‰, ์ˆ˜์ • ๋“ฑ์˜ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋Š” ๊ฒƒ.

 

*๋น„ํŠธ๋งˆ์Šคํ‚น์„ ์ด์šฉํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜

Q. {์‚ฌ๊ณผ, ๋”ธ๊ธฐ, ํฌ๋„, ๋ฐฐ} ์˜ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜ ๊ตฌํ•˜๊ธฐ.

A. ์ด 16๊ฐ€์ง€ ๊ฒฝ์šฐ์˜ ์ˆ˜. (ํฌํ•จํ•˜๊ฑฐ๋‚˜ ํฌํ•จํ•˜์ง€ ์•Š๊ฑฐ๋‚˜์˜ ๊ฒฝ์šฐ์˜ ์ˆ˜๋กœ 2^4 = 16๊ฐ€์ง€๊ฐ€ ๋‚˜์˜ด)

#include <iostream>
using namespace std;

const int FRUITS_NUM = 4;

int main(){
    string fruits[FRUITS_NUM] = {"์‚ฌ๊ณผ", "๋”ธ๊ธฐ", "ํฌ๋„", "๋ฐฐ"};
    
    for(int i = 0; i < (1 << FRUITS_NUM); i++){ // 0000(0) ~ 1111(15) ๊นŒ์ง€ ๊ฒฝ์šฐ์˜ ์ˆ˜ ๊ตฌํ•˜๊ธฐ(10000 ๋ณด๋‹ค ์ž‘์€)
    	string result = "";
        for(int j = 0; j < FRUITS_NUM; j++){ // 0 ~ 3 ์ž๋ฆฌ๋ฅผ ๋Œ๋ฉด์„œ ์ผœ์ ธ ์žˆ๋Š” ๋น„ํŠธ ์ฐพ๊ธฐ
            if(i & (1 << j)){ // ์ผœ์ ธ ์žˆ๋‹ˆ ~?
                result += (fruits[j] + " ");
            }
        }
        cout << result << '\n';
    }
    return 0;
}

: ๋น„ํŠธ๋งˆ์Šคํ‚น 0 1 ๋กœ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ฒดํฌํ•˜๋ฉฐ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•œ๋‹ค.

 

๋ฐ”๊นฅ for๋ฌธ i

0 ~ 1111(10000๋ณด๋‹ค ์ž‘์„ ๋•Œ ๊นŒ์ง€) ๊นŒ์ง€ ์ˆ˜ ๋Š˜๋ฆฌ๊ธฐ. (0~15. 16๊ฐ€์ง€ ๊ฒฝ์šฐ์˜ ์ˆ˜)

 

์•ˆ์ชฝ for๋ฌธ j

0 ~ 3 ์ž๋ฆฌ๋ฅผ ๋Œ๋ฉฐ(4๋ณด๋‹ค ์ž‘์„ ๋•Œ ๊นŒ์ง€) ํ˜„์žฌ ์ž๋ฆฌ๊ฐ€ ์ผœ์ ธ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ

 

0 & (1 << 0) = 0000

0 & (1 << 1) = 0000

0 & (1 << 2) = 0000

0 & (1 << 3) = 0000

 

1 & (1 << 0) = 0001 & 0001 = 0001 ์‚ฌ๊ณผ

1 & (1 << 1) = 0001 & 0010 = 0000

1 & (1 << 2) = 0001 & 0100 = 0000

1 & (1 << 3) = 0001 & 1000 = 0000

 

2 & (1 << 0) = 0010 & 0001 = 0000

2 & (1 << 1) = 0010 & 0010 = 0010 ๋”ธ๊ธฐ

2 & (1 << 2) = 0010 & 0100 = 0000

2 & (1 << 3) = 0010 & 1000 = 0000

 

3 & (1 << 0) = 0011 & 0001 = 0001 ์‚ฌ๊ณผ

3 & (1 << 1) = 0011 & 0010 = 0010 ๋”ธ๊ธฐ

3 & (1 << 2) = 0011 & 0100 = 0000

3 & (1 << 3) = 0011 & 1000 = 0000

 

4 & (1 << 0) = 0100 & 0001 = 0000

4 & (1 << 1) = 0100 & 0010 = 0000

4 & (1 << 2) = 0100 & 0100 = 0100 ํฌ๋„

4 & (1 << 3) = 0100 & 1000 = 0000

 

5 & (1 << 0) = 0101 & 0001 = 0001 ์‚ฌ๊ณผ

5 & (1 << 1) = 0101 & 0010 = 0000

5 & (1 << 2) = 0101 & 0100 = 0100 ํฌ๋„

5 & (1 << 3) = 0101 & 1000 = 0000

 

6 & (1 << 0) = 0110 & 0001 = 0000

6 & (1 << 1) = 0110 & 0010 = 0010 ๋”ธ๊ธฐ

6 & (1 << 2) = 0110 & 0100 = 0100 ํฌ๋„

6 & (1 << 3) = 0110 & 1000 = 0000

 

7 & (1 << 0) = 0111 & 0001 = 0001 ์‚ฌ๊ณผ

7 & (1 << 1) = 0111 & 0010 = 0010 ๋”ธ๊ธฐ

7 & (1 << 2) = 0111 & 0100 = 0100 ํฌ๋„

7 & (1 << 3) = 0111 & 1000 = 0000

 

8 & (1 << 0) = 1000 & 0001 = 0000

8 & (1 << 1) = 1000 & 0010 = 0000

8 & (1 << 2) = 1000 & 0100 = 0000

8 & (1 << 3) = 1000 & 1000 = 1000 ๋ฐฐ

...

15 & (1 << 0) = 1111 & 0001 = 0001 ์‚ฌ๊ณผ

15 & (1 << 1) = 1111 & 0010 = 0010 ๋”ธ๊ธฐ

15 & (1 << 2) = 1111 & 0100 = 0100 ํฌ๋„

15 & (1 << 3) = 1111 & 1000 = 1000 ๋ฐฐ

728x90