UVAOJ 537 ——Artificial Intelligence? 模拟
2014-12-03 10:36
330 查看
Artificial Intelligence? |
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
Miguel A. Revilla
1999-01-11
判P=或U=或I=这样连续字符,后面的用字符串存储一直到单位,然后用 stdlib.h中的atof()函数把字符窜转换成double就能计算了,注意单位换算。。。。。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char s[10010];
struct node
{
char num[1010];
char c;
char dw;
double zhn;
}ls[2];
void zh(int j,char ch,int x)
{
char zch;
int xb = 0;
if(ch == 'P')
zch = 'W';
else if(ch == 'U')
zch = 'V';
else if(ch == 'I')
zch = 'A';
ls[x].c = ch;
ls[x].dw = '\0';
for(;s[j] != zch && s[j] != '\0';j++)
{
ls[x].num[xb++] = s[j];
}
if(ls[x].num[xb - 1] == 'm' || ls[x].num[xb - 1] == 'k' || ls[x].num[xb - 1] == 'M')
{
xb--;
ls[x].dw = ls[x].num[xb];
ls[x].num[xb] = '\0';
}
else
{
ls[x].num[xb] = '\0';
}
}
void tod(int xb)
{
ls[xb].zhn = atof(ls[xb].num);
if(ls[xb].dw == 'm')
ls[xb].zhn /= 1000;
if(ls[xb].dw == 'M')
ls[xb].zhn *= 1000000;
if(ls[xb].dw == 'k')
ls[xb].zhn *= 1000;
}
int main()
{
int n,y = 0;
scanf("%d%*c",&n);
while(n--)
{
gets(s);
int flag = 0;
for(int i = 0;s[i] != '\0';i++)
{
if(flag == 2)
break;
if(s[i] == 'P' && s[i + 1] == '=')
{
zh(i + 2,'P',flag);
flag++;
}
else if(s[i] == 'U' && s[i + 1] == '=')
{
zh(i + 2,'U',flag);
flag++;
}
else if(s[i] == 'I' && s[i + 1] == '=')
{
zh(i + 2,'I',flag);
flag++;
}
}
tod(0);
tod(1);
printf("Problem #%d\n",++y);
if(ls[0].c == 'P' && ls[1].c == 'U')
{
printf("I=%.2lfA\n\n",ls[0].zhn / ls[1].zhn);
}
else if(ls[0].c == 'U' && ls[1].c == 'P')
{
printf("I=%.2lfA\n\n",ls[1].zhn / ls[0].zhn);
}
else if(ls[0].c == 'P' && ls[1].c == 'I')
{
printf("U=%.2lfV\n\n",ls[0].zhn / ls[1].zhn);
}
else if(ls[0].c == 'I' && ls[1].c == 'P')
{
printf("U=%.2lfV\n\n",ls[1].zhn / ls[0].zhn);
}
else if((ls[0].c == 'U' && ls[1].c == 'I') || (ls[0].c == 'I' && ls[1].c == 'U'))
{
printf("P=%.2lfW\n\n",ls[1].zhn * ls[0].zhn);
}
}
return 0;
}
Artificial Intelligence? |
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
Miguel A. Revilla
1999-01-11
相关文章推荐
- UVA 537-Artificial Intelligence?(模拟)
- UVaOJ 537 - Artificial Intelligence?
- uvaoj-133:约瑟夫环的模拟
- UvaOJ 537 - Artificial Intelligence?
- UVaOJ 537
- UvaOJ 537 - Artificial Intelligence?
- UVAOJ 10361 ——Automatic Poetry 模拟
- UVAOJ 409 ——Excuses, Excuses! 模拟
- UVAOJ 537字符串处理
- UVAOJ 10878 ——Decode the tape 模拟
- uva 537 Artificial Intelligence?(字符串模拟)
- UvaOJ 537 - Artificial Intelligence?
- UVAOJ 10815 —— Andy's First Dictionary 模拟 排序
- uvaoj 540 Team Queue 队列模拟
- UVAoj 146 ID Code(模拟/排列)
- UVaOJ 537 - Artificial Intelligence?
- 文字中间加横线和对文字打叉(模拟文字错误显示)
- Java模拟弹球效果(监听键盘)
- IOS UI Automation 学习之常用类,方法和模拟手势
- 腾讯微博采集器 模拟登陆 2012-11-26版