uva 537 - Artificial Intelligence?
November 9, 2012
Uva
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 file 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 formI=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: Additional assertions: For each test case, print three lines: 其实你看到下面的代码后不要害怕,有30多行是重复的…… 想法是 先找到等号,判断一下等号前面的字符,接着输入等号后面的浮点数,在得到数字后面的单位的前缀,就是 m k M 。 然后再找下一个等号,进行上述操作,对于后面的字符,用 gets() 放进一个大的数组里面就可以了,没什么作用。 WA了一次,原因是在最后一个case后面没有输出空行,题目要求是在每一个case后面输出空行,并不是在case之间输出空行,哎,又是错在了这里,以后要注意。
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
char s[1000];
int main(void)
{
int t, i;
double I, U, P;
cin >> t;
for (i = 1; i < t + 1; i++)
{
char c, ch, cc;
I = U = P = 0;
cout<<"Problem #"<<i<<endl;
getchar();
while ((c = getchar()))
{
if (c == '=')
{
if (ch=='I')
{
cin >> I;
cc = getchar();
if (cc=='m') I*=0.001;
else if (cc=='k') I*=1000;
else if (cc=='M') I*=1000000;
else ;
}
else if (ch=='U')
{
cin >> U;
cc = getchar();
if (cc=='m') U*=0.001;
else if (cc=='k') U*=1000;
else if (cc=='M') U*=1000000;
else ;
}
else if (ch=='P')
{
cin >> P;
cc = getchar();
if (cc=='m') P*=0.001;
else if (cc=='k') P*=1000;
else if (cc=='M') P*=1000000;
else ;
}
break;
}
ch = c;
}
while ((c = getchar()))
{
if (c == '=')
{
if (ch=='I')
{
cin >> I;
cc = getchar();
if (cc=='m') I*=0.001;
else if (cc=='k') I*=1000;
else if (cc=='M') I*=1000000;
else ;
}
else if (ch=='U')
{
cin >> U;
cc = getchar();
if (cc=='m') U*=0.001;
else if (cc=='k') U*=1000;
else if (cc=='M') U*=1000000;
else ;
}
else if (ch=='P')
{
cin >> P;
cc = getchar();
if (cc=='m') P*=0.001;
else if (cc=='k') P*=1000;
else if (cc=='M') P*=1000000;
else ;
}
break;
}
ch = c;
}
gets(s);
cout << setprecision(2)<< fixed;
if (P!=0&&U!=0) cout<<"I="<<P/U<<'A'<<endl;
else if (P!=0&&I!=0) cout<<"U="<<P/I<<'V'<<endl;
else if (I!=0&&U!=0) cout<<"P="<<I*U<<'W'<<endl;
cout << endl;
}
return 0;
}