자료구조, 코딩테스트/배열(Array)

[코딩테스트] BOJ2577 - 숫자의 개수 (성공)

RꞮbble 2026. 3. 25. 22:11

 

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

 

2577번: 숫자의 개수

첫째 줄에 A, 둘째 줄에 B, 셋째 줄에 C가 주어진다. A, B, C는 모두 100보다 크거나 같고, 1,000보다 작은 자연수이다.

www.acmicpc.net

 

문제

세 개의 자연수 A, B, C가 주어질 때 A × B × C를 계산한 결과에 0부터 9까지 각각의 숫자가 몇 번씩 쓰였는지를 구하는 프로그램을 작성하시오.

예를 들어 A = 150, B = 266, C = 427 이라면 A × B × C = 150 × 266 × 427 = 17037300 이 되고, 계산한 결과 17037300 에는 0이 3번, 1이 1번, 3이 2번, 7이 2번 쓰였다.

 

 

입력

첫째 줄에 A, 둘째 줄에 B, 셋째 줄에 C가 주어진다. A, B, C는 모두 100보다 크거나 같고, 1,000보다 작은 자연수이다.

 

 

출력

첫째 줄에는 A × B × C의 결과에 0 이 몇 번 쓰였는지 출력한다. 마찬가지로 둘째 줄부터 열 번째 줄까지 A × B × C의 결과에 1부터 9까지의 숫자가 각각 몇 번 쓰였는지 차례로 한 줄에 하나씩 출력한다.

 

 

예제 입력 1

150
266
427

 

 

예제 출력 1

3
1
0
2
0
0
0
2
0
0

 

 

내 코드 - 정답 

public class BOJ2577
{
    public static void Main(string[] args)
    {
        // arr[10] = 숫자가 각각 몇번 쓰였는지 저장하는 배열 
        // a x b x c = 결과값 
        // a x b x c 자릿수는 어떻게 구해? 
        // (a x b x c).ToString().Length
        // 1234 % 10 = 4 -> 마지막 숫자 

        int multiply = 1;
        for (int i = 0; i < 3; i++)
        {
            multiply *= int.Parse(Console.ReadLine());
        }

        int[] arr = new int[10];
        int remainder = 0;
        while (multiply / 10 != 0)
        {
            remainder = multiply % 10;
            multiply = multiply / 10;
            arr[remainder] += 1;
        }

        remainder = multiply % 10;
        multiply = multiply / 10;
        arr[remainder] += 1;

        foreach (int number in arr)
        {
            Console.WriteLine(number);
        }
    }
}

 

 

다른 사람 코드

public class BOJ2557
{
    public static void Main(string[] args)
    {
        int multiply = 1;
        for (int i = 0; i < 3; i++)
        {
            multiply *= int.Parse(Console.ReadLine());
        }

        int[] arr = new int[10];

        while (multiply > 0)
        {
            arr[multiply % 10] += 1;
            multiply /= 10;
        }

        foreach (int number in arr)
        {
            Console.WriteLine(number);
        }
    }
}

 

 

복습1 풀이 (5/4)

크기가 10인 배열의 인덱스 0~9번을 0~9번 숫자로 보고, 인덱스 0~9번에 들어갈 값을 0~9번 숫자의 등장 횟수로 설정하고 풀었다. 

 

 

복습1 코드 - 정답

using System.Text;

namespace 배열_구현
{
    internal class BOJ2577_숫자의_개수_복습1
    {
        static StreamReader sr = new StreamReader(Console.OpenStandardInput());
        static StreamWriter sw = new StreamWriter(Console.OpenStandardOutput());
        static StringBuilder sb = new StringBuilder();

        static void Main(string[] args)
        {
            int A = int.Parse(sr.ReadLine());
            int B = int.Parse(sr.ReadLine());
            int C = int.Parse(sr.ReadLine());
            long result = A * B * C;

            int[] arr = new int[10];
            while(result > 0)
            {
                long remainder = result % 10;
                arr[remainder]++;

                result /= 10;
            }

            foreach (int num in arr)
                sb.Append($"{num}\n");

            sw.WriteLine(sb.ToString());

            sw.Close();
            sr.Close();
        }
    }
}