1.

/**
* Angewandte Mathematik, SS11
* Problem: 537 - Artificial Intelligence?
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=478
*
* @author Marco Schuster
* @author Gent Selmanaj
* @author Chris Westerfield
*
* Method : Ad-Hoc
* Status : Accepted (#8906029)
* Runtime: 0.124
*/
import java.text.DecimalFormat;
import java.util.Locale;
import java.util.Scanner;
import java.util.StringTokenizer;

public class Main {
enum states {
nothing, have_u, have_ui, have_up, have_i, have_iu, have_ip, have_p,
have_pi, have_pu
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// discard first line
sc.nextLine();
Integer cnt = 0;
while (sc.hasNextLine()) {
cnt++;
String line = sc.nextLine();
states currentState = states.nothing; // state
StringTokenizer lineTokenizer = new StringTokenizer(line, " "); // split
// by
// whitespace
Double[] params = new Double[2];
while (lineTokenizer.hasMoreTokens()) {
String currentToken = lineTokenizer.nextToken();
if (currentToken.contains("=")) { // check if this sequence
// contains an assignment
String[] a = currentToken.split("=");
if (!a[1].matches("(.*)[VWA]$")) { // check for left-over
// commas
a[1] = a[1].substring(0, a[1].length() - 1); // and
// remove
// them
}
a[1] = a[1].substring(0, a[1].length() - 1); // remove the
// unit, we
// get it by
// the left
// side of
// the
// assignment
// later
if (a[1].contains("m") || a[1].contains("k")
|| a[1].contains("M")) { // check for magnitude
// modifiers (m, k and
// M)
String ch = a[1].substring(a[1].length() - 1,
a[1].length()); // get the modifier
a[1] = a[1].substring(0, a[1].length() - 1); // get the
// raw
// value
double baseVal = Double.parseDouble(a[1]);
if (ch.equals("m"))
a[1] = new Double(baseVal * 0.001).toString();
else if (ch.equals("k"))
a[1] = new Double(baseVal * 1000).toString();
else if (ch.equals("M"))
a[1] = new Double(baseVal * 1000000).toString();
}

if (currentState == states.nothing)
params[0] = new Double(a[1]);
else if (currentState == states.have_i
|| currentState == states.have_u
|| currentState == states.have_p)
params[1] = new Double(a[1]);
// state automaton
if (a[0].toLowerCase().equals("u")
&& currentState == states.nothing)
currentState = states.have_u;
else if (a[0].toLowerCase().equals("u")
&& currentState == states.have_i)
currentState = states.have_iu;
else if (a[0].toLowerCase().equals("u")
&& currentState == states.have_p)
currentState = states.have_pu;
else if (a[0].toLowerCase().equals("i")
&& currentState == states.nothing)
currentState = states.have_i;
else if (a[0].toLowerCase().equals("i")
&& currentState == states.have_u)
currentState = states.have_ui;
else if (a[0].toLowerCase().equals("i")
&& currentState == states.have_p)
currentState = states.have_pi;
else if (a[0].toLowerCase().equals("p")
&& currentState == states.nothing)
currentState = states.have_p;
else if (a[0].toLowerCase().equals("p")
&& currentState == states.have_i)
currentState = states.have_ip;
else if (a[0].toLowerCase().equals("p")
&& currentState == states.have_u)
currentState = states.have_up;

}
}
System.out.println("Problem #" + cnt);
Double result = null;
String unit = null;
if (currentState == states.have_iu
|| currentState == states.have_ui) { // formula p=ui
result = new Double(params[0].doubleValue()
* params[1].doubleValue());
System.out.print("P=");
unit = "W";
} else if (currentState == states.have_up) { // formula i=p/u
result = new Double(params[1].doubleValue()
/ params[0].doubleValue());
System.out.print("I=");
unit = "A";
} else if (currentState == states.have_pu) { // formula i=p/u
result = new Double(params[0].doubleValue()
/ params[1].doubleValue());
System.out.print("I=");
unit = "A";
} else if (currentState == states.have_pi) { // formula u=p/i
result = new Double(params[0].doubleValue()
/ params[1].doubleValue());
System.out.print("U=");
unit = "V";
} else if (currentState == states.have_ip) {// formula u=p/i
result = new Double(params[1].doubleValue()
/ params[0].doubleValue());
System.out.print("U=");
unit = "V";
}
DecimalFormat df = (DecimalFormat) DecimalFormat
.getInstance(Locale.ENGLISH); // apply english separator
// (dot, not comma)
df.applyPattern("0.00");
System.out.println(df.format(result) + unit + "\n");
}
}

}
------------------------




1.

/**
* FWP: Ausgewaehlte Probleme aus dem ACM (SS10)
*
* Method: ad hoc
* Problem: 537 - Artificial Intelligence?
* Accepted: 0.108
* @author Evgeni Pavlidis
*
*/

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

class Main {

public static void main(String...args) throws Exception
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String input;
int testCases = Integer.parseInt(reader.readLine());
double current;
int j;


for(int c = 1; c <= testCases; c++)
{
double P = 0.0,U = 0.0 ,I = 0.0;
input = reader.readLine();
for(int i = 0; i < input.length(); i++)
if(input.charAt(i) == '=')
{
j = i+1;
while(Character.isDigit(input.charAt(j)) || input.charAt(j) == '.' )
j++;

current = Double.parseDouble(input.substring(i+1,j));

if(input.charAt(j) == 'm')
current /= 1000;
else if(input.charAt(j) == 'k')
current *= 1000;
else if(input.charAt(j) == 'M')
current *= 1000*1000;

if(input.charAt(i-1) == 'U')
U = current;
else if(input.charAt(i-1) == 'P')
P = current;
else if(input.charAt(i-1) == 'I')
I = current;
}

if(U == 0.0)
System.out.printf("Problem #%d\nU=%.2fV\n\n", c, P/I);
else if(P == 0.0)
System.out.printf("Problem #%d\nP=%.2fW\n\n", c, U*I);
else if(I == 0.0)
System.out.printf("Problem #%d\nI=%.2fA\n\n", c, P/U);
}
}
}