[백준 21567] 숫자의 개수 2 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
세 자연수가 주어지는 상황에서, A, B, C (각각 1 ≤ A, B, C ≤ 1,000,000)가 주어질 때, A × B × C의 결과에서 0부터 9까지 각 숫자가 몇 번 등장하는지 구하는 문제입니다.
결과는 0의 등장 횟수부터 9의 등장 횟수까지 10줄로 출력합니다.
접근법
세 수의 곱을 계산한 후 문자열로 변환합니다.
크기 10의 카운트 배열을 준비하고, 문자열의 각 문자를 순회하며 해당 숫자의 등장 횟수를 증가시킵니다. 0부터 9까지의 등장 횟수를 순서대로 출력합니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
using System;
namespace Solution {
class Program {
static void Main(string[] args) {
var a = long.Parse(Console.ReadLine()!);
var b = long.Parse(Console.ReadLine()!);
var c = long.Parse(Console.ReadLine()!);
var count = new int[10];
var result = (a * b * c).ToString();
foreach (var digit in result)
count[digit - '0']++;
for (var i = 0; i < 10; i++)
Console.WriteLine(count[i]);
}
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll a, b, c; cin >> a >> b >> c;
ll product = a * b * c;
int count[10] = {0};
string result = to_string(product);
for (char digit : result)
count[digit - '0']++;
for (int i = 0; i < 10; i++)
cout << count[i] << "\n";
return 0;
}