Created
May 8, 2016 13:42
-
-
Save wreulicke/7a95711426f248d6f807a1cc21af64d3 to your computer and use it in GitHub Desktop.
ある点Xへの各点からの距離の総和の最小化
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package jp.co.ss; | |
import org.junit.Test; | |
public class CodeTest { | |
int[] testCase={1,2,4,8,16,32,64}; | |
@Test | |
public void plan1(){ | |
int min=Integer.MAX_VALUE; | |
int x=0; | |
for(int i=testCase[0];i<testCase[testCase.length-1];i++){ | |
int sum=0; | |
for(int j=0;j<testCase.length;j++){ | |
sum+=Math.abs(i-testCase[j]); | |
} | |
if(min>sum){ | |
min=sum; | |
x=i; | |
} | |
} | |
System.out.println(min); | |
System.out.println(x); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment