알고리즘 문제풀기/인프런 강의 정답

30. 3의 개수는? (large : 제한시간 1초) [코드구현력 기르기]

코다람쥐 2022. 4. 5. 18:06

나의정답.

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

int main(int argc, char** argv) {
	//freopen("input.txt", "rt", stdin);

	int n, temp, inc = 1, prev = 0, res = 0, remain;
	int num = 0;
	int target = 3; // 세려는 숫자 

	scanf("%d", &n);
	
	temp = n;
	for (int i = 0; temp > 0; i++) {
		remain = temp % 10;
		
		if (remain == target) {
			int rt = (n % inc) + 1;	
			for (int j = 0; j < rt; j++)
				num++;

			for (int j = 0; j < target; j++) {
				num += res;
			}	
		}
		else {
			for (int j = 0; j < remain; j++) {
				if (j == target) {
					num += inc + res;
					continue;
				}
				num += res;
			}
		}
		temp /= 10;
		res = inc + prev;
		inc *= 10;
		prev = res * 10;
	}

	printf("%d", num);
}