• UVa 537 Artificial Intelligence?


    题目大意:输入一个字符串,根据物理公式P=U*I,已知其中两个量,求第三个量,结果保留两位小数。

      Artificial Intelligence? 

    Physics teachers in high school often think that problems given as text are more demanding than pure computations. After all, the pupils have to read and understand the problem first!

    So they don't state a problem like ``U=10V, I=5A, P=?" but rather like ``You have an electrical circuit that contains a battery with a voltage of U=10V and a light-bulb. There's an electrical current of I=5A through the bulb. Which power is generated in the bulb?".

    However, half of the pupils just don't pay attention to the text anyway. They just extract from the text what is given: U=10V, I=5A. Then they think: ``Which formulae do I know? Ah yes, P=U*I. Therefore P=10V*5A=500W. Finished."

    OK, this doesn't always work, so these pupils are usually not the top scorers in physics tests. But at least this simple algorithm is usually good enough to pass the class. (Sad but true.)

    Today we will check if a computer can pass a high school physics test. We will concentrate on the P-U-I type problems first. That means, problems in which two of power, voltage and current are given and the third is wanted.


    Your job is to write a program that reads such a text problem and solves it according to the simple algorithm given above.

    Input 

    The first line of the input file will contain the number of test cases.

    Each test case will consist of one line containing exactly two data fields and some additional arbitrary words. A data field will be of the form I=xAU=xV or P=xW, where x is a real number.

    Directly before the unit (AV or W) one of the prefixes m (milli), k (kilo) and M (Mega) may also occur. To summarize it: Data fields adhere to the following grammar:

    DataField ::= Concept '=' RealNumber [Prefix] Unit
    Concept   ::= 'P' | 'U' | 'I'
    Prefix    ::= 'm' | 'k' | 'M'
    Unit      ::= 'W' | 'V' | 'A'
    

    Additional assertions:

    • The equal sign (`=') will never occur in an other context than within a data field.
    • There is no whitespace (tabs,blanks) inside a data field.
    • Either P and UP and I, or U and I will be given.

    Output 

    For each test case, print three lines:

    • a line saying ``Problem #k" where k is the number of the test case
    • a line giving the solution (voltage, power or current, dependent on what was given), written without a prefix and with two decimal places as shown in the sample output
    • a blank line

    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
    

    Miguel A. Revilla 
    1999-01-11
     
    AC代码:
     1 //#define LOCAL
     2 #include <iostream>
     3 #include <cstdio>
     4 #include <cstring>
     5 using namespace std;
     6 
     7 char s[300];
     8 
     9 struct AI
    10 {
    11     char Concept, Prefix, Unit;
    12     double x;
    13 }a[2];
    14 
    15 void transform(struct AI &t);
    16 double calculate(char u, struct AI a1, struct AI a2);
    17 char f(char Concept);
    18 
    19 int main(void)
    20 {
    21     #ifdef LOCAL
    22         freopen("537in.txt", "r", stdin);
    23     #endif
    24     int N, kase = 0;
    25     cin >> N;
    26     getchar();
    27     while(N--)
    28     {
    29         int i, len;
    30         ++kase; 
    31         gets(s);
    32         len = strlen(s);
    33         for(i = 0; i < len; ++i)//寻找第一个等号
    34         {
    35             if(s[i] == '=')
    36                 break;
    37         }
    38         sscanf(&s[i] - 1,"%c=%lf%c", &a[0].Concept, &a[0].x, &a[0].Prefix);
    39         for(++i; i < len; ++i)//寻找第二个等号
    40         {
    41             if(s[i] == '=')
    42                 break;
    43         }
    44         sscanf(&s[i] - 1,"%c=%lf%c", &a[1].Concept, &a[1].x, &a[1].Prefix);
    45         transform(a[0]);
    46         transform(a[1]);
    47 
    48         char u = 'P' + 'U' + 'I' - a[0].Concept - a[1].Concept;//这是要求的物理量
    49         double res;
    50         res = calculate(u, a[0], a[1]);
    51 
    52         cout << "Problem #" << kase << endl;
    53         printf("%c=%.2lf%c
    
    ", u, res, f(u));
    54     }
    55     return 0;
    56 }
    57 //根据物理量求单位
    58 char f(char Concept)
    59 {
    60     if(Concept == 'P')
    61         return 'W';
    62     if(Concept == 'U')
    63         return 'V';
    64     if(Concept == 'I')
    65         return 'A';
    66 }
    67 //单位换算及转化为国际标准单位 
    68 void transform(struct AI &t)
    69 {
    70     if(t.Prefix == 'm')
    71         t.x /= 1000.00;
    72     if(t.Prefix == 'k')
    73         t.x *= 1000.00;
    74     if(t.Prefix == 'M')
    75         t.x *= 1000000.00;
    76     t.Unit = f(t.Concept);
    77 }
    78 //根据公式计算
    79 double calculate(char u, struct AI a1, struct AI a2)
    80 {
    81     if(u == 'P')
    82         return a1.x * a2.x;
    83     if(a1.Concept == 'P')
    84         return a1.x / a2.x;
    85     return a2.x / a1.x;
    86 }
    代码君
  • 相关阅读:
    知识搜索
    使用 getopt() 进行命令行处理
    【新提醒】夏新大v安卓4.1尝鲜最新更新版本发布(包含进步版)1124更新 大V综合交流区 360论坛
    搜狗知立方高调亮相 开启知识计算新时代
    socat: Linux / UNIX TCP Port Forwarder
    Crontab 每两周执行一次
    python 命令行解析 optionparser
    crontab jojo's blog--快乐忧伤都与你同在 BlogJava
    搜索引擎开始「实体搜索」新领域竞争,Google、百度分别发力实体搜索产品
    netcat(nc)的替代产品 Socat
  • 原文地址:https://www.cnblogs.com/AOQNRMGYXLMV/p/3817640.html
Copyright © 2020-2023  润新知