UVA 537-Artificial Intelligence?
题目大意:输入一个问题,会给出P/U/I之中俩个的值,输出算出问题没给出的值利用公式P=I*U。
解题思路:读字符串找出等号,然后获取值计算
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
int main() {
int n;
cin >> n;
getchar();
int all = 0;
while(n--) {
all++;
char a[100000];
for(int i = 0; (a[i] = getchar()) != '\n'; i++)
;
int count = 0;
double t[3];
int p;
int k = 1, j = 1, l = 1;
for(int i = 0; a[i] != '\n'; i++) {
if(count == 2)
break;
if(a[i] == '=') {
count++;
if(a[i-1] == 'I') {
p = 0;
k = 0;
}
else if(a[i-1] == 'U') {
p = 1;
j = 0;
}
else if(a[i-1] == 'P') {
p = 2;
l = 0;
}
i++;
double x = 10;
t[p] = 0;
for(; a[i] != 'A' && a[i] != 'V' && a[i] != 'W'; i++) {
if(i == 0)
t[p] = a[i] - '0';
else if(a[i] == '.')
x = 0.1;
else if(a[i] == 'm')
t[p] = 0.001 * t[p];
else if(a[i] == 'k')
t[p] = 1000 * t[p];
else if(a[i] == 'M')
t[p] = 1000000 * t[p];
else if(x == 10)
t[p] = t[p] * x + a[i] - '0';
else {
t[p] = t[p] + (a[i] - '0') * x;
x = x * 0.1;
}
}
}
}
if(k == 1)
printf("Problem #%d\nI=%.2lfA\n\n",all,(t[2] / (t[1] * 1.0)));
else if(j == 1)
printf("Problem #%d\nU=%.2lfV\n\n",all,(t[2] / (t[0] * 1.0)));
else
printf("Problem #%d\nP=%.2lfW\n\n",all,(t[0] * (t[1] * 1.0)));
}
return 0;
}