• UVa 537 Artificial Intelligence?


      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

     每行找到两个等号的位置,在等号前后提取信息,算出答案即可。

    注意每组测试数据要输出一个空行

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 
     5 using namespace std;
     6 
     7 double U,I,P;
     8 char s[10000];
     9 
    10 void trans(int x)
    11 {
    12     double temp;
    13     sscanf(&s[x+1],"%lf",&temp);
    14     int pos;
    15     for(pos=x+1;s[pos]!='A'&&s[pos]!='W'&&s[pos]!='V';pos++);
    16     switch(s[pos-1])
    17     {
    18         case 'm':temp/=1000;break;
    19         case 'k':temp*=1000;break;
    20         case 'M':temp*=1000000;break;
    21     }
    22     switch(s[x-1])
    23     {
    24         case 'P':P=temp;break;
    25         case 'I':I=temp;break;
    26         case 'U':U=temp;break;
    27     }
    28 }
    29 
    30 int main()
    31 {
    32     int kase;
    33 
    34     scanf("%d",&kase);
    35     getchar();
    36 
    37     for(int k=1;k<=kase;k++)
    38     {
    39         gets(s);
    40 
    41         U=I=P=-1;
    42         for(int i=0;s[i]!='';i++)
    43         {
    44             if(s[i]=='=')
    45                 trans(i);
    46         }
    47 
    48         printf("Problem #%d
    ",k);
    49         if(P==-1)
    50             printf("P=%.2lfW
    
    ",U*I);
    51         else if(I==-1)
    52             printf("I=%.2lfA
    
    ",P/U);
    53         else
    54             printf("U=%.2lfV
    
    ",P/I);
    55     }
    56 
    57     return 0;
    58 }
    [C++]
  • 相关阅读:
    把文本数据转化为json
    componentsSeparatedByString 的注意事项
    内存管理
    审核问题2.3.1
    H5缩放效果的问题和缓存问题
    iOS库
    DDOS 攻击防范
    连接数过多的问题
    nginx 长连接keeplive
    javascript 判断身份证的正确性
  • 原文地址:https://www.cnblogs.com/lzj-0218/p/3526284.html
Copyright © 2020-2023  润新知