3영2 2019. 3. 13. 15:32
반응형

11050번 문제 : 이항 계수 1

https://www.acmicpc.net/problem/11050


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>
using namespace std;
 
int main() {
    int x, y;
    cin >> x >> y;
    
    int temp1 = 1;
    for (int i = x; i > x - y; i--)
    {
        temp1 *= i;
    }
 
    int temp2 = 1;
    for (int i = 1; i <= y; i++)
    {
        temp2 *= i;
    }
 
    int result = temp1 / temp2;
 
    if (y != 0)
        cout << result << endl;
    else
        cout << "1" << endl;
 
    return 0;
}
cs


10872번 문제 : 팩토리얼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;
 
int main() {
    int inp, result = 1;
    cin >> inp;
 
    for (int i = 1; i <= inp; i++)
    {
        result *= i;
    }
    
    cout << result << endl;
 
    return 0;
}
cs


1676번 문제 : 팩토리얼 0의 개수

https://www.acmicpc.net/problem/1676


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
#include <iostream>
using namespace std;
 
int main() {
    int inp;
    int counter = 0;
    cin >> inp;
 
    int counterA = 0, counterB = 0;
    for (int i = 1; i <= inp; i++)
    {
        int temp = i;
        while (temp % 2 == 0)
        {
            counterA++;
            temp /= 2;
        }
        
        while (temp % 5 == 0)
        {
            counterB++;
            temp /= 5;
        }
    }
 
    cout << "a: " << counterA << "   b: " << counterB << endl;
    counterA >= counterB ? counter = counterB : counter = counterA;
 
    cout << counter << endl;
 
    return 0;
}
cs


메모:

- 팩토리얼 결과값을 구한 후에 나눠서 계산하는 방법은 효율적이지 못함 + 데이터형의 범위를 벗어나기 때문에 제대로된 답을 구할 수 없음.

- 2와 5가 한쌍 씩있으면 10의 배수이기 때문에 뒷자리가 0이다. 따라서 2와 5가 몇개의 쌍이 있는 지 확인만 하면 뒷자리의 0개수를 확인 할 수 있다. 




반응형