UVa 537 Artificial Intelligence?
2015-08-09 16:16
295 查看
Description
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.
Input
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 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'
Additional assertions:
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.
Output
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
Sample Input
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?
Sample Output
Problem #1 P=900.00W Problem #2 I=0.45A Problem #3 U=1250000.00V
#include <stdio.h> #include <string.h> #define N 10010 char s ; int main() { int n, len; double num, dec, p, u, i; scanf("%d\n", &n); for (int j = 1; j <= n; j++) { memset(s, 0, sizeof(s)); fgets(s, N, stdin); p = 0, u = 0, i = 0; len = strlen(s); for (int q = 0; q < len - 1; q++) { if (s[q] == '=') { num = 0; while (s[++q] <= '9' && s[q] >= '0') num = num * 10 + s[q] - '0'; if (s[q] == '.') { dec = 1; while (s[++q] <= '9' && s[q] >= '0') { dec *= 0.1; num = num + (s[q] - '0') * dec; } } if (s[q] == 'k') { num *= 1000; q++; } else if (s[q] == 'm') { num /= 1000; q++; } else if (s[q] == 'M') { num *= 1000000; q++; } if (s[q] == 'V') u = num; if (s[q] == 'A') i = num; if (s[q] == 'W') p = num; } } printf("Problem #%d\n", j); if (u == 0) printf("U=%.2fV\n", p / i); else if (i == 0) printf("I=%.2fA\n", p / u); else if (p == 0) printf("P=%.2fW\n", u * i); printf("\n"); } return 0; }
相关文章推荐
- 华为机试——两个超长正整数的加法 java
- HDU 1847 Good Luck in CET-4 Everybody!【博弈论】
- Uber是怎么入侵打车软件市场的
- 华为机试题 最小长方形 java 转载
- [Erlang_Question33]使用recon从网页查看Erlang运行状态
- 第23章 尝试互联网(3)
- UVa 10361 Automatic Poetry
- 剑指Offer面试题35(java版):第一个只出现一次的字符
- POJ 2513 Colored Sticks(欧拉回路+字典树+并查集)
- 机房收费系统之优化一
- 黑马程序员——反射2:应用
- Scala入门到精通——第二十一节 类型参数(三)-协变与逆变
- POJ 3630- Phone List(Trie)
- java多线程零碎知识点
- java中使用字符串或者动态创建对象的三种方法(Class,Constructor,Proxy)
- 读书笔记-C++PrimerPlus-8.2.6 对象、继承和引用10.2.5 修改实现
- jQuery 参考手册 - 选择器
- java jdbc
- android获取webservice接口的数据
- Hadoop之Map-Reduce