UVA 537-artificial Intelligence? (analog)

Source: Internet
Author: User

Artificial Intelligence?Time limit:MS Memory Limit:0KB 64bit IO Format:%lld & %llu SubmitStatus

Description


Artificial Intelligence?

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

So they don't state a problem like 'u=10v, i=5a, p=? "Butthe rather like ' you had aelectrical 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 of 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 is usually not the top scorers in physics tests. But at least this simple algorithm was usually good enough to pass the class. (Sad but true.)

Today We'll check if a computer can pass a high school physics test. We'll concentrate on the p-u-i type problems first. That's means, problems in which of power, voltage and current are given and the third is wanted.


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

InputThe first line of the input file would contain the number of test cases.

Each test case would consist of one line containing exactly the data fields and some additional arbitrary words. A data field would be is 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, adhere to the following grammar:

DataField:: = Concept ' = ' realnumber [Prefix] unitconcept   :: = ' P ' | ' U ' | ' I ' Prefix    :: = ' m ' | ' K ' | ' M ' Unit      :: = ' W ' | ' V ' | A

Additional assertions:

    • The equal sign ('=') would 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 'll be given.

Outputfor the test case, print three lines:

    • A line saying 'problem #k' where K -is the number of the the test case
    • A line giving the solution (voltage, power or current, dependent on what is given), written without a prefix and with Decimal places as shown in the sample output
    • A blank line

Sample Input

3If the voltage is u=200v and the current was 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 #1P =900.00wproblem #2I =0.45aproblem #3U =1250000.00v


Test instructions: According to the physical formula P=u*i, give any of the two to ask for another.

Idea: Use of the stupid way to enumerate all the situation, pay attention to the conversion of units.

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <algorithm>using namespace    Std;char Str[1010];int Main () {int t,i,j;    int k=1;    Double P,i,u,flag;        while (~SCANF ("%d", &t)) {GetChar ();            while (t--) {p=u=i=0;            flag=1;            Gets (str);            for (i=0; I<strlen (str);)                    {if (str[i]== ' P ' &&str[i+1]== ' = ') {i + = 2;                        while (str[i]>= ' 0 ' &&str[i]<= ' 9 ') {p=p*10+str[i]-' 0 ';                    i++;                    } if (str[i] = = '. ')                        {i++;                            while (str[i]>= ' 0 ' &&str[i]<= ' 9 ') {flag=flag*0.1;                            p=p+flag* (str[i]-' 0 ');                        i++;        }            } flag=1;                        if (str[i]== ' m ') {p=p/1000;                    i++;                        } else if (str[i]== ' K ') {p=p*1000;                    i++;                        } else if (str[i]== ' M ') {p=p*1000000;                    i++;                    }} else if (str[i]== ' U ' &&str[i+1]== ' = ') {i+=2; while (str[i]>= ' 0 ' &&str[i]<= ' 9 ') {u=u*10+str[i]-                        ' 0 ';                    i++;                    } if (str[i]== '. ')                        {i++;                  while (str[i]>= ' 0 ' &&str[i]<= ' 9 ') {flag=flag*0.1;          u=u+flag* (str[i]-' 0 ');                        i++;                    }} flag=1;                        if (str[i]== ' m ') {u=u/1000;                    i++;                        } if (str[i]== ' K ') {u=u*1000;                    i++;                        } else if (str[i]== ' M ') {u=u*1000000;                    i++;                    }} else if (str[i]== ' i ' &&str[i+1]== ' = ') {i+=2; while (str[i]>= ' 0 ' &&str[i]<= ' 9 ') {i=i*10+str[i]-                        ' 0 ';                    i++;                    } if (str[i] = = '. ')                        {i++; while (str[i]>= ' 0 ' &&str[i]<= ' 9 ') {flag=flag*0.1;                            i=i+flag* (str[i]-' 0 ');                        i++;                    }} flag=1;                        if (str[i]== ' m ') {i= i/1000;                    i++;                        } if (str[i]== ' K ') {i=i*1000;                    i++;                        } else if (str[i]== ' M ') {i=i*1000000;                    i++;            }} i++;            } printf ("Problem #%d\n", k++);            if (p>0&&i>0) printf ("u=%.2lfv\n", p/i);            else if (p>0&&u>0) printf ("i=%.2lfa\n", p/u);            else if (u>0&&i>0) printf ("p=%.2lfw\n", u*i); printf ("\n "); }} return 0;}




UVA 537-artificial Intelligence? (analog)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.