uva UVa 537 Artificial Intellige…

来源:互联网 发布:python输入ctrl c 编辑:程序博客网 时间:2024/06/15 18:48

ArtificialIntelligence? 

Physics teachers in high school often think that problems givenas text are more demanding than pure computations. After all, thepupils 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 containsa battery with a voltage of U=10V and a light-bulb. There's anelectrical current of I=5A through the bulb. Which power isgenerated in the bulb?".

However, half of the pupils just don't pay attention to the textanyway. 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 notthe top scorers in physics tests. But at least this simplealgorithm is usually good enough to pass the class. (Sad buttrue.)

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


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

Input 

The first line of the input file will contain the number of testcases.

Each test case will consist of one line containing exactly twodata fields and some additional arbitrary words. A data field willbe 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) andM (Mega) may also occur. To summarize it: Data fieldsadhere 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 (`=') will never occur in an othercontext than within a data field.
  • There is no whitespace (tabs,blanks) inside a data field.
  • Either P and U, P and I, orU and I will be given.

Output 

For each test case, print three lines:

  • a line saying ``Problem #k" where k isthe 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 twodecimal places as shown in the sample output
  • a blank line

SampleInput 

3If 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?

SampleOutput 

Problem #1P=900.00WProblem #2I=0.45AProblem #3U=1250000.00V



 

代码:

#include<stdio.h>
#include<string.h>
int main()
{
 inti,j,len,flag,flag1,flag2,p,u,ii,count,num,cases;
 double z,z1,z2,p1,u1,i1,tt,ans,k;
 char s[10001],ch1,ch2;
 scanf("%d",&cases);
 getchar();
 num=1;
 while(cases--)
 {
  gets(s);
  len=strlen(s);
  p=u=ii=count=0;
  p1=u1=i1=0;
  for(i=0;i<len;i++)
  {
   
   if(s[i]=='=')
   {
    if(s[i-1]=='P'){p=1;flag=1;}
    if(s[i-1]=='U'){u=1;flag=2;}
    if(s[i-1]=='I'){ii=1;flag=3;}
    count++;
    z=z1=z2=0;
    flag1=flag2=0;
    for(j=i+1;j<len;j++)
    {
     if(s[j]>='0'&& s[j]<='9')
      z1=z1*10+s[j]-'0';
     if(s[j]=='.')
     {
      flag1=1;
      break;
     }
     if(s[j]=='m'){tt=0.001;flag2=1;break;}
     if(s[j]=='k'){tt=1000;flag2=1;break;}
     if(s[j]=='M'){tt=1000000;flag2=1;break;}
     if(s[j]=='A'|| s[j]=='V' || s[j]=='W')break;
    }
    if(flag1==1)
    {
     k=10;
     for(j=j+1;j<len;j++)
     {
        if(s[j]>='0'&& s[j]<='9')
      {
       z2=z2+(s[j]-'0')/k;
       k=k*10;
      }
        if(s[j]=='m'){tt=0.001;flag2=1;break;}
        if(s[j]=='k'){tt=1000;flag2=1;break;}
        if(s[j]=='M'){tt=1000000;flag2=1;break;}
        if(s[j]=='A' || s[j]=='V' || s[j]=='W')break;
     }
    }
    if(flag2==1)z=(z1+z2)*tt;
    elsez=z1+z2;
   // printf("%f%f %f asdf \n",z1,z2,z);
    if(flag==1)p1=z;
    if(flag==2)u1=z;
    if(flag==3)i1=z;
   }
   if(count==2)break;
  }//printf("%f %f%f  yf\n",u1,i1,p1);
  if(p==1&&u==1){ans=p1/u1;ch1='I',ch2='A';}
  if(p==1&&ii==1){ans=p1/i1;ch1='U',ch2='V';}
  if(u==1&&ii==1){ans=u1*i1;ch1='P',ch2='W';}
  printf("Problem#%d\n%c=%.2lf%c\n\n",num++,ch1,ans,ch2);
 }
    return0;
}

原创粉丝点击