Java Codes by Gamer
Sunday, September 05, 2004
 
Google Code Jam 2004 Practice Room 500 Point

Problem Statement

A simple network of resistors can be defined recursively as:simple network ::= a simple network in parallel with another simple networksimple network ::= a simple network in series with another simple networksimple network ::= a single resistorIf two networks are in parallel with each other and have resistances r1 and r2, then the total resistance is 1/(1/r1+1/r2). If two networks are in series with each other and have resistances r1 and r2, then the total resistance is r1+r2. If a network is a single resistor, then its resistance is defined as the resistance of that single resistor.You will be given a simple network of resistors represented as a String[], resistors. Each element of resistors will be either "P", "S", or a decimal number. If an element is a number, then it represents a single resistor with resistance defined by that number. If an element is "P", it defines a simple network whose two component simple networks consist of the next two simple networks in resistors. Similarly, if an element is "S", then the next two simple networks in resistors are in series. Thus, the total number of elements that are numbers will be equal to the total number of elements that are "S" or "P" plus 1. For example, {"S","5.3","P","40","60"} represents a simple network where a single resistor with resistance 5.3 is in series with two resistors in parallel with resistances 40 and 60:



+----- 40 ----+
| |
---- 5.3 ---+ +----
| |
+----- 60 ----+





Your task is to compute the total resistance of the network, and return the result as a double. Small rounding errors
will be ignored when examining your result for correctness. As long as your result has a relative or absolute error
of less than 1e-9, it will be judged correct.







import java.util.*;

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

double result;
String[][] param0 = {{"S","5.3","P","40","60"},
{"S","P","P","1.65","7.59","S","75","1.2","10"}, {"032.350"}};

double[] result_expected = {29.3,11.331670926296885, 32.35};

for(int count = 0; count < result_expected.length; count++)
{
result = getResistance(param0[count]);
if(result != result_expected[count])
System.out.println("Failure:" + result);
else
System.out.println("Success:" + result);
}
}


public static double getResistance(String[] resisters)
{
double ans = 0.0;

Stack myStack = new Stack();


if(resisters.length > 1 )
{
myStack.push( resisters[0] );
myStack.push( resisters[1] );
myStack.push( resisters[2] );
}
else
{
myStack.push( resisters[0] );
}



int j = 3;

while( myStack.size() != 1 )
{
boolean flag = true;

while( myStack.size() >= 3 && flag )
{
String one = (String)myStack.pop();
String two = (String)myStack.pop();
String op = (String)myStack.pop();

if( one.equals("P") || one.equals("S") || two.equals("P")
|| two.equals("S") || !( op.equals("P") || op.equals("S")) )
{
myStack.push(op);
myStack.push(two);
myStack.push(one);
flag = false;
}
else
{
double done = Double.parseDouble( one );
double dtwo = Double.parseDouble( two );

double res = 0.0;

if( op.equals("P") )
{
res = 1/( 1/done + 1/dtwo );
}
else
{
res = done + dtwo;
}

myStack.push("" + res);
flag = true; //check again
}
}



//if still top three elements are solvable revert


if( j != resisters.length )
{
myStack.push( resisters[j] );
j++;
}
}


return Double.parseDouble( (String)myStack.pop() );
}
}



Comments:
The day was getting on and I needed to spend the evening putting thefinishing touches to my scheme. I need to get out there and do something.
true adult diaper stories
literotica stories first time
gay deman erotic stories
virgin rape stories
bestiality stories with pictures
The day was getting on and I needed to spend the evening putting thefinishing touches to my scheme. I need to get out there and do something.
 
Post a Comment

<< Home

Powered by Blogger