Find us on Google+ Kill the code: event
Showing posts with label event. Show all posts
Showing posts with label event. Show all posts

Sunday, 31 March 2013

Rope Intranet : Google Code Jam


 Problem Statement 
Problem
A company is located in two very tall buildings. The company intranet connecting the buildings consists of many wires, each connecting a window on the first building to a window on the second building.
You are looking at those buildings from the side, so that one of the buildings is to the left and one is to the right. The windows on the left building are seen as points on its right wall, and the windows on the right building are seen as points on its left wall. Wires are straight segments connecting a window on the left building to a window on the right building.
 
You've noticed that no two wires share an endpoint (in other words, there's at most one wire going out of each window). However, from your viewpoint, some of the wires intersect midway. You've also noticed that exactly two wires meet at each intersection point.
On the above picture, the intersection points are the black circles, while the windows are the white circles.
How many intersection points do you see?
Input
The first line of the input gives the number of test cases, T. T test cases follow. Each case begins with a line containing an integer N, denoting the number of wires you see.
The next N lines each describe one wire with two integers Ai and Bi. These describe the windows that this wire connects: Ai is the height of the window on the left building, and Bi is the height of the window on the right building.

Rope Intranet : Google Code Jam


public class rope_intranet {
    static int input[][] = {{5,6},{7,7},{4,3},{2,1},{1,5}};
    public static void main(String args[]) {
        int row = input.length;
        int count = 0;
        for(int t=0;t input[i][1]) {
                        count++;
                    }
                }
            }
        }
        System.out.println("count = " + count);
    }
}

Sunday, 18 November 2012

Store Credit in JAVA

This program help customer to buy the most expensive products from all the available in the shop. This program takes from user the available amount with him and cost of all the available products in the shop. Than it finally gives the index number of the products that are most suitable.



public class store_credit {
    public static void main(String[] args) {
        int credit = 100; /*This is the available credit to the user.*/
    int total = 3; /*This is the total number of the products in shop.*/
    int price[] = {5,75,25}; /*This is the price array of all the items.*/
    int size = total;
    int temp = 0;
    int i;
    int pos1=0,pos2=0;
    for(i=0;i<size;i++) {
            for(int j=0;j<size;j++) {
                int t = price[i]+price[j];
        if( t > temp && t <= credit && i != j) {
                    temp = t;
                    pos1 = i + 1;
                    pos2 = j + 1;
        }
        }
    }
        System.out.println("Case#1: "+pos1 + " "+pos2);
    }
}

Reverse String Dramatically in JAVA

Input    : my name is nisarg mehta
Output : mehta nisarg is name my


public class reverse_string {
    public static void main(String args[]) {
        String s = "my name is nisarg mehta";
        String output = "";
        int space = 4;
        String temp = "";
        int i = s.length();
        int j = i;
        while(space >= 0) {
            while(i>=1 && s.charAt(--i) != ' ') {
            }
            temp = s.substring(i,j);
            j = i;
            output += " " + temp;
            temp = "";
            space--;
        }
        System.out.println("Case#1 : "+output);
    }
}

Thursday, 8 March 2012

Draw Table with required rows and cols in JAVA



import java.awt.*;
import java.applet.*;
import java.awt.event.*;


/*
<applet code="mousemotion" width=400 height=400>
</applet>
*/


public class mousemotion extends Applet implements MouseMotionListener,MouseListener {
String name="NISARG";
//String dim=" ";
int mx,my,mx2,my2,mx1,my1,mx3,my3;
int reles=0,dragged=0,drg_reles=0,rls_aftr_drg=0,clr_screen=0;
int ROW=12;
int COLOUM=12;



public void init() {
addMouseMotionListener(this);
addMouseListener(this);
}
public void mouseClicked(MouseEvent me) {
showStatus("MOUSE CLICKED.");
clr_screen=1;
repaint();
}
public void mousePressed(MouseEvent me) {
mx1=me.getX();
my1=me.getY();
}
public void mouseEntered(MouseEvent me) {
showStatus("SHOW STATUS.");
}
public void mouseReleased(MouseEvent me) {
if(dragged==1) {
drg_reles=1;
}
mx2=me.getX();
my2=me.getY();
dragged=0;
reles=1;
repaint();
}
public void mouseExited(MouseEvent me) {
showStatus("MOUSE EXITED.");
}
public void mouseDragged(MouseEvent me) {
showStatus("Mouse is at "+me.getX()+" "+me.getY());
mx3=me.getX();
my3=me.getY();
rls_aftr_drg=1;
//drg_reles=1;
dragged=1;
repaint();
}
public void mouseMoved(MouseEvent me) {
showStatus("Mouse is at "+me.getX()+" "+me.getY());
}
public void paint(Graphics g) {
if(clr_screen==1) {
clr_screen=0;
}
else if(reles==1 && dragged==1) { //MOUSE RELEASED.
g.drawLine(mx1,my1,mx2,my1);
g.drawLine(mx1,my1,mx1,my2);
g.drawLine(mx1,my2,mx2,my2);
g.drawLine(mx2,my1,mx2,my2);
int ROW11=(my3-my1)/ROW;
int ROW111=ROW11;
ROW11+=my1;
int COLOUM11=(mx3-mx1)/COLOUM;
int COLOUM111=COLOUM11;
COLOUM11+=mx1;
for(int i=0;i<ROW;i++) {
g.drawLine(mx1,ROW11,mx3,ROW11);
ROW11+=ROW111;

}
//g.drawLine(mx1,ROW11,mx3,ROW11);
for(int j=0;j<COLOUM;j++) {
g.drawLine(COLOUM11,my1,COLOUM11,my3);
COLOUM11+=COLOUM111;
}
reles=0;
}
else if(rls_aftr_drg==1) { //MOUSE DRAGGED.
g.drawLine(mx1,my1,mx3,my1);
g.drawLine(mx1,my1,mx1,my3);
g.drawLine(mx1,my3,mx3,my3);
g.drawLine(mx3,my1,mx3,my3);
int ROW11=(my3-my1)/ROW;
int ROW111=ROW11;
ROW11+=my1;
int COLOUM11=(mx3-mx1)/COLOUM;
int COLOUM111=COLOUM11;
COLOUM11+=mx1;
for(int i=0;i<ROW;i++) {
g.drawLine(mx1,ROW11,mx3,ROW11);
ROW11+=ROW111;

}
for(int j=0;j<COLOUM;j++) {
g.drawLine(COLOUM11,my1,COLOUM11,my3);
COLOUM11+=COLOUM111;
}
dragged=0;
//rls_aftr_drg=0;
}
if(drg_reles==1) { //MOUSE RELEASED.
g.drawLine(mx1,my1,mx2,my1);
g.drawLine(mx1,my1,mx1,my2);
g.drawLine(mx1,my2,mx2,my2);
g.drawLine(mx2,my1,mx2,my2);
int ROW11=(my3-my1)/ROW;
int ROW111=ROW11;
ROW11+=my1;
int COLOUM11=(mx3-mx1)/COLOUM;
int COLOUM111=COLOUM11;
COLOUM11+=mx1;
for(int i=0;i<ROW;i++) {
g.drawLine(mx1,ROW11,mx3,ROW11);
ROW11+=ROW111;

}
for(int j=0;j<COLOUM;j++) {
g.drawLine(COLOUM11,my1,COLOUM11,my3);
COLOUM11+=COLOUM111;
}
drg_reles=0;
}
}
}

Saturday, 25 February 2012

Moving Circle in JAVA

An applet program to move circle in JAVA

import java.applet.*;
import java.awt.*;

/*
  <applet code="moving_circle" width=300 height=100>
  </applet>
*/

public class moving_circle extends Applet implements Runnable
{
    int x=0;
    Thread t;
    Image buffer;
    Graphics bufferg;

    public void init()
    {
        t=new Thread(this);
        t.start();
        Dimension d=getSize();
        buffer=createImage(d.width,d.height);
    }
    public void run()
    {
        try
        {
            while(true)
            {
               repaint();
               Thread.sleep(500);
            }
        }
        catch(Exception e)
        {
        }
    }

    public void update(Graphics g)
    {
        paint(g);
    }

    public void paint(Graphics g)
    {
        if(bufferg==null)
            bufferg=buffer.getGraphics();

        Dimension d=getSize();
        bufferg.setColor(Color.black);
        bufferg.fillRect(0,0,d.width,d.height);
        bufferg.setColor(Color.white);
        bufferg.fillOval(x,d.height/4,50,50);

        g.drawImage(buffer,0,0,this);
        x+=5;
        if(x+50>d.width)
            x=0;
    }
}

Wednesday, 22 February 2012

Set background color on mouse click


import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*
<applet code="Brighter" width=300 height=400>
</applet>
*/
public class Brighter extends Applet implements MouseListener
{
int i;
Color color;
public void init()
{
color=new Color(i,i,i);
setBackground(color);
addMouseListener(this);
}
public void mouseClicked(MouseEvent me)
{
i+=10;
if(i>255)
{
i=255;

}
color=new Color(i,i,i);
setBackground(color);
repaint();


}
public void mouseEntered(MouseEvent me)
{
}
public void mouseExited(MouseEvent me)
{

}
public void mousePressed(MouseEvent me)
{
}
public void mouseReleased(MouseEvent me)
{
}
}