SHARE
TWEET

Untitled

a guest Feb 16th, 2020 89 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package project1_demo;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.PrintWriter;
  7. import java.util.Arrays;
  8. import java.util.Scanner;
  9.  
  10. public class Project1_demo {
  11.  
  12.     public static void main(String[] args) throws IOException {
  13.         FileInputStream inputFiles[] = new FileInputStream[9];
  14.         for (int i = 0; i < 9; i++) {
  15.             inputFiles[i] = new FileInputStream("Photo_" + String.valueOf(i + 1) + ".ppm");
  16.         }
  17.         Scanner scanners[] = new Scanner[9];
  18.         for (int i = 0; i < 9; i++) {
  19.             scanners[i] = new Scanner(inputFiles[i]);
  20.         }
  21.         FileOutputStream outputFile = new FileOutputStream("output.ppm");
  22.         PrintWriter writer = new PrintWriter(outputFile);
  23.        
  24.         //Jump over the first 4 values in each input file.
  25.         for (int i = 0; i < 9; i++) {
  26.             scanners[i].next();
  27.             scanners[i].next();
  28.             scanners[i].next();
  29.             scanners[i].next();
  30.         }
  31.    
  32.         //Write the "header" to the output file.
  33.         writer.println("P3\n800 600\n255");
  34.         for (int i = 0; i < 800 * 600 * 3; i++) {
  35.             int[] arr = new int[9];
  36.             for (int j = 0; j < 9; j++) {
  37.                 arr[j] = scanners[j].nextInt();            
  38.             }
  39.            
  40.             //Get the majority element in arr.
  41.             //1) Sort the arr,
  42.             //2) Get the median.
  43.             Arrays.sort(arr);
  44.             writer.println(arr[arr.length / 2]);
  45.         }
  46.        
  47.         writer.close();
  48.         outputFile.close();
  49.         for (int i = 0; i < 9; i++) {
  50.             scanners[i].close();
  51.             inputFiles[i].close();
  52.         }
  53.        
  54.     }
  55.    
  56. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top