본문 바로가기

Algorithm

C++(씨쁠쁠)(cplusplus)-백준(baekjoon)(BaekJoon)코딩 10819번:차이를 최대로 답

반응형

next_permutation()함수를 사용하여 풀었습니다.

1.먼저 sort()함수를 이용해 오름차순으로 정렬해줍니다.

2.다음 순열이 있다면 그 배열을 다음 순열(집합이 작은순)로 바꿔주고 true를 반환합니다.

3.반대의 경우는 false를 반환합니다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<stdio.h>
#include<algorithm>
using namespace std;
int n, a[8], big, sum;
int main() {
    scanf("%d"&n);
    for (int i = 0; i < n; i++scanf("%d"&a[i]);
    sort(a, a + n);
    while (next_permutation(a, a + n))
    {
        sum = 0;
        for (int i = 0; i < n - 1; i++
            sum += abs(a[i] - a[i + 1]);
        big = max(big, sum);
    } 
    printf("%d", big);
    return 0;
}
cs