UVa 537 Artificial Intelligence?
来源:互联网 发布:手机蓝牙网络共享电脑 编辑:程序博客网 时间:2024/06/05 16:43
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 theP-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 formI=xA, U=xV or P=xW, wherex is a real number.
Directly before the unit (A, V or W) one of the prefixesm (milli), k (kilo) and M (Mega) may also occur. To summarize it: Data fields adhere to the following grammar:
DataField ::= Concept '=' RealNumber [Prefix] UnitConcept ::= '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 andI 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
3If 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 #1P=900.00WProblem #2I=0.45AProblem #3U=1250000.00V
#include <stdio.h>#include <string.h>#define N 10010char s[N];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;}
- uva 537 Artificial Intelligence?
- UVa 537 - Artificial Intelligence?
- UVa 537 - Artificial Intelligence?
- UVa 537 - Artificial Intelligence?
- UVA-537 - Artificial Intelligence?
- uva 537 - Artificial Intelligence?
- uva 537 Artificial Intelligence?
- UVA 537 - Artificial Intelligence?
- UVa 537 - Artificial Intelligence?
- UVA 537Artificial Intelligence?
- UVA 537 Artificial Intelligence?
- UVa 537: Artificial Intelligence?
- UVA 537 Artificial Intelligence?
- UVA 537 Artificial Intelligence?
- uva 537 Artificial Intelligence?
- uva - 537 - Artificial Intelligence?
- UVa 537 - Artificial Intelligence?
- UVa 537 - Artificial Intelligence?
- 莱特币(litecoin)矿池(p2pool)架设之WINDOWS篇
- 好的是大哥大哥
- 华为OJ(输出单链表倒数第K个节点)
- CXF学习(二)--- WebService
- python 爬虫中文乱码问题
- UVa 537 Artificial Intelligence?
- JavaScript面向对象精要
- Oracle归档模式与非归档模式设置
- 黑马程序员——其他8:eclipse使用技巧总结
- 华为机试——两个超长正整数的加法 java
- 操作系统面试题整理
- hdu 1078 记忆化搜索
- 【屌丝程序的口才逆袭演讲稿50篇】第十三篇:爱迪生欺骗了我们!【张振华.Jack】
- C++中虚函数内存布局