Skip to content

Instantly share code, notes, and snippets.

@wreulicke
Created May 8, 2016 13:42
Show Gist options
  • Save wreulicke/7a95711426f248d6f807a1cc21af64d3 to your computer and use it in GitHub Desktop.
Save wreulicke/7a95711426f248d6f807a1cc21af64d3 to your computer and use it in GitHub Desktop.
ある点Xへの各点からの距離の総和の最小化
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