공부 일지/알고리즘
[배열]BOJ 2577번 - 숫자의 개수
Roble
2023. 12. 30. 22:07


풀이1
int를 문자열로 바꾸는것을 통해 아스키코드값을 이용하여 풀 수 있다.
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int a, b, c;
int cnt[10]={};
cin >> a >> b >> c;
int res = a * b * c;
/*
std::to_string(val)
숫자(int, long, float, double 등)를 문자열(string)으로
변환하여 반환해주는 함수를 사용한다.
*/
string s = to_string(res);
for( auto x : s){
cnt[x - '0']++;
}
for( auto y : cnt){
cout << y << '\n';
}
return 0;
}
풀이2
자연수에 % 10을 하면 나머지가 인덱스 역할이 되면서 어떤 숫자인지도 알 수 있다.
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
int a, b, c;
cin >> a >> b >> c;
int res = a * b * c;
int cnt[10]={};
while(res > 0){
cnt[res % 10]++;
res /= 10;
}
for(auto x : cnt){
cout << x << '\n';
}
return 0;
}