UVA 537 人工智能

来源:互联网 发布:公司网络整改方案 编辑:程序博客网 时间:2024/06/05 01:53

要点:

从字符串或者字符数组中读取  小数

采用sscanf可以实现  sscanf(const char *,  format, &)

 

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <string>using namespace std;///#define INF 0xffffff7#define MAXN 200char problem[MAXN];char pp[MAXN];char uu[MAXN];char II[MAXN];double prefix[] = {0.001, 1000, 1000000};double P, U, I;void readin(char num[], int pos){int i, j;j = 0;for (i = pos; problem[i] != 'W' && problem[i] != 'A' && problem[i] != 'V'; i++){num[j++] = problem[i];}}double transfer(char num[]){int i, j;double mul = 1;double temp;int len = strlen(num);if (num[len - 1] == 'm'){mul = prefix[0];num[len - 1] = '\0';}else if (num[len - 1] == 'k'){mul = prefix[1];num[len - 1] = '\0';}else if (num[len - 1] == 'M'){mul = prefix[2];num[len - 1] = '\0';}sscanf(num, "%lf", &temp);temp *= mul;return temp;}int main(){///int i, j, th;int nCases;scanf("%d", &nCases);getchar();th = 1;while (nCases--){memset(pp, 0, sizeof(pp));memset(uu, 0, sizeof(uu));memset(II, 0, sizeof(II));gets(problem);int len = strlen(problem);P = U = I = 0;for (i = 0; i < len; i++){if (problem[i] == '='){switch(problem[i - 1]){case 'P':readin(pp, i + 1);break;case 'U':readin(uu, i + 1);break;case 'I':readin(II, i + 1);break;}}}printf("Problem #%d\n", th);if (strlen(pp) == 0){U = transfer(uu);I = transfer(II);P = U * I;printf("P=%.2lfW\n", P);}else if (strlen(uu) == 0){P = transfer(pp);I = transfer(II);U = P / I;printf("U=%.2lfV\n", U);}else if (strlen(II) == 0){P = transfer(pp);U = transfer(uu);I = P / U;printf("I=%.2lfA\n", I);}printf("\n");th++;}    ///    return 0;}


再贴一个

采用C++  isringstream 的方法,也比较好

#include<iostream>#include<sstream>#include<string>#include<cstdio>using namespace std;int main(){    /*    freopen("data.in","r",stdin);    freopen("data.out","w",stdout);    //*/    int T;    cin>>T;getchar();    for(int case_num=1;case_num<=T;case_num++)    {        string line;        getline(cin,line);        double P(-1.0),U(-1.0),I(-1.0);        istringstream sin(line);        char ch;        while(sin>>ch)        {            if(ch=='P')            {                char t;                sin>>t;                if(t!='=')                    continue;                sin>>P;                sin>>t;                if(t=='m')                    P/=1000;                else if(t=='k')                    P*=1000;                else if(t=='M')                    P*=1000000;            }            else if(ch=='U')            {                char t;                sin>>t;                if(t!='=')                    continue;                sin>>U;                sin>>t;                if(t=='m')                    U/=1000;                else if(t=='k')                    U*=1000;                else if(t=='M')                    U*=1000000;            }            else if(ch=='I')            {                char t;                sin>>t;                if(t!='=')                    continue;                sin>>I;                sin>>t;                if(t=='m')                    I/=1000;                else if(t=='k')                    I*=1000;                else if(t=='M')                    I*=1000000;            }        }        cout<<"Problem #"<<case_num<<endl;        if(P==-1.0)            printf("P=%.2fW\n",U*I);        else if(U==-1.0)            printf("U=%.2fV\n",P/I);        else            printf("I=%.2fA\n",P/U);        printf("\n");    }    return 0;}


 

原创粉丝点击