🍧 μ½”λ”©ν…ŒμŠ€νŠΈ

[μΈν”„λŸ° C++] 29. 3의 κ°œμˆ˜λŠ”? (small)

peewoong 2024. 4. 23. 21:13

🍧 문제

μžμ—°μˆ˜ n이 μž…λ ₯되면 1λΆ€ν„° nκΉŒμ§€μ˜ μžμ—°μˆ˜λ₯Ό 쒅이에 적을 λ•Œ 각 숫자 쀑 3의 κ°œμˆ˜κ°€ λͺ‡ κ°œμžˆλŠ”μ§€ κ΅¬ν•˜λ €κ³  ν•©λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄, 1λΆ€ν„° 15κΉŒμ§€λŠ” 3, ~ 1, 3 ~ 으둜 3의 κ°œμˆ˜κ°€ 2κ°œμ΄λ‹€.

 

🍧 μž…λ ₯

15

 

🍧 좜λ ₯

2

 

🍧 1트 (κ°•μ˜ 풀이)

#include <iostream>
#include <vector>
#include<algorithm>
using namespace std;

int main(void){
	//freopen("input.txt", "rt", stdin);
	
	int n, tmp, digit, cnt = 0;
	cin >> n;
	
	for(int i = 1; i<= n; ++i){
		tmp = i;
		while(tmp > 0){
			digit = tmp % 10;
			if(digit == 3) cnt++;
			tmp = tmp / 10;
		}
	}
	
	cout << cnt;
	return 0;
}