본문 바로가기
CodingTest/백준

[c++] 백준 - 2577: 숫자의 개수

by Daybreak21 2023. 5. 21.

2577번: 숫자의 개수 (acmicpc.net)

 

2577번: 숫자의 개수

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

www.acmicpc.net

 

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int main() {

	int numArr[10] = {0,}, A, B, C, num;
	cin >> A >> B >> C;
	num = A * B * C;

	string numStr = to_string(num);
	char* numChar = new char[strlen(numStr.c_str())];
	strcpy(numChar, numStr.c_str());

	for (int i = 0; i < strlen(numStr.c_str()); i++) {
		numArr[numChar[i]-48]++;
	}
	for (int i = 0; i < 10; i++) cout << numArr[i] << endl;

	return 0;
}

-string라이브러리를 사용하여 정수를 char형태로 바꾸어주었다.

-그냥 numChar[i]는 문자이기때문에 아스키코드에 48을 빼주어 문자에 맞는 10진수형태로 바꾸어주었다. 

'CodingTest > 백준' 카테고리의 다른 글

[c++] 백준 - 10773: 제로  (1) 2023.05.21
[c++] 백준 - 8979번: 올림픽  (0) 2023.05.21
[c++] 백준 - 2816: 디지털 티비  (0) 2023.05.20
[c++] 백준 - 2621: 카드게임  (0) 2023.05.19
[c++] 백준 - 25206: 너의 평점은  (1) 2023.05.16