Friday, 15 November 2013

UVA Problem ID 10281 (Average Speed)

UVA Problem ID 10281 (Average Speed)
Note :- it's not necessary to have a "speed" input at the end of the first line and if that’s the case we have to print distance 0.00 km (distance must be printed for every query.)


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;

public class Main {

  public static void main(String[] args) throws IOException 
     {
         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
         String input;
         StringBuffer sb=new StringBuffer("");
         String lasttime="";
         double distance=0;
         double lastspeed=0;
         int t=0;
         while((input=br.readLine())!=null)
         {
          String s[]=input.split(" ");
          if(s.length==1)
          {
           if(lasttime!="")
           {
            String s1[]=s[0].split(":");
            double time1=Double.parseDouble(s1[0])+Double.parseDouble(s1[1])/60+Double.parseDouble(s1[2])/3600;
            String s2[]=lasttime.split(":");
            double time2=Double.parseDouble(s2[0])+Double.parseDouble(s2[1])/60+Double.parseDouble(s2[2])/3600;
            //calculate timediff=s[0]-lasttime;
            //distance1=distance+lastspeed*timediff;
            //sop (s[0]+" "+distance1+" km");
            double timediff=time1-time2;
            
            double distance1=distance+lastspeed*timediff;
            DecimalFormat df = new DecimalFormat("0.00");
            sb.append(s[0]+" "+df.format(distance1)+" km");
            sb.append("\n");
            
           }
    else
           {
            sb.append(s[0]+" 0.00 km");
            sb.append("\n");
           }
           
          }
          else
          {
           if(t==0)
           {
            t++;
            lasttime=s[0];
            lastspeed=Double.parseDouble(s[1]);
           }
           else
           {
            String s1[]=s[0].split(":");
            double time1=Double.parseDouble(s1[0])+Double.parseDouble(s1[1])/60+Double.parseDouble(s1[2])/3600;
            String s2[]=lasttime.split(":");
            double time2=Double.parseDouble(s2[0])+Double.parseDouble(s2[1])/60+Double.parseDouble(s2[2])/3600;
            double timediff=time1-time2;
            distance=distance+lastspeed*timediff;
            //calculate timediff=s[0]-lasttime;
            //distance=lastspeed*timediff;
            lasttime=s[0];
            lastspeed=Double.parseDouble(s[1]);
           }
          }
         }
         System.out.print(sb);
     }
}

UVA Problem ID 10141 (Request for Proposal)

UVA Problem ID 10141 (Request for Proposal)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main
{

    public static void main(String[] args) throws IOException 
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input;
        StringBuffer sb=new StringBuffer("");
        int rfpno=0;
        while((input=br.readLine())!=null)
        {
        	rfpno++;
        	String best[]=new String[3];
        	String s[]=input.split(" ");
        	best[2]="-1";
        	int n=Integer.parseInt(s[0]);
        	int p=Integer.parseInt(s[1]);
        	
        	if(n<0 || n>1000)
        	{
        		break;
        	}
        	if(n==0 && p==0)
        		break;
        	for (int i = 0; i 80)
					break;
			}
        	for (int i = 0; i < p; i++) {
        		String proposalname=br.readLine();
            	if(proposalname.length()>80)
    				break;
            	String s1[]=br.readLine().split(" ");
            	double price=Double.parseDouble(s1[0]);
            	int requiementmet=Integer.parseInt(s1[1]);
            	if(requiementmet<0 || requiementmet>n)
            		break;
//            	System.out.println("name "+proposalname);
//            	System.out.println("price "+price);
//            	System.out.println("requirement met "+requiementmet);
            	if(requiementmet>Integer.parseInt(best[2]))
            	{
            		best[0]=proposalname;
            		best[1]=price+"";
            		best[2]=requiementmet+"";
            	}
            	else if(requiementmet==Integer.parseInt(best[2]))
            	{
            		if(price1)
        	{
        		sb.append("\n");
        	}
        	sb.append("RFP #"+rfpno);
        	sb.append("\n");
        	sb.append(best[0]);
        	sb.append("\n");
        }
        System.out.print(sb);
        
    }
}

Thursday, 14 November 2013

UVA Problem ID 10082 ( WERTYU )

UVA Problem ID 10082 ( WERTYU )
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main
{

    public static void main(String[] args) throws IOException 
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String keyboard="`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";
        String input;
        StringBuffer sb=new StringBuffer("");
        while((input=br.readLine())!=null)
        {
         for (int i = 0; i < input.length(); i++) {
    int index=keyboard.indexOf(input.charAt(i));
    if(index==-1)
    {
     sb.append(input.charAt(i));
    }
    else
     sb.append(keyboard.charAt(index-1));
   }
         sb.append("\n");
        } 
        System.out.print(sb);
    }
}

UVA Problem ID 739 (Soundex Indexing )

UVA Problem ID 739 (Soundex Indexing )
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main
{

    public static void main(String[] args) throws IOException 
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String input;
        StringBuffer sb1 = new StringBuffer("");
        for (int i = 0; i <9; i++) {
         sb1.append(" ");
  }
        sb1.append("NAME");
        while(sb1.length()<34)
        {
         sb1.append(" ");
        }
        sb1.append("SOUNDEX CODE");
        
     StringBuffer sb = new StringBuffer("");

        while((input=br.readLine())!=null) 
        {
            if(input.length()>20)
             break;
            int previousvalue=-1;
            for (int i = 0; i <9; i++) {
             sb.append(" ");
      }
            sb.append(input);
            for (int i = 0; i <34-9-input.length(); i++) {
             sb.append(" ");
      }
//            while(sb.length()<34)
//            {
//             sb.append(" ");
//            }
            int length=0;
         char letters[]=input.toCharArray();
         for (int i = 0; i < letters.length; i++) 
         {
          switch (letters[i]) 
          {
           case 'A':
     case 'E':
     case 'I':
     case 'O':
     case 'U':
     case 'Y':
     case 'W':
     case 'H':
     {
      if(i==0)
      {
       sb.append(input.charAt(0));
      }
      previousvalue=-1;
      break;
     }
     case 'B':
     case 'P':
     case 'F':
     case 'V':
     {
      if(i==0)
      {
       sb.append(input.charAt(0));
       previousvalue=1;
      }
      else
      {
       if(previousvalue!=1)
       {
        sb.append(1);
        length++;
       }
       previousvalue=1;
       
      }
      break;
     }
     case 'C':
     case 'S':
     case 'K':
     case 'G':
     case 'J':
     case 'Q':
     case 'X':
     case 'Z':
     {
      if(i==0)
      {
       sb.append(input.charAt(0));
       previousvalue=2;
      }
      else
      {
       if(previousvalue!=2)
       {
        sb.append(2);
        length++;
       }
       previousvalue=2;
      }
      break;
     }
     case 'D':
     case 'T':
     {
      if(i==0)
      {
       sb.append(input.charAt(0));
       previousvalue=3;
      }
      else
      {
       if(previousvalue!=3)
       {
        sb.append(3);
        length++;
       }
       previousvalue=3;
      }
      break;
     }
     case 'L':
     {
      if(i==0)
      {
       sb.append(input.charAt(0));
       previousvalue=4;
      }
      else
      {
       if(previousvalue!=4)
       {
        sb.append(4);
        length++;
       }
       previousvalue=4;    
      }
      break;
     }
     case 'M':
     case 'N':
     {
      if(i==0)
      {
       sb.append(input.charAt(0));
       previousvalue=5;
      }
      else
      {
       if(previousvalue!=5)
       {
        sb.append(5);
        length++;
       }
       previousvalue=5;
      }
      break;
     }
     case 'R':
     {
      if(i==0)
      {
       sb.append(input.charAt(0));
       previousvalue=6;
      }
      else
      {
       if(previousvalue!=6)
       {
        sb.append(6);
        length++;
       }
       previousvalue=6;
      }
      break;
     }
          }          
         }
         if(length<3)
      {
       while(length!=3)
       {
        sb.append(0);
        length++;
       }
      }
         else if(length>3)
       sb=sb.delete(sb.length()-length+3,sb.length());
      sb.append("\n");
        } 
        System.out.println(sb1);
     System.out.print(sb);

        for (int i = 0; i <19; i++) {
   System.out.print(" ");
  }
        System.out.println("END OF OUTPUT");
    }
}

Wednesday, 13 November 2013

UVA Problem ID 661 ( Blowing Fuses )

UVA Problem ID 661 ( Blowing Fuses )

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuffer sb = new StringBuffer("");
        int ind=1;
        while (true) {
            String[] str = br.readLine().split(" ");
            int n = Integer.parseInt(str[0]);
            int m = Integer.parseInt(str[1]);
            int c = Integer.parseInt(str[2]);
            if (n == 0 && m == 0 && c == 0) {
                break;
            }
            boolean[] used = new boolean[n];
            int[] cap = new int[n];
            for (int i = 0; i < n; i++) {
                cap[i] = Integer.parseInt(br.readLine());
            }
            int sum = 0, max = -1;
            boolean over = false;
            for (int i = 0; i < m; i++) {
                int index = Integer.parseInt(br.readLine())-1;
                if (!over) {
                    if (!used[index]) {
                        used[index] = true;
                        sum += cap[index];
                        if (sum > max) {
                            max = sum;
                        }
                        if (sum > c) {
                            over = true;
                            continue;
                        }
                    } else {
                        used[index] = false;
                        sum -= cap[index];
                    }
                }
            }
            sb.append("Sequence ").append(ind).append("\n");
            if(over){
               sb.append("Fuse was blown.").append("\n");
            }else{
               sb.append("Fuse was not blown.").append("\n");
               sb.append("Maximal power consumption was ").append(max).append(" amperes.").append("\n");
            }
            sb.append("\n");
            ind++;
        }
        System.out.print(sb);
    }
}

UVA Problem ID 573 ( The Snail )

UVA Problem ID 573 ( The Snail )

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

public class Main {

     static String ReadLn (int maxLg)  // utility function to read from stdin
     {
         byte lin[] = new byte [maxLg];
         int lg = 0, car = -1;
         String line = "";

         try
         {
             while (lg < maxLg)
             {
                 car = System.in.read();
                 if ((car < 0) || (car == '\n')) break;
                 lin [lg++] += car;
             }
         }
         catch (IOException e)
         {
             return (null);
         }

         if ((car < 0) && (lg == 0)) return (null);  // eof
         return (new String (lin, 0, lg));
     }

     public static void main (String args[])  // entry point from OS
     {
         Main myWork = new Main();  // create a dynamic instance
         myWork.Begin();            // the true entry point
     }

     void Begin()
     {
         String input;
         StringTokenizer st;
         while ((input = Main.ReadLn (255)) != null)
         {
          st=new StringTokenizer(input);
          int H=Integer.parseInt(st.nextToken());
          int U=Integer.parseInt(st.nextToken());
          int D=Integer.parseInt(st.nextToken());
          int F=Integer.parseInt(st.nextToken());
          if(H==0)
          {
           System.exit(0);
          }
          float previousheight=0;
          int days=1;
          while(true)
          {
           float climbingdistance=(U-((days-1)*U*(float)F/100));
           float y=previousheight;
           if(climbingdistance > 0)
           {
            y=previousheight+climbingdistance;
            //System.out.println("y ="+y+" previousheight "+previousheight);
           }
           if(y>H)
           {
            System.out.println("success on day "+days);
            break;
           }
           if(y<=H && y>=0)
           {
            y=y-D;
           }
           if(y<0)
           {
            System.out.println("failure on day "+days);
            break;
           }
           days++;
           previousheight=y;
           
          }
         }
     }
}

UVA Problem ID 272 ( TeX Quotes )

UVA Problem ID  272 ( TeX Quotes )

import java.io.*;

public class Main {

    static String ReadLn (int maxLg)  // utility function to read from stdin
    {
        byte lin[] = new byte [maxLg];
        int lg = 0, car = -1;
        String line = "";

        try
        {
            while (lg < maxLg)
            {
                car = System.in.read();
                if ((car < 0) || (car == '\n')) break;
                lin [lg++] += car;
            }
        }
        catch (IOException e)
        {
            return (null);
        }

        if ((car < 0) && (lg == 0)) return (null);  // eof
        return (new String (lin, 0, lg));
    }

    public static void main (String args[])  // entry point from OS
    {
        Main myWork = new Main();  // create a dynamic instance
        myWork.Begin();            // the true entry point
    }

    void Begin()
    {
        String input;
        StringBuilder s;
          boolean starting=true;

        while ((input = Main.ReadLn (255)) != null)
        {
                s=new StringBuilder(input);
          while(s.indexOf("\"")!=-1)
          {
                if(starting)
                {
                        s.replace(s.indexOf("\""),s.indexOf("\"")+1, "``");
                        starting=false;
                }
                else {
                        s.replace(s.indexOf("\""),s.indexOf("\"")+1, "''");
                        starting=true;
}
          }
          System.out.println(s);
        }
    }
}

Advertisement