백준 2577번 - 숫자의 개수[C/C++]

나모네·2023년 7월 24일
0

백준 문제풀이

목록 보기
1/8

문제

2577번 (숫자의 개수)
https://www.acmicpc.net/problem/2577


접근 방법

세 자연수를 곱한 값의 나머지들을 구해줌. 곱한 값 % 10해준 결과에 따라 특정 변수의 수를 늘려주고, 곱한 값 = 곱한 값/10으로 설정해줌. 곱한 값>0 일때까지 반복.


문제 풀이

#include <stdio.h>

int main() {
	int a, b, c;
	int res;
	int i;
	int zero = 0, one = 0, two = 0, three = 0, four = 0, five = 0, six = 0, seven = 0, eight = 0, nine = 0;

	scanf("%d", &a);
	scanf("%d", &b);
	scanf("%d", &c);

	res = a * b * c;

	while (res>0) {
		if (res % 10 == 0) {
			++zero;
		}
		else if (res % 10 == 1) {
			++one;
		}
		else if (res % 10 == 2) {
			++two;
		}
		else if (res % 10 == 3) {
			++three;
		}
		else if (res % 10 == 4) {
			++four;
		}
		else if (res % 10 == 5) {
			++five;
		}
		else if (res % 10 == 6) {
			++six;
		}
		else if (res % 10 == 7) {
			++seven;
		}
		else if (res % 10 == 8) {
			++eight;
		}
		else if (res % 10 == 9) {
			++nine;
		}
		res =res/ 10;
	}

	printf("%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n%d\n", zero, one, two, three, four, five, six, seven, eight, nine);

	return 0;
}

피드백

쓸데 없이 변수명을 많이 선언했음. 코드가 클린하지 않다. 변수명의 수를 줄이기 위해 배열 사용.
또한 반복되는 작업이 너무 많음. 반복문 사용

"Rujang"님의 코드를 보면 매우 깔끔하다.
https://rujang.tistory.com/entry/%EB%B0%B1%EC%A4%80-2577%EB%B2%88-%EC%88%AB%EC%9E%90%EC%9D%98-%EA%B0%9C%EC%88%98-CC

2개의 댓글

comment-user-thumbnail
2023년 7월 24일

개발자로서 성장하는 데 큰 도움이 된 글이었습니다. 감사합니다.

1개의 답글