oj赛氪练习题

                                             数组调整

oj赛氪练习题_第1张图片

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int k = scanner.nextInt();

        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = scanner.nextInt();
        }

        int sum = findAdjustedArraySum(arr, n, k);
        System.out.println(sum);
    }

    private static int findAdjustedArraySum(int[] arr, int n, int k) {
        int sum = 0;
        for (int i = 0; i < n; i++) {
            if (i == k - 1) {
                arr[i] = -arr[i];
            }
            sum += arr[i];
        }
        return sum;
    }
}

你可能感兴趣的:(算法)