• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    해결됨

완전탐색 숫자야구 2503

23.10.20 20:49 작성 조회수 500

2

c++로 수강중인 학생입니다.. 백준 숫자야구 문제 c++로 풀어주실 수는 없을까요..? 몇번 구현해보다가 멘탈이 터져버렸습니다..

 

우선은

#include <iostream>

using namespace std;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    int numbers[1000];
    int strikes[1000];
    int balls[1000];

    for (int i = 0; i < n; i++) {
        cin >> numbers[i] >> strikes[i] >> balls[i];
    }

    int answer = 0;

    for (int a = 1; a < 10; a++) {
        for (int b = 1; b < 10; b++) {
            for (int c = 1; c < 10; c++) {
                int counter = 0;

                if (a == b || b == c || c == a) {
                    continue;
                }

                for (int i = 0; i < n; i++) {
                    int check = numbers[i];
                    int strike = strikes[i];
                    int ball = balls[i];

                    int strike_count = 0;
                    int ball_count = 0;

                    int check1 = check / 100;
                    int check2 = (check / 10) % 10;
                    int check3 = check % 10;

                    // 스트라이크 계산
                    if (a == check1) {
                        strike_count++;
                    }
                    if (b == check2) {
                        strike_count++;
                    }
                    if (c == check3) {
                        strike_count++;
                    }

                    // 볼 계산
                    if (a == check2 || a == check3) {
                        ball_count++;
                    }
                    if (b == check1 || b == check3) {
                        ball_count++;
                    }
                    if (c == check1 || c == check2) {
                        ball_count++;
                    }

                    // 매칭 여부 확인
                    if (strike != strike_count || ball != ball_count) {
                        break;
                    }

                    counter++;
                }

                if (counter == n) {
                    answer++;
                }
            }
        }
    }

    cout << answer << '\n';

    return 0;
}

이렇게 풀기는 했는데 선생님이 가르쳐주신 방향하고는 살짝 다른 거 같습니다.

답변 3

·

답변을 작성해보세요.

2

be_clever님의 프로필

be_clever

2023.10.21

// 주신 코드 기반으로 구현했습니다.
#include <bits/stdc++.h>

using namespace std;

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);

	int n;
	cin >> n;

	vector<vector<int>> hint;

	// 입력 받는 방법만 조금 다를 뿐이지 강의 풀이대로 잘 푸셨다고 생각합니다.
	// 입력을 최대한 비슷하게 받아보면 다음과 같이 나올 것 같습니다.
	for (int i = 0; i < n; i++) {
		int number, strike, ball;
		cin >> number >> strike >> ball;
		hint.push_back({ number, strike, ball });
	}

	int answer = 0;

	for (int a = 1; a <= 9; a++) {
		for (int b = 1; b <= 9; b++) {
			for (int c = 1; c <= 9; c++) {
				int cnt = 0;

				if (a == b || b == c || c == a) {
					continue;
				}

				for (auto& arr : hint) {
					int number = arr[0];
					int strike = arr[1];
					int ball = arr[2];

					int strike_count = 0;
					int ball_count = 0;

					// 자릿수 처리 방법은 편한대로 하시면 됩니다.
					int check1 = to_string(number)[0] - '0';
					int check2 = to_string(number)[1] - '0';
					int check3 = to_string(number)[2] - '0';

					// 이 부분이 각자 숙제로 구현해보는 부분인데 잘 작성한 풀이인 것 같습니다.
					if (a == check1) {
						strike_count++;
					}
					if (b == check2) {
						strike_count++;
					}
					if (c == check3) {
						strike_count++;
					}

					if (a == check2 || a == check3) {
						ball_count++;
					}
					if (b == check1 || b == check3) {
						ball_count++;
					}
					if (c == check1 || c == check2) {
						ball_count++;
					}

					if (strike == strike_count && ball == ball_count) {
						cnt++;
					}
				}

				if (cnt == n) {
					answer++;
				}
			}
		}
	}

	cout << answer << '\n';
	return 0;
}

위 코드 참고 부탁드립니다!

0

Natasha Evans님의 프로필

Natasha Evans

2023.10.25

Thank you for producing such a fascinating essay on this subject. This has sparked a lot of thought in me, and I'm looking forward to reading more pumpkin panic

 

0

인프런 AI 인턴님의 프로필

인프런 AI 인턴

2023.10.21

안녕하세요 soso1461님, 인프런 AI 인턴이에요.

제가 보았을 때, 작성해주신 코드는 숫자 야구 문제를 푸는 완전탐색 알고리즘으로 구현된 코드로 보입니다. 실제로 문제를 푸는데에는 문제가 없어보입니다.

선생님이 가르쳐주신 방향과 조금 다른 부분이 있을 수 있지만, 완전탐색 알고리즘을 사용한 코드로 정답을 도출해내는 것은 중요합니다. 선생님이 가르쳐주신 방향과 동일한 결과를 얻을 수 있다면 그것이 가장 중요한 점입니다.

만약 선생님이 가르쳐주신 방향과 어떻게 다른지에 대한 구체적인 내용을 알고 싶으시다면, 선생님께 직접 문의하시는 것이 가장 좋을 것 같습니다. 선생님께서 좀 더 적합한 방향으로 도움을 주실 수 있을 것입니다.

수강 중인 과정에서 멘탈이 터져버리신다니 아쉽네요. 그럴 때는 조금 쉬고 다시 도전해보시는 것을 추천드립니다. 문제 해결에는 시간과 노력이 필요한 경우가 많으니까요. 화이팅하세요!