Advertisement
Guest User

Untitled

a guest
May 25th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.11 KB | None | 0 0
  1. import java.util.Scanner;
  2. import java.util.Vector;
  3. import java.util.Arrays;
  4. import java.io.File;
  5.  
  6. class TripleSum{
  7.  
  8.     public static boolean tripleSum(int[] A){
  9.         return findTriple(A,225);
  10.     }
  11.  
  12.     public static boolean findTriple(int[] A, int sum){
  13.         int n = A.length;
  14.         boolean[] refTable = new boolean[sum+1];
  15.  
  16.         //Count all instaces of 0 <= x <= 225
  17.         for(int i = 0; i < n; i++){
  18.             if( (A[i] <= sum) && !refTable[A[i]]){
  19.                 refTable[A[i]] = true;
  20.             }
  21.         }
  22.         //Try and find a pair of numbers that add to sum
  23.         for(int i = 0; i <= sum/2; i++){
  24.             if(refTable[i] && refTable[sum-i]){
  25.                 //If so, try and break at least one of those numbers into another pair
  26.                 for(int j = 0; j <= (sum-i)/2; j++){
  27.                     if((i-j) > 0){
  28.                         if(refTable[j] && refTable[i-j]){
  29.                             return true;
  30.                         }
  31.                     }
  32.                     else{
  33.                         if(refTable[j] && refTable[sum-i-j]){
  34.                             return true;
  35.                         }
  36.                     }
  37.                 }
  38.             }
  39.         }
  40.  
  41.         return false;
  42.     }
  43.  
  44.     public static void main(String[] args){
  45.         Scanner s;
  46.         if (args.length > 0){
  47.             try{
  48.                 s = new Scanner(new File(args[0]));
  49.             } catch(java.io.FileNotFoundException e){
  50.                 System.out.printf("Unable to open %s\n",args[0]);
  51.                 return;
  52.             }
  53.             System.out.printf("Reading input values from %s.\n",args[0]);
  54.         }else{
  55.             s = new Scanner(System.in);
  56.             System.out.printf("Enter a list of non-negative integers. Enter a negative value to end the list.\n");
  57.         }
  58.         Vector<Integer> inputVector = new Vector<Integer>();
  59.  
  60.         int v;
  61.         while(s.hasNextInt() && (v = s.nextInt()) >= 0)
  62.             inputVector.add(v);
  63.  
  64.         int[] array = new int[inputVector.size()];
  65.  
  66.         for (int i = 0; i < array.length; i++)
  67.             array[i] = inputVector.get(i);
  68.  
  69.         System.out.printf("Read %d values.\n",array.length);
  70.  
  71.  
  72.         long startTime = System.currentTimeMillis();
  73.  
  74.         boolean tripleExists = tripleSum(array);
  75.  
  76.         long endTime = System.currentTimeMillis();
  77.  
  78.         double totalTimeSeconds = (endTime-startTime)/1000.0;
  79.  
  80.         System.out.printf("Array %s a triple of values which add to 225.\n",tripleExists? "contains":"does not contain");
  81.         System.out.printf("Total Time (seconds): %.4f\n",totalTimeSeconds);
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement