해결방법
입력받은 숫자를 문자로 변환하여 문자열의 맨 뒤에서부터 '0'을 빼서 아스키코드를 통한 연산으로 숫자를 받아서 배열의 수를 증가시킨다. (예를 들어 13410의 경우 맨 뒤에서부터 0, 1, 4, 3, 1이 1번씩 증가)
원리는 숫자의 아스키 코드값이 연속적이고, 0, 1, 2, 3... 순으로 배치되어 있기 때문에 그 특성을 활용한 것이다.
정답 코드
#include <iostream>
#include <string>
using namespace std;
int main()
{
std::cin.tie(NULL);
std::ios::sync_with_stdio(false);
unsigned long long A, B, C, temp;
string num;
cin >> A;
cin >> B;
cin >> C;
temp = A * B * C;
num = to_string(temp);
int numarr[10] = {0};
while (true)
{
if (num.empty())
{
break;
}
const char tempchar = num.back();
const int tempnum = tempchar - '0';
numarr[tempnum] += 1;
num.pop_back();
}
for (int i = 0; i < 10; i++)
{
cout << numarr[i] << "\n";
}
return 0;
}
'코딩테스트' 카테고리의 다른 글
BOJ - 팩토리얼 0의 개수 1676 (0) | 2025.01.04 |
---|---|
BOJ solved.ac 18110 (0) | 2025.01.04 |
BOJ - 이항 계수 1 11050 (0) | 2024.12.17 |
BOJ - FizzBuzz 28702 (0) | 2024.12.15 |
BOJ - 부녀회장이 될테야 2775 (0) | 2024.12.15 |