2013
11-11

# Artificial Intelligence?

Physics teachers in high school often think that problems given as text are more demanding than pure computations. After all, the pupils have to read and understand the problem first!

So they don’t state a problem like “U=10V, I=5A, P=?” but rather like “You have an electrical circuit that contains a battery with a voltage of U=10V and a light-bulb. There’s an electrical current of I=5A through the bulb. Which power is generated in the bulb?”.

However, half of the pupils just don’t pay attention to the text anyway. They just extract from the text what is given: U=10V, I=5A. Then they think: “Which formulae do I know? Ah yes, P=U*I. Therefore P=10V*5A=500W. Finished.”

OK, this doesn’t always work, so these pupils are usually not the top scorers in physics tests. But at least this simple algorithm is usually good enough to pass the class. (Sad but true.)

Today we will check if a computer can pass a high school physics test. We will concentrate on the P-U-I type problems first. That means, problems in which two of power, voltage and current are given and the third is wanted.

Your job is to write a program that reads such a text problem and solves it according to the simple algorithm given above.

The first line of the input will contain the number of test cases.

Each test case will consist of one line containing exactly two data fields and some additional arbitrary words. A data field will be of the form I=xA, U=xV or P=xW, where x is a real number. Directly before the unit (A,V or W) one of the prefixes m (milli), k (kilo) and M (Mega) may also occur. To summarize it: Data fields adhere to the following grammar:

DataField ::= Concept ‘=’ RealNumber [Prefix] Unit
Concept   ::= 'P' | 'U' | 'I'
Prefix    ::= 'm' | 'k' | 'M'
Unit      ::= 'W' | 'V' | 'A'

• The equal sign (‘=’) will never occur in an other context than within a data field.
• There is no whitespace (tabs,blanks) inside a data field.
• Either P and U, P and I, or U and I will be given.

For each test case, print three lines:
• a line saying “Problem #k” where k is the number of the test case
• a line giving the solution (voltage, power or current, dependent on what was given), written without a prefix and with two decimal places as shown in the sample output
• a blank line

3
If the voltage is U=200V and the current is I=4.5A, which power is generated?
A light-bulb yields P=100W and the voltage is U=220V. Compute the current, please.
bla bla bla lightning strike I=2A bla bla bla P=2.5MW bla bla voltage?


Problem #1
P=900.00W

Problem #2
I=0.45A

Problem #3
U=1250000.00V


import java.io.BufferedReader;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class Main {

/**
* Parse the value.
* @param s the given string such as  4.5A, 2.5MW
* @return
*/
private static double parseValue(String s) {
int i = 0;
while(i < s.length() && s.charAt(i) >= '0' && s.charAt(i) <= '9' || s.charAt(i) == '.') {
i++;
}

if (i == s.length()) {
return Double.parseDouble(s);
}

// in case that the end of string is not reached.

double value = Double.parseDouble(s.substring(0, i));

if (s.charAt(i) == 'm') {
value = value / 1000.0;
} else if (s.charAt(i) == 'M') {
value = value * 1000000.0;
} else if (s.charAt(i) == 'k') {
value = value * 1000.0;
}

return value;

}

/**
* @param args
*/
public static void main(String[] args) throws Exception  {

int n = Integer.parseInt(line);

for (int i = 1; i<= n; i++){

String[] t = line.split("[ ,]+");

boolean u_value = false;
boolean i_value = false;
boolean p_value = false;

double u = 0;
double I = 0;
double p = 0;

for (int j = 0; j < t.length; j++) {

if (t[j].indexOf("=") > 0) {

// split the kind of string like:U=200V
String[] temp = t[j].split("[=]");

if (temp[0].charAt(0) == 'U') {
u_value = true;

u = parseValue(temp[1]);
} else if (temp[0].charAt(0) == 'P') {
p_value = true;
p = parseValue(temp[1]);
} else if (temp[0].charAt(0) == 'I') {
i_value = true;
I = parseValue(temp[1]);
}

}

}

System.out.println("Problem #" + i);

if (u_value && i_value) {
System.out.print("P=");
System.out.print(new BigDecimal(I * u).setScale(2, RoundingMode.HALF_UP));
System.out.println("W");
} else if (p_value && u_value) {
System.out.print("I=");
System.out.print(new BigDecimal(p /u).setScale(2, RoundingMode.HALF_UP));
System.out.println("A");
} else if (p_value && i_value) {
System.out.print("U=");
System.out.print(new BigDecimal(p /I).setScale(2, RoundingMode.HALF_UP));
System.out.println("V");
}

System.out.println();
}
}
}

1. 有限自动机在ACM中是必须掌握的算法，实际上在面试当中几乎不可能让你单独的去实现这个算法，如果有题目要用到有限自动机来降低时间复杂度，那么这种面试题应该属于很难的级别了。

2. /*
* =====================================================================================
*
* Filename: 1366.cc
*
* Description:
*
* Version: 1.0
* Created: 2014年01月06日 14时52分14秒
* Revision: none
* Compiler: gcc
*
* Author: Wenxian Ni (Hello World~), [email protected]
* Organization: AMS/ICT
*
* =====================================================================================
*/

#include
#include

using namespace std;

int main()
{
stack st;
int n,i,j;
int test;
int a[100001];
int b[100001];
while(cin>>n)
{
for(i=1;i>a[i];
for(i=1;i>b[i];
//st.clear();
while(!st.empty())
st.pop();
i = 1;
j = 1;

while(in)
break;
}
while(!st.empty()&&st.top()==b[j])
{
st.pop();
j++;
}
}
if(st.empty())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}

3. 有两个重复的话结果是正确的，但解法不够严谨，后面重复的覆盖掉前面的，由于题目数据限制也比较严，所以能提交通过。已更新算法