Sunday, August 14, 2016

PROMED@CS 2012 Question 7 (LegoLand Ticket)

Download Question Here

Change Snippet Background Color
          
import java.util.*;
import java.io.*;
import java.text.*;

/**
 * Created by Hafiq Iqmal on 14/8/2016.
 * PROMED CS 2012
 */

public class LegoLandTicket{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      String line = System.getProperty("line.separator");
      scan.useDelimiter(line);
   
      int cases = scan.nextInt();
      String [] ans = new String[cases];
      
      DecimalFormat df = new DecimalFormat("RM0.00");
      
      for(int x=0;x<ans.length;x++){
         String[] split = scan.next().trim().split(" ");
         int [] org = new int[split.length];
         for(int bil=0;bil<split.length;bil++){
            org[bil] = Integer.parseInt(split[bil]);
         }
         
         float child = 0,adult = 0, senior=0;
         int c=0,a=0,s=0;
         
         for(int y=1;y<=org[0];y++){
            if(org[y]>=3 && org[y]<=11){
               child+= 110;
               c++;
            }
            else if(org[y]>=12 && org[y]<=59){
               adult += 140;
               a++;
            }
            else if(org[y]>=60){
               senior += 110;
               s++;
            }
         }
         
         int voucher = org[org.length-1];
         float pv = 0;
         if(a>=c){
            if(voucher>=c){
               pv = c * 110;
            }
            else{
               pv = voucher * 110;   
            }
         }
         else{
            pv = a * 110;
         }
         
         ans[x] = "Cases #"+(x+1)+": "+df.format((adult+child+senior) - pv);
      }
      
      for(int x=0;x<cases;x++){
         System.out.println(ans[x]);
      }
   }
}
          
        

PROMED@CS 2012 Question 6 (Even Or Odd Wins!)

Download Question Here

Change Snippet Background Color
          
            
          

import java.util.*;
import java.io.*;
import java.math.*;

/**
 * Created by Hafiq Iqmal on 14/8/2016.
 * PROMED CS 2012
 */

public class EvenOrOdd{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      String line = System.getProperty("line.separator");
      scan.useDelimiter(line);
      
      int cases = scan.nextInt();
      String [] ans = new String[cases];
      
      for(int x=0;x<cases;x++){
         int num = scan.nextInt();
         
         int cube = num * num * num;
         boolean even = false;
         boolean odd = false;
         
         while(cube > 0){
            if(((cube % 10) % 2 == 0)){
               even = true;
            }
            else{
               odd = true;
            }
            
            cube /= 10;            
         }
         
                  
         if(even && !odd){
            ans[x] = "Case #"+(x+1)+": EVEN";
         }
         else if(!even && odd){
            ans[x] = "Case #"+(x+1)+": ODD";
         }
         else{
            ans[x] = "Case #"+(x+1)+": MIXED";
         }
      }
      
      for(int x=0;x<cases;x++){
         System.out.println(ans[x]);
      }
   }
}

        

PROMED@CS 2012 Question 5 (Dash Pattern)

Download Question Here

Change Snippet Background Color
          
import java.util.*;
import java.io.*;

/**
 * Created by Hafiq Iqmal on 14/8/2016.
 * PROMED CS 2012
 */

public class DashPattern{

   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      String line = System.getProperty("line.separator");
      scan.useDelimiter(line);
   
      int cases = scan.nextInt();
      String [] ans = new String[cases];
      
      for(int x=0;x<cases;x++){
         String[] split = scan.next().split("\\s+");
         
         String tiang = "|";
         String draw = "";
         
         int[] num = new int[split.length];
         
         for(int z=0;z<split.length;z++){
            num[z] = Integer.parseInt(split[z]);
         }
         
         int len = num[0];
         
         int current = 0;
         boolean selang = true;
         
         while(current < len){
            for(int y=1;y<num.length;y++){
                  for(int index=0;index<num[y];index++){
                     if (current < len){
                        if(y%2 == 1)
                           draw += "_";
                        else
                           draw += " ";
                     }
                     else{
                        break;
                     }
                     current ++;
                  }
                                
               selang = (selang)?false:true;
            }
         }
         
         ans[x] = "Case #"+(x+1)+": "+tiang+draw+tiang;
      }
      
      for(int x=0;x<cases;x++){
         System.out.println(ans[x]);
      }
      
   }

}
          
        

PROMED@CS 2012 Question 4 (Hail Taxi)

Download Question Here

Change Snippet Background Color

import java.util.*;
import java.io.*;

/**
 * Created by Hafiq Iqmal on 14/8/2016.
 * PROMED CS 2012
 * Using bruteforce closest pairing coordinate
 */

public class HailTaxi {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);
        String line = System.getProperty("line.separator");
        scan.useDelimiter(line);

        int cases = scan.nextInt();
        String[] ans = new String[cases];

        for (int x = 0; x < cases; x++) {
            List <Coordinate> list = new ArrayList <>();
            String[] p = scan.next().trim().split(" ");
            
            Coordinate player = new Coordinate(Double.parseDouble(p[0]), Double.parseDouble( p[1]));
            
            while (true) {
                String input = scan.next().trim();
                if (input.equals("10001 10001"))
                    break;
                else {
                    String[] in = input.split(" ");
                    list.add(new Coordinate(Double.parseDouble(in[0]), Double.parseDouble(in[1])));
                }
            }
            
            list.add(new Coordinate(0,0));
            Collections.sort(list);
                        
            Coordinate bf = findNearest(list,player);

            ans[x] = "Case #" + (x + 1) + ": " + bf;
        }

        for (int x = 0; x < cases; x++) {
            System.out.println(ans[x]);
        }
    }

    private static class Coordinate implements Comparable<Coordinate> {
        private double x;
        private double y;

        public Coordinate(double x, double y) {
            this.x = x;
            this.y = y;
        }

        public double getX() {
            return x;
        }

        public double getY() {
            return y;
        }
        
        @Override
        public int compareTo(Coordinate p){
            if(y - p.y != 0){
                return (int)(y - p.y);
            }
 
            if(x - p.x != 0){
                return (int)(x - p.x);
            }
 
            return 0;
        }

        public String toString() {
            return (int)x + " " + (int)y;
        }
    }

 
    private static double calDistance(Coordinate a,Coordinate p){
        return (a.getX()-p.getX())*(a.getX()-p.getX()) + (a.getY()-p.getY())*(a.getY()-p.getY());
    }

    private static Coordinate findNearest(List <Coordinate> points, Coordinate player) {        
        Coordinate closest = points.get(0);
        for (Coordinate coor:points) {            
            if (calDistance(coor,player) <= calDistance(closest, player))
               closest = coor;
        }
        
        return closest;
    }
   
}

PROMED@CS 2012 Question 3 (Count Sub Sequence)

Download Question Here

Change Snippet Background Color
          
import java.lang.reflect.Array;
import java.util.*;

/**
 * Created by Hafiq Iqmal on 14/8/2016.
 * PROMED CS 2012
 */
public class CountSubSequence{

    static final int MODULES = 1000000003;

    public static void main(String[] args){

        Scanner scan = new Scanner(System.in);
        String line = System.getProperty("line.separator");
        scan.useDelimiter(line);

        int cases = scan.nextInt();
        String[] ans = new String[cases];

        for (int x = 0; x < cases; x++) {

            String first = scan.next();
            String second = scan.next();

            List<String> fList = getForwardPermutation(first);
            List<String> sList = getForwardPermutation(second);

            ans [x] ="Case #"+(x+1)+": "+countSS(fList,sList);
        }

        for (int x = 0; x < cases; x++) {
            System.out.println(ans[x]);
        }
    }

    private static int countSS(List<String> flist,List<String> slist){
        // swap which is greater
        if (flist.size() < slist.size()){
            List<String> temp  = flist;
            flist = slist;
            slist = temp;
        }

        int count = 0;

        for (String aFlist : flist) {
            for (String aSlist : slist) {
                if (aFlist.equals(aSlist)) {
                    count++;
                }
            }
        }

        return count % MODULES;
    }

    private static List<String> getForwardPermutation(String text) {
        List<String> res = new ArrayList<>();
        for (int i = 0; i < text.length(); i++) {
            int lenRes = res.size();
            for (int j = 0; j < lenRes; j++) {
                res.add(text.charAt(i) + res.get(j));
            }
            res.add(Character.toString(text.charAt(i)));
        }
        // put null at first list
        res.add(0,"");

        return res;
    }

}
     
          
        

PROMED@CS 2012 Question 2 (Matrix Reloaded)

Download Question Here

Change Snippet Background Color

import java.util.*;

/**
 * Created by Hafiq Iqmal on 14/8/2016.
 * PROMED CS 2012
 */


public class MatrixReloaded{
    public static void main(String[] args){

        Scanner scan = new Scanner(System.in);
        String line = System.getProperty("line.separator");
        scan.useDelimiter(line);

        int cases = scan.nextInt();
        String [] ans = new String[cases];

        for(int x=0;x<cases;x++){
            String binary = scan.next();
            int len = binary.length();
            boolean range = (len > 100 && len < 65535);
            int count = 0;
            if(range){
                for(int y=0;y<len;y++){
                    if(binary.charAt(y) == '1'){
                        int z = y+1;
                        if (z>=len){
                            if(binary.charAt(y) == '1') {
                                count++;
                            }
                            break;
                        }
                        else {
                            while (true) {
                                if (z < len) {
                                    if (binary.charAt(z) == '0') {
                                        y = z;
                                        count++;
                                        break;
                                    }
                                } else {
                                    break;
                                }

                                z++;
                            }
                        }
                    }
                    else{
                        y++;
                    }
                }
                ans[x] = "Cases #"+(x+1)+": "+count;
            }
            else{
                ans[x] = "Cases #"+(x+1)+": -1";
            }
        }

        for (int x=0;x<cases;x++){
            System.out.println(ans[x]);
        }
    }
}

PROMED@CS 2012 Question 1 (P-LOCK)

Download Question Here

Change Snippet Background Color

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

/**
 * Created by Hafiq Iqmal on 14/8/2016.
 * PROMED CS 2012
 */
public class PLock {

    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        String line = System.getProperty("line.separator");
        scan.useDelimiter(line);

        int cases = scan.nextInt();
        String [] ans = new String[cases];

        for(int x=0;x2 && get.matches("[A-Z]+")){
                if (!findDuplicate(get))
                    ans[x] = "Case #"+(x+1)+": VALID";
                else
                    ans[x] = "Case #"+(x+1)+": INVALID";
            }
            else
                ans[x] = "Case #"+(x+1)+": INVALID";

        }

        for (int x=0;x map = new HashMap<>();
        char[] getChar = str.toCharArray();
        for(Character isDuplicate:getChar){
            if(map.containsKey(isDuplicate)){
               // plus 1 if found same character     
               map.put(isDuplicate, map.get(isDuplicate)+1);
            } else {
               // add 1 to new Character
               map.put(isDuplicate, 1);
            }
        }
        Set keys = map.keySet();
        for(Character isDuplicate:keys){
            if(map.get(isDuplicate) > 1){
                //found more than 1
                return true;
            }
        }

        return false;
    }
}