알고리즘&자료구조 공부/it 취업을 위한 알고리즘 문제풀이 입문 강의

it 취업을 위한 알고리즘 문제 풀이 입문 (With C/C++) 29. 3의 개수는?(small)

dubu0721 2024. 7. 8. 12:27

[기억할 내용들]

  1. 시간제한이 없는 문제라 원래 풀던 것처럼 2중 반복문을 사용해도 문제가 없다.
  2. 문제가 쉬워서 딱히 기억할 만한 내용이 없다.

 

[나의 코드]

#include <iostream>
#include <stdio.h>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false);

	int n;
	cin >> n;
	int count3 = 0;

	for (int i = 1; i <= n; i++) {
		int tmp = i;
		while (tmp > 0) {
			if (tmp % 10 == 3) {
				count3++;
			}
			tmp /= 10;
		}
	}

	cout << count3 << "\n";

	return 0;
}

 

 

[강의 코드]

#include <iostream>
#include <stdio.h>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
	int n, tmp, i, cnt = 0, digit;
	scanf_s("%d", &n);
	for (i = 1; i <= n; i++) {
		tmp = i;
		while (tmp > 0) {
			digit = tmp % 10;
			if (digit == 3) cnt++;
			tmp /= 10;
		}
	}
	printf("%d\n", cnt);

	return 0;
}

 

 

[의견]

아무래도 쉬운 문제라 그런지 풀이 방식이 아예 똑같았다.