Algorithm/백준(BOJ)
[백준 10819] 차이를 최대로
Gyoogle
2019. 1. 20. 14:53
728x90
반응형
[백준 10819] 차이를 최대로
문제 출처 : https://www.acmicpc.net/problem/10819
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | import java.util.Scanner; public class Problem10819 { static int[] t = new int[27483084]; // static으로 배열 선언 static int cnt = 0; // static으로 int형 변수 선언 public static void swap(int[] arr, int a, int b){ // swap 메소드 int temp = arr[a]; arr[a] = arr[b]; arr[b] = temp; } public static void perm(int[] set, int size, int n, int k){ // 순열 메소드 int sum = 0; if(size == k){ for (int i = 0; i < size-1; i++) { sum += Math.abs(set[i]-set[i+1]); // 절대값 합 저장 } t[cnt] = sum; cnt++; return; } for (int i = size; i < n; i++) { swap(set, i, size); perm(set, size+1, n, k); swap(set, i, size); } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int count = sc.nextInt(); int[] arr = new int[count+1]; int[] set = new int[100]; for (int i = 0; i < count; i++) { arr[i] = sc.nextInt(); } for (int i = 0; i < count; i++) { set[i] = arr[i]; } perm(set, 0, count, count); int max = 0; // 최대값 저장 for (int i = 0; i < cnt; i++) { if(max <= t[i]) max = t[i]; } System.out.println(max); } } | cs |
728x90
반응형