UVA-537 Artificial Intelligence?

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值