본문 바로가기

CodingTest Exam/[C++] Algorithm Study

29. 3의 개수는?(small) (it 취업을 위한 알고리즘 문제풀이 입문 (with C/C++) : 코딩테스트 대비) ★☆☆☆☆

#include<stdio.h>
#include<vector>

using namespace std;
int main()
{
	int n;
	scanf("%d", &n);
	
	vector<int> count(10, 0);
	for(int i = 1; i <= n; i++)
	{
		int idx = 0;
		int x = i;
		while(x >= 1)
		{
			idx = x % 10;
			count[idx]++;
			x /= 10;
		}
	}
	
	printf("%d", count[3]);
	return 0;
}

1. 각 숫자마다 1의 자리수 추출 후 그 숫자 사용횟수를 count++ 한다.

2. 자릿수를 줄여가며 반복한다.

3. 3의 사용횟수를 출력한다.