UVA-537 Artificial Intelligence?
题目大意:从一句话中抽取出已知条件,应用公式 P = U * I,求出未知的元素。
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
解题思路:对字符逐个判断,遇到 p,u,i 时,判断下一个是否是 = ,如果是,以 lf 读取已知条件的数值,运算后输出结果。
//UVA-537 Artificial Intelligence?
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char c;
double u,i,p;
int count = 1;
char str[100000];
int main() {
int T;
scanf("%d",&T);
getchar();
while (T--) {
u = 0, i = 0, p = 0;
int t = 2;
while (t--) {
while ((c = getchar()) != '\n' ) {
if (c == 'I') {
if((c = getchar()) == '=') {
scanf("%lf",&i);
c = getchar();
if(c == 'm')
i = i * 0.001;
if(c == 'k')
i = i * 1000;
if(c == 'M')
i = i * 1000000;
break;
}
}
if(c == 'P') {
if((c = getchar()) != '\n') {
scanf("%lf",&p);
c = getchar();
if(c == 'm')
p = p * 0.001;
if(c == 'k')
p = p * 1000;
if(c == 'M')
p = p * 1000000;
break;
}
}
if(c == 'U') {
if((c = getchar()) != '\n') {
scanf("%lf",&u);
c = getchar();
if(c == 'm')
u = u * 0.001;
if(c == 'k')
u = u * 1000;
if(c == 'M')
u = u * 1000000;
break;
}
}
}
}
gets(str);
printf("Problem #%d\n",count++);
if (u == 0)
printf("U=%.2lfV",p/i);
if (i == 0)
printf("I=%.2lfA",p/u);
if (p == 0)
printf("P=%.2lfW",u*i);
printf("\n\n");
}
return 0;
}