본문 바로가기
코딩테스트/알고리즘 기초 강의

3강 수학 1 - 참고

by 의정부핵꿀밤 2020. 8. 20.
728x90

-진법 변환

10진수 N을 B진법으로 바꾸려면 N이 0이 될때까지 나머지를 계속해서 구하면 된다. 

<11005번 / 진법 변환 2>

난 나머지를 모두 string배열로 변환해서 저장하고 숫자 나머지들은 스택에 넣은 후, 나머지 연산 끝나면 스택에서 차례대로 pop하면서 string배열 인덱스로 나머지를 문자열로 만들어서 출력하려고 했다. 여기서 0을 생각 못해서 한번 틀리고, 인덱스가 헷갈려서 두번 틀렸다....에고 창피해///

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

#include<iostream>

#include<string>

#include <stack>

using namespace std;

string arr[36];

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);

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

    {

        arr[i] = to_string(i);

    }

    int p = 0;

    for (int i = 10; i < 36; i++)

    {

        arr[i] = 'A' + p;

        p++;

    }

    stack<int> s;

    int n, b;

    cin >> n >> b;

    while (n!=0)

    {

        s.push(n%b);

        n /= b;

    }

    string str;

    while (!s.empty())

    {

        str += arr[s.top()];

        s.pop();

    }

    cout << str << '\n';

    return 0;

}

cs

반대로 B진법 수 N을 10진수로 바꾸려면 B^k을 곱하면서 더해가면 된다.

 

<2745번 / 진법 변환>

이건 배열 사용안하고 아스키코드로 했다. 그리고 뒤에서부터 계산해줘야하기 떄문에 문자열의 길이를 구해 뒤에서 부터 index를 찾아가며 구해줬다.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

#include<iostream>

#include<string>

#include <stack>

using namespace std;

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);

    string str;

    int b;

    cin >> str >> b;

    int len = str.length();

    int res = 0;

    int y = 1;

    for (int i = len-1; i >= 0; i--)

    {

        int k;

        if (str[i] >= 'A'&&str[i] <= 'Z')

        {

            k = str[i] - 55;

        }

        else

        {

            k = str[i]-48;

        }

        res += k * y;

        y *= b;

    }

    cout << res << '\n';

    return 0;

}

cs

 

<11576번 / Base Conversion>

재귀함수를 통해 구현하였다. 우선 a진법 수를 10진수로 바꾸고 그 10진수를 다시 b진법 수로 바꿔준다. b진법 수로 바꿔줄때 재귀함수를 사용한다. 바로바로 출력하는 형식으로 하였따.(전에 엥? 했던 재귀함수방법 사용)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

#include<iostream>

#include<string>

#include <stack>

using namespace std;

void converse(int a, int b)

{

    if (a == 0)

        return;

    converse(a / b, b);

    cout << a % b << " ";

}

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);

    int a, b, m;

    cin >> a >> b;

    cin >> m;

    int ten = 0;

    for (int i = 0; i < m; i++)

    {

        int x;

        cin >> x;

        ten = ten * a + x;

    }

    converse(ten, b);

    return 0;

}

cs

 

-소인수분해

 

소인수분해 알고리즘

정수 N을 소수의 곱으로 분해한다. 이는 소수를 구하지 않고도 해결할 수 있다. N을 소인수 분해 했을때, 나타날 수 있는 인수 중에서 가장 큰 값은 루트 N이다. 따라서 2부터 루트 N까지 for문을 돌면서 N을 나눌 수 있으면, 나눌 수 없을 때까지 계속해서 나누면 된다. 

 

<11653번 / 소인수분해>

위의 알고리즘을 고대~~~로 써서 구현하였다. 아주 쉽게 했군! 이걸 안보고도 딱 생각해낼수 있도록 연습하자!!

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

#include<iostream>

#include<string>

#include <stack>

using namespace std;

int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(nullptr);

    int n;

    cin >> n;

    for (int i = 2; i*<= n; i++)

    {

        while (n%i == 0)

        {

            cout << i << '\n';

            n /= i;

        }

    }

    if (n > 1)

    {

        cout << n << '\n';

    }

    return 0;

}

cs

 

끝끝끝 끝이에유~~~><

728x90

댓글