[algorithm] 백준 2577번 숫자의 개수

2026. 1. 10. 12:12·algorithm/배열
반응형

 

이 문제는 10808번과 유사하게

아스키코드를 활용해서 풀면 되는 문제다

#include <iostream>
#include <string>
using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int a,b,c;
    cin >> a >> b >> c;
    
    int arr[10] = {};
    int i = a*b*c;

    string j = to_string(i);
    
    for(int tmp : j)
        {
            arr[tmp-48]++; 
        }
    
    for(int tmp : arr)
        {
            cout << tmp << "\n";
        }
 
    return 0;
}

 

똑같이 배열의 원소값을 숫자의 등장횟수로 정하고

0의 아스키코드 값이 48인 점을 활용하면 된다

 

int형을 string형으로 바꾸기 위해 to_string함수를 사용했다

 

// Authored by : BaaaaaaaaaaarkingDog
// Co-authored by : OceanShape
// http://boj.kr/fefbce1d30c9442db611909c690df1a8
#include <bits/stdc++.h>
using namespace std;

int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int A, B, C;
  cin >> A >> B >> C;
  int t=A*B*C;
  int d[10] = {};
  // 계산 결과를 자릿수별로 확인하여 저장
  while (t>0) {
    d[t%10]++;
    t/=10;
  }
  for (int i=0; i<10; ++i) cout << d[i] << '\n';
}

 

창의적인 다른 풀이가 있어서 소개해보려 한다

while문으로 몫이 0이 될때까지 반복하여

나머지 값은 무조건 0~9이기에

나머지 값과 배열 상 원소의 위치를 같게하여 카운팅하는 방식이다

반응형

'algorithm > 배열' 카테고리의 다른 글

[algorithm] 백준 11328번 Strfry  (0) 2026.01.10
[algorithm] 백준 13300번 방 배정  (0) 2026.01.10
[algorithm] 백준 1475번 방 번호  (0) 2026.01.10
[algorithm] 백준 10807번 개수 세기  (0) 2026.01.10
[algorithm] 백준 10808번 알파벳 개수  (0) 2026.01.10
'algorithm/배열' 카테고리의 다른 글
  • [algorithm] 백준 13300번 방 배정
  • [algorithm] 백준 1475번 방 번호
  • [algorithm] 백준 10807번 개수 세기
  • [algorithm] 백준 10808번 알파벳 개수
20puddle
20puddle
20puddle 님의 블로그 입니다.
  • 20puddle
    20puddle 님의 블로그
    20puddle
  • 전체
    오늘
    어제
    • 분류 전체보기 (100)
      • Spring boot (5)
      • git & github (5)
      • algorithm (47)
        • theory (3)
        • 배열 (7)
        • 연결 리스트 (3)
        • 스택 (5)
        • 큐 (3)
        • 덱 (2)
        • 기초 코드 (19)
        • BFS (5)
      • AI (43)
        • Machine Learning (35)
        • Deep Learning (8)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    그레디언트 클리핑
    머신러닝
    AI
    Aimers
    github
    게시판
    주성분변환
    ML
    Java
    list
    홀드아웃 검증
    백준
    git
    인공지능
    알고리즘
    DL
    Spring Boot
    오프라인 학습
    딥러닝
    연결리스트
  • 최근 댓글

  • 최근 글

  • 반응형
  • hELLO· Designed By정상우.v4.10.3
20puddle
[algorithm] 백준 2577번 숫자의 개수
상단으로

티스토리툴바