537-artificial Intelligence?
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem &problem=478
Physics teachers in high school often the "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 as ' you have a electrical circuit that contains a BA Ttery with a voltage of u=10v and a light-bulb. There ' s a electrical current of i=5a through the bulb. Which power are 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: ' Which formulae do I know? Ah Yes, p=u*i. therefore p=10v*5a=500w. Finished. "
OK, this is doesn ' t always work, so we pupils are usually not the top scorers in physics tests. But at least this simple algorithm are usually good enough to pass the class. (Sad but true.)
Today we'll check if a computer can pass a high school physics test. We'll concentrate on the p-u-i type problems. That means, problems in which two of power, voltage and current are given and the third are wanted.
Your job is to write a, reads such a text problem and solves it according to the simple algorithm given.
Input
The ' I ' input file would contain the number of test cases.
Each test case would consist of one line containing exactly two data fields and some additional arbitrary. A data field would be of the form I=xa, U=XV or P=XW, where X was 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
Additional assertions:
The equal sign (' = ') would never occur in the 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 would be given.
Output
For each test case, print three lines:
A line saying ' Problem #k ' where K's the ' test case
A line giving the solution (voltage, power or current, dependent on what is given), written without a prefix and with two Decimal places as shown in the sample output
A blank line
Sample Input
3
If the voltage is u=200v and the are i=4.5a, which power is generated?
A light-bulb yields p=100w and the voltage is u=220v. Compute, please.
Bla bla bla lightning strike i=2a bla bla bla p=2.5mw bla bla voltage?
Sample Output
Problem #1
p=900.00w
Problem #2
i=0.45a
Problem #3
u=1250000.00v
From left to right, from right to left, find ' = '.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/
Complete code:
/*0.019s*/#include <cstdio> #include <cctype> #include <cstdlib> #include <cstring>
Char str[1000];
Double num (char *str, int i, char unit) {int J;
Double prefixes = 1.0;
for (j = i; str[j]!= unit; j + +);
if (Isalpha (Str[j-1])) {if (str[j-1] = = ' m ') prefixes = 0.001;
else if (str[j-1] = = ' k ') prefixes = 1000.0;
else prefixes = 1000000.0;
return atof (str + i + 1) * prefixes;///OK number is not followed by E or e~} int main (void) {Double U, I, P;
int n, I, count = 0;
scanf ("%d\n", &n);
while (n--) {U = I = P =-1.0;
Gets (str);
int L = strlen (str);
for (i = 0; str[i]!= ' = '; i++);
if (str[i-1] = = ' U ') U = num (str, I, ' V ');
else if (str[i-1] = = ' P ') P = num (str, I, ' W '); else i = num (str, I, ' A ');
for (i = l-1; Str[i]!= ' = '; i--);
if (str[i-1] = = ' U ') U = num (str, I, ' V ');
else if (str[i-1] = = ' P ') P = num (str, I, ' W ');
else i = num (str, I, ' A ');
printf ("Problem #%d\n", ++count);
if (U = = -1.0) printf ("u=%.2fv\n\n", p/i);
else if (P = = -1.0) printf ("p=%.2fw\n\n", U * I);
else printf ("i=%.2fa\n\n", p/u);
return 0; }