공부/SWEA

SWEA 9839. 최고의 쌍 (C++)

밤톨ㅇl 2024. 5. 8. 01:35
#include <stdio.h>

int N, max;
int num[1001];
bool visited[1001];

bool check(int n)
{
    int a = n % 10;
    bool flag = true;
    while (n / 10 > 0)
    {
        n = n / 10;
        if (n % 10 == a - 1)
            a--;
        else
        {
            flag = false;
            break;
        }
    }
    return flag;
}

void solve(int depth, int n)
{
    if (depth == 2)
    {
        if (max < n && check(n))
            max = n;

        return;
    }

    for (int i = 0; i < N; i++)
    {
        if (!visited[i])
        {
            visited[i] = true;
            n *= num[i];
            solve(depth + 1, n);
            n /= num[i];
            visited[i] = false;
        }
    }
}
int main()
{
    int tc = 0;
    scanf("%d", &tc);
    for (int t = 1; t <= tc; t++)
    {
        max = -1;
        scanf("%d", &N);
        for (int i = 0; i < N; i++)
        {
            scanf("%d", &num[i]);
        }

        solve(0, 1);

        printf("#%d %d\n", t, max);
    }
}

1. dfs로 모든 경우의 수 확인

2. 곱셈의 결과가 max보다 작다면 check 필요 없음

3. 연속되는 숫자 확인은 n % 10, n / 10으로 확인

4. 끝!!

'공부 > SWEA' 카테고리의 다른 글

SWEA 3282. 0/1 Knapsack (C++)  (0) 2024.05.10
SWEA 19113. 식료품 가게 (C++)  (0) 2024.05.10
SWEA 11315. 오목 판정 (C++)  (0) 2024.05.08
SWEA 9778. 카드 게임 (C++)  (0) 2024.05.06
SWEA 3975. 승률 비교하기 (C++)  (0) 2024.05.06