https://www.acmicpc.net/problem/2503
문제 풀이
두 수의 자리수를 비교하는 경우 string으로 변환해서 비교하는게 편해 string으로 변환한 후 자리수를 비교했다.
'1에서 9까지의 서로 다른 숫자 세 개로 구성된 세 자리 수'라는 조건이 있어 0이 있거나 중복된 수는 고려하지 않고 완전탐색 방식으로 답을 구했다.
소스 코드
#include <iostream>
#include <string>;
using namespace std;
string num[101];
int strike[101] = { 0 };
int ball[101] = { 0 };
int main() {
int N;
cin >> N;
for (int i = 0; i < N; i++) {
int a;
cin >> num[i] >> strike[i] >> ball[i];
}
int cnt = 0;
for (int j = 123; j <= 987; j++) {
bool find = true;
string comp_j = to_string(j);
if (comp_j[0] == '0' || comp_j[1] == '0' || comp_j[2] == '0') continue; // 0이 들어있는 수
else if (comp_j[0] == comp_j[1] || comp_j[0] == comp_j[2] || comp_j[1] == comp_j[2]) continue; // 서로 다른 수로 구성되지 않은 경우
else {
for (int n = 0; n < N; n++) {
int s = 0;
int b = 0;
string origin = num[n];
for (int t = 0; t < 3; t++) {
if (comp_j[t] == origin[t]) s++; //자리수의 수가 같은 경우
else {
if (comp_j[t] == origin[0] || comp_j[t] == origin[1] || comp_j[t] == origin[2]) {
// 자리수의 수가 같지 않을 때 다른 자리수에 수가 있는지 확인
b++;
}
}
}
if (s != strike[n] || b != ball[n]) find = false;
}
if (find == true) cnt++;
}
}
cout << cnt << '\n';
}
'알고리즘' 카테고리의 다른 글
[백준] 15686번 치킨 배달 C++ 문제풀이 (0) | 2023.08.12 |
---|---|
[백준] 14501번 퇴사 C++ 문제풀이 (0) | 2023.08.11 |
[백준] 1300번 K번째 수 C++ 문제풀이 (0) | 2023.08.09 |
[백준] 2108번 통계학 C++ 문제풀이 (0) | 2023.08.09 |
[백준] 1339번 단어 수학 C++ 문제풀이 (0) | 2023.08.07 |