zoukankan      html  css  js  c++  java
  • UVA537

      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=xA, U=xV or P=xW, where x is a real number.

    Directly before the unit (A, V 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 U, P 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
    


    这题觉得自己做的没问题,可还是WA了好几次,然后就搁下了几天……又读了一遍题,发现是这样的……
    Concept   ::= 'P' | 'U' | 'I' 概念::='P'|'U'|'I'
    Prefix    ::= 'm' | 'k' | 'M' 前缀::='m'|'K'|'M'
    Unit      ::= 'W' | 'V' | 'A' 单位::='W'|'V'|'A'

    P、U、I都要有这三种前缀,m、K、M分别对应的是*0.001,*1000,*1000000。看来不读题直接看输入输出就是不行,明白个大意,细节方面很容易导致WA。

     1 #include<stdio.h>
     2 #include<string.h>
     3 
     4 int main()
     5 {
     6     int n, i;
     7     double P, U, I;
     8     char ch;
     9     scanf("%d", &n);
    10     getchar();
    11     for (i = 1; i <= n; i++)
    12     {
    13         P = 0, I = 0, U = 0;
    14         while(1)
    15         {
    16             ch = getchar();                  //做了安迪的第一个字典后立刻来现学现卖,每次只输入一个字符,循环输入
    17             if (ch == 'P' || ch == 'p')
    18             {
    19                 ch = getchar();             //如果是P,则看下一个字符是不是'='
    20                 if (ch == '=')
    21                 {
    22                     scanf("%lf", &P);      //如果是,则输入double型接收数字
    23                     ch = getchar();
    24                     if (ch == 'm')              //这就是所说的前缀了。。。就WA这了
    25                         P = P * 0.001;
    26                     else if (ch == 'k')
    27                         P = P * 1000;
    28                     else if (ch == 'M')
    29                         P = P * 1000000;
    30                 }
    31                 else
    32                     continue;
    33             }
    34             else if (ch == 'U' || ch == 'u')
    35             {
    36                 ch = getchar();
    37                 if (ch == '=')
    38                 {
    39                     scanf("%lf", &U);
    40                     ch = getchar();
    41                     if (ch == 'm')
    42                         U = U * 0.001;
    43                     else if (ch == 'k')
    44                         U = U * 1000;
    45                     else if (ch == 'M')
    46                         U = U * 1000000;
    47                 }
    48                 else
    49                     continue;
    50             }
    51             else if (ch == 'I' || ch == 'i')
    52             {
    53                 ch = getchar();
    54                 if (ch == '=')
    55                 {
    56                     scanf("%lf", &I);
    57                     ch = getchar();
    58                     if (ch == 'm')
    59                         I = I * 0.001;
    60                     else if (ch == 'k')
    61                         I = I * 1000;
    62                     else if (ch == 'M')
    63                         I = I * 1000000;
    64                 }
    65                 else
    66                     continue;
    67             }
    68             else if (ch == '
    ')              //跳出循环,一开始还以为用
    
    69                 break;
    70         }
    71         printf("Problem #%d
    ", i);
    72         if (P && I)
    73         {
    74             printf("U=%.2lfV", P / I);
    75         }
    76         else if (P && U)
    77         {
    78             printf("I=%.2lfA", P / U);
    79         }
    80         else if (U && I)
    81         {
    82             printf("P=%.2lfW", I * U);
    83         }
    84         printf("
    
    ");
    85     }
    86     return 0;
    87 }
  • 相关阅读:
    Jumpserver之安装在CentOS主机步骤
    Nginx负载均衡后端健康检查(支持HTTP和TCP)
    Nginx负载均衡后端健康检查
    ELK之使用packetbeat分析网络包流量
    ELK之使用heartbeat监控WEB站点
    ELK之elasticsearch导致CPU居高不下系统慢解决办法
    Saltstack如何修改主机名或者minion id
    mac中使用 sourcetree 的快速配置和git服务器登录
    [转]从三层架构迈向领域驱动设计 — 关于领域设计中描述相对简要及明了的一篇文章
    io-nio 区别示意图
  • 原文地址:https://www.cnblogs.com/youdiankun/p/3309816.html
Copyright © 2011-2022 走看看