UVA 537-Artificial Intelligence?

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值