__rain1

Krebota

May 30th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.22 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3.  
  4. public class Krebota{
  5.     public static void main(String[] args) {
  6.  
  7.     getHeliumFraction(6, 6, 1, 3, 2, 2, 3);
  8.  
  9.     }
  10.  
  11.     public static void getHeliumFraction(int n, int... s){
  12.  
  13.         boolean possible = true;
  14.         int[] balloons = new int[n];
  15.        
  16.         int[] cannisters = new int[n];
  17.        
  18.  
  19.         for(int i = 1; i <= n ; i++ ){
  20.             balloons[i - 1] = i;
  21.             System.out.println(i);
  22.         }
  23.  
  24.  
  25.         for(int i = 0 ; i < s.length ; i ++){
  26.             cannisters [i] = s[i];
  27.         }
  28.  
  29.         Arrays.sort(cannisters);
  30.  
  31.         if (cannisters.length == balloons.length ){
  32.  
  33.            
  34.            
  35.  
  36.             double currNum = 1;
  37.             int numerator = 0;
  38.             int denominator = 1;
  39.  
  40.             for(int i = 0 ; i < cannisters.length ; i++) {
  41.  
  42.                 int a = cannisters[i];
  43.            
  44.                 if(a > balloons[i]){
  45.                    
  46.                     System.out.println("impossible");
  47.                     possible = false;
  48.                     break;
  49.                 }
  50.  
  51.                 double fraction = a/balloons[i] ;
  52.  
  53.                 if(fraction <= currNum){
  54.  
  55.                     currNum = fraction;
  56.                     denominator = balloons[i] ;
  57.                     numerator  = a;
  58.                    
  59.                 }
  60.             }
  61.  
  62.             if(possible){
  63.                 System.out.println("The fraction needed is " + numerator + "/" + denominator);
  64.             }
  65.         }else{
  66.             System.out.println("The number of cannisters must be same as that of balloons.");
  67.         }
  68.  
  69.  
  70.     }
  71.  
  72. }
Add Comment
Please, Sign In to add comment