###前提・実現したいこと
荷物の重量の配列が入力され、i位置とj位置の持ち上げ交換する
荷物の重量を昇順に整列するコスト(移動させた)重量の総和の最小を求める。
sortedはソートされた配列です。
sorted[i] * j と、の意味と
sorted[0] * j + (sorted[i] * 2 + sorted[0] * 2) の意味を解説してくださる方いませんか?
エラーではなく、この1行の意味が知りたいです
cost += Math.min(sorted[i] * j, sorted[0] * j + (sorted[i] + sorted[0] * 2));
###発生している問題・エラーメッセージ
エラーメッセージ
###該当のソースコード
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] W = new int[n]; for (int i = 0; i < n; i++) { W[i] = scanner.nextInt(); } System.out.println(minCostSort(W)); } public static int indexOf(int[] A, int x) { for (int i = 0; i < A.length; i++) { if (A[i] == x) { return i; } } return -1; } public static int minCostSort(int[] W) { int sorted[] = W.clone(); Arrays.sort(sorted); int cost = 0; for (int i = 0; i < W.length; i++) { int a = indexOf(W, sorted[i]); int j = 0; while (a > i) { j++; int b = indexOf(W, sorted[a]); cost += W[b]; int t = W[a]; W[a] = W[b]; W[b] = t; a = b; } cost += Math.min(sorted[i] * j, sorted[0] * j + (sorted[i] + sorted[0] * 2)); } return cost; } }
###試したこと
課題に対してアプローチしたことを記載してください
###補足情報(言語/FW/ツール等のバージョンなど)
ロジック訂正
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/03/13 09:40