๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐“ก๐“ธ๐“ธ๐“ถ๐Ÿฃ: ๐’œ๐“๐‘”๐‘œ๐“‡๐’พ๐“‰๐’ฝ๐“‚/์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ ํ’€์ด

BOJ 2503 : ์ˆซ์ž์•ผ๊ตฌ (Silver 5)

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

int number;
int strike, ball;
vector<int> array;
void Filtering(int num, int s, int b)
{
    vector<int> newarray;
    vector<int>::iterator iter;
    int strike = 0, ball = 0;
    for (iter = array.begin(); iter != array.end(); iter++)
    {
        int strike = 0, ball = 0;
        // cout << *iter << "๊ฒ€์‚ฌ\n";
        int a1 = num / 100;
        int b1 = num / 10 % 10;
        int c1 = num % 10;
        int a2 = *iter / 100;
        int b2 = *iter / 10 % 10;
        int c2 = *iter % 10;
        // cout << a1 << a2 << "/" << b1 << b2 << "/" << c1 << c2 << "\n";
        if (a1 == a2)
            strike++;
        else if (a1 == b2)
            ball++;
        else if (a1 == c2)
            ball++;
        if (b1 == b2)
            strike++;
        else if (b1 == a2)
            ball++;
        else if (b1 == c2)
            ball++;
        if (c1 == c2)
            strike++;
        else if (c1 == a2)
            ball++;
        else if (c1 == b2)
            ball++;
        if (strike == s && ball == b && a2 != b2 && b2 != c2 && a2 != c2 && a2 != 0 && b2 != 0 && c2 != 0)
        {
            newarray.push_back(*iter);
            //     cout << *iter << "\n";
        }
    }
    array = newarray;
    return;
}

int main()
{
    int T;
    cin >> T;
    for (int i = 123; i <= 987; i++)
    {
        array.push_back(i);
    }
    for (int i = 0; i < T; i++)
    {
        cin >> number >> strike >> ball;
        Filtering(number, strike, ball);
        // cout << "****************************\n";
    }
    cout << array.size();
}

์ด์ œ ๋งจ ์ฒ˜์Œ์—” 123~987 ๋กœ array๋ฅผ ์—…๋ฐ์ดํŠธ ใ…ใ…Ž์ฃผ๊ณ , ์ด ์•ˆ์—์„œ ๊ฐ ์ž๋ฆฌ ์ˆซ์ž๋ฅผ ๊ตฌํ•ด์„œ strike๋ž‘ ball์„ ๊ตฌํ•œ ๋’ค, ํ•จ์ˆ˜ ์ธ์ž๋กœ ๋„˜๊ฒจ์ค€ ์ •๋‹ต strike, ball๊ณผ ๊ฐ’์ด ์ผ์น˜ํ•˜๋Š”์ง€ ๊ฒ€์‚ฌํ•จ. ๊ทธ๋ž˜์„œ ์ผ์น˜ํ•˜๋ฉด ๊ทธ ์ˆซ์ž๋Š” ๊ฐ€๋Šฅํ•œ ์ˆซ์ž์ด๋ฏ€๋กœ newArray์— ์ถ”๊ฐ€ํ•ด์คŒ. ์ด ์ง“์„ ๋ชจ๋“  array์— ์žˆ๋Š” ๊ฐ’์— ๋Œ€ํ•ด ํ•ด์ค€ ๋’ค, newArray๋กœ array๋ฅผ ๋Œ€์ฒดํ•ด์คŒ. (์—…๋ฐ์ดํŠธ ํ•ด์ฃผ๋Š”๊ฒƒ!)

์ด ๋•Œ, ๊ฐ ์ž๋ฆฌ ์ˆซ์ž์— 0์ด ์žˆ๊ฑฐ๋‚˜, ๊ฐ™์€ ์ˆซ์ž๊ฐ€ ์žˆ๋Š” ๊ฒฝ์šฐ๋Š” ์ถ”๊ฐ€ํ•ด ์ฃผ๋ฉด ์•ˆ๋œ๋‹ค.