https://www.acmicpc.net/problem/6463
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <stack>
#include <iomanip>
#include <cmath>
using namespace std;
int lastNonZeroDigitOfFactorial(int n) {
int result = 1;
int two_count = 0, five_count = 0;
for (int i = 1; i <= n; ++i) {
int num = i;
while (num % 2 == 0) {
++two_count;
num /= 2;
}
while (num % 5 == 0) {
++five_count;
num /= 5;
}
result *= num;
result %= 10;
}
for (int i = 0; i < two_count - five_count; i++) {
result *= 2;
result %= 10;
}
return result;
}
int main()
{
int num;
while (cin >> num || !cin.eof()) {
int lastNonZeroDigit = lastNonZeroDigitOfFactorial(num);
cout << setw(5) << num;
cout << " -> ";
cout << lastNonZeroDigit << endl;
}
return 0;
}
'콘솔창 & 윈도우창 > 코딩 테스트' 카테고리의 다른 글
프로그래머스 LV.1 폰켓몬 (0) | 2024.08.17 |
---|---|
프로그래머스 - LV.1 [1차] 비밀지도 (0) | 2024.08.15 |
프로그래머스 lv.2 H-Index (0) | 2024.04.18 |
에라토스테네스의 체를 응용한 소수 사이 수열 3896 (0) | 2024.04.12 |
Anagram문제 (0) | 2024.04.11 |