原题地址:http://poj.org/problem?id=2256
Artificial Intelligence?
Time Limit:1000MS |
|
Memory Limit:65536K |
|
|
|
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 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
由于开始以为是m是million的意思,WA了好多次,郁闷........
AC代码如下
/**************************************************************************
* Problem: POJ 2256 Artificial Intelligence?
* Copyright 2011 by Yan
* DATE:
* E-Mail: yming0221@gmail.com
************************************************************************/
#include <stdio.h>
char cache[2000];
char op1[50];
char op2[50];
double dop1,dop2;
int len;
void get_op(int p1,int p2)
{
int index=p1;
int op_index=0;
int i;
char tmp;/*存放单位字符*/
while(cache[++index]!='A'&& cache[index]!='W'&& cache[index]!='V')
{
op1[op_index++]=cache[index];
}
op1[op_index]='\0';
tmp=op1[op_index-1];
if(tmp=='m'||tmp=='k'||tmp=='M') op1[op_index-1]='\0';/*从字符串中取出单位*/
sscanf(op1,"%lf",&dop1);
switch (tmp)
{
case 'k':dop1=dop1*1000.0;break;
case 'm':dop1=dop1*0.001;break;
case 'M':dop1=dop1*1000000.0;break;
}
index=p2;
op_index=0;
while(cache[++index]!='A'&& cache[index]!='W'&& cache[index]!='V')
{
op2[op_index++]=cache[index];
}
op2[op_index]='\0';
tmp=op2[op_index-1];
if(tmp=='m'||tmp=='k'||tmp=='M') op1[op_index-1]='\0';/*从字符串中取出单位*/
sscanf(op2,"%lf",&dop2);
switch (tmp)
{
case 'k':dop2=dop2*1000.0;break;
case 'm':dop2=dop2*0.001;break;
case 'M':dop2=dop2*1000000.0;break;
}
}
int main()
{
/*freopen("input","r",stdin);*/
int n,i,j;
int index[2];/*存放两个等号的位置*/
int cnt;
double ans;
scanf("%d ",&n);
for(i=1;i<=n;i++)
{
gets(cache);
/*puts(cache);*/
len=strlen(cache);
cnt=0;
for(j=0;j<len;j++)
{
if(cache[j]=='=') index[cnt++]=j;
if(cnt==2) break;
}/*扫描到两个等号*/
get_op(index[0],index[1]);/*获取操作数和操作*/
printf("Problem #%d\n",i);
if( (cache[index[0]-1]=='U'&&cache[index[1]-1]=='I') || (cache[index[0]-1]=='I'&&cache[index[1]-1]=='U'))
{
printf("P=%.2fW\n\n",dop1*dop2);
}
else if( (cache[index[0]-1]=='P'&&cache[index[1]-1]=='I') || (cache[index[0]-1]=='I'&&cache[index[1]-1]=='P'))
{
if(cache[index[0]-1]=='P')
printf("U=%.2fV\n\n",dop1/dop2);
else printf("U=%.2fV\n\n",dop2/dop1);
}
else if( (cache[index[0]-1]=='U'&&cache[index[1]-1]=='P') || (cache[index[0]-1]=='P'&&cache[index[1]-1]=='U'))
{
if(cache[index[0]-1]=='P')
printf("I=%.2fA\n\n",dop1/dop2);
else printf("I=%.2fA\n\n",dop2/dop1);
}
}
return 0;
}
分享到:
相关推荐
北大POJ3414-Pots 解题报告+AC代码
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题报告poj 解题...
北大POJ1004-Financial Management 解题报告+AC代码
POJ第1861题源码 POJ第1861题源码 POJ第1861题源码
poj3045的源码,很久以前写的,语言是C++
北大POJ1159-Palindrome 解题报告+AC代码
poj分类poj分类poj分类poj分类
C语言 poj npu 西工大 C语言Poj答案全完整打包,给有需要的朋友
poj 3414解题报告poj 3414解题报告poj 3414解题报告poj 3414解题报告
poj 1012解题报告poj 1012解题报告poj 1012解题报告poj 1012解题报告
poj 2329解题报告poj 2329解题报告poj 2329解题报告poj 2329解题报告
poj 1659解题报告poj 1659解题报告poj 1659解题报告poj 1659解题报告
POJ1503解答 POJ1503解答,正确答案(已通过POJ)
北大POJ2002-Squares 解题报告+AC代码
POJ1048,加强版的约瑟夫问题 难度中等
POJ1083的代码,POJ1083的代码,POJ1083的代码
poj 百练 题目分类 poj 百练 题目分类
poj 1001答案
POJ2968代码有用,欢迎下载,POJ代码