uva 537 - Artificial Intelligence?

这题看题目时要细心,讲一下做题步骤

1.得到 P,I,U的double值,可以用string类型中搜索法

2.确定精度

3.得到目标值

因为在输出时忘记打印P=,U=,I=,还wa了一次

#include<iostream>
#include<string>
#include<stdlib.h>
#include<iomanip>
#include<stdio.h>
using namespace std;
int main(){
    int n,num=1;
    string annotation[]={"P=","I=","U="};
    string character[]={"W","A","V"};
    cin>>n; 
    getchar();
    string l;
    while(n--){
        string sp,si,su;
        double dp,di,du;
        int start,end,tempP=0,tempI=0,tempU=0;
        double precs=1.0;
        getline(cin,l);
        if((start=l.find(annotation[0],0))!=-1){
            end=l.find(character[0],start);
            precs=1.0;
            if(l[end-1]=='m')
                precs=0.001;
            if(l[end-1]=='k')
                precs=1000.0;
            if(l[end-1]=='M')
                precs=1000000.0;
            sp.assign(l,start+2,end-2-start);
            dp=atof(const_cast<const char*>(sp.c_str()));
            dp*=precs;
            tempP=0;
        }
        if((start=l.find(annotation[1],0))!=-1){
            end=l.find(character[1],start);
            precs=1.0;
            if(l[end-1]=='m')
                precs=0.001;
            if(l[end-1]=='k')
                precs=1000.0;
            if(l[end-1]=='M')
                precs=1000000.0;
            si.assign(l,start+2,end-2-start);
            di=atof(const_cast<const char*>(si.c_str()));
            di*=precs;
            tempI=1;
        }
        if((start=l.find(annotation[2],0))!=-1){
            end=l.find(character[2],start);
            precs=1.0;
            if(l[end-1]=='m')
                precs=0.001;
            if(l[end-1]=='k')
                precs=1000.0;
            if(l[end-1]=='M')
                precs=1000000.0;
            su.assign(l,start+2,end-2-start);
            du=atof(const_cast<const char*>(su.c_str()));
            du*=precs;
            tempU=2;
        }

        switch((tempI+tempP+tempU)){
        case 1:
            cout<<"Problem #"<<num++<<endl;
            cout<<annotation[2];
            printf("%.2lfV\n\n",dp/di);
            break;
        case 2:
            cout<<"Problem #"<<num++<<endl;
            cout<<annotation[1];
            printf("%.2lfA\n\n",dp/du);
            //cout<<setprecision(2)<<(dp/du)<<endl<<endl;
            break;
        case 3:
            cout<<"Problem #"<<num++<<endl;
            cout<<annotation[0];
            printf("%.2lfW\n\n",di*du);
            //cout<<setprecision(2)<<(di*du)<<endl<<endl;
        }
    }
    //system("pause");
}

 

posted on 2013-12-09 11:00  云在心  阅读(203)  评论(0)    收藏  举报

导航