Created
December 23, 2015 16:28
-
-
Save peter279k/6c8be08852fd211e24a6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import java.util.*; | |
public class Main { | |
public static void main(String[] args) { | |
Scanner input = new Scanner(System.in); | |
int count = 0; | |
while(true) { | |
int nums = input.nextInt(); | |
if(nums == 0) | |
break; | |
int num_arr[] = new int[nums]; | |
for(int i=0;i<nums;i++) { | |
num_arr[i] = input.nextInt(); | |
} | |
nums = input.nextInt(); | |
int num_arr2[] = new int[nums]; | |
for(int i=0;i<nums;i++) { | |
num_arr2[i] = input.nextInt(); | |
} | |
int temp = 0; | |
ArrayList<Integer> lists = new ArrayList<Integer>(); | |
for(int i=0;i<num_arr.length;i++) { | |
for(int j=i+1;j<num_arr.length;j++) { | |
temp = num_arr[i] + num_arr[j]; | |
lists.add(temp); | |
} | |
} | |
Collections.sort(lists); | |
count += 1; | |
int index = 0; | |
int min = Math.abs(lists.get(0) - num_arr2[1]); | |
System.out.println("Case " + count + ":"); | |
for(int j=0;j<num_arr2.length;j++) { | |
for(int i=0;i<lists.size();i++) { | |
temp = Math.abs(lists.get(i) - num_arr2[j]); | |
//res_lists.add(temp); | |
if(temp < min) { | |
index = lists.get(i); | |
min = temp; | |
} | |
} | |
System.out.println("Closest sum to " + num_arr2[j] + " is " + index + "."); | |
index = 0; | |
min = Math.abs(lists.get(0) - num_arr2[1]); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment