`
SunnyYoona
  • 浏览: 366553 次
社区版块
存档分类
最新评论

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 theP-U-Itype 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 formI=xA,U=xVorP=xW, wherexis a real number.

Directly before the unit (A,VorW) one of the prefixesm(milli),k(kilo) andM(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.
  • EitherPandU,PandI, orUandIwill be given.

Output

For each test case, print three lines:

  • a line saying ``Problem #k" wherekis 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

【题意】:
根据P = U * I公式进行计算。
输入一行字符串,其中给出P,U,I三个变量的任意两个值求出第三个值。

【代码】:
  1. <spanstyle="font-style:normal;">/*********************************
  2. *日期:2013-4-28
  3. *作者:SJF0115
  4. *题号:题目537-ArtificialIntelligence?
  5. *来源:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=478
  6. *结果:AC
  7. *来源:UVA
  8. *总结:
  9. **********************************/
  10. #include<stdio.h>
  11. #include<string.h>
  12. intmain(){
  13. inti,j,Case,index=1;
  14. doubleP,I,U,c;
  15. charstr[1001];
  16. //freopen("C:\\Users\\XIAOSI\\Desktop\\acm.txt","r",stdin);
  17. while(scanf("%d\n",&Case)!=EOF){
  18. while(Case--){
  19. P=0;
  20. I=0;
  21. U=0;
  22. c=1;
  23. gets(str);
  24. for(i=0;i<strlen(str);){
  25. //P
  26. if(str[i]=='P'&&str[i+1]=='='){
  27. i+=2;
  28. //提取小数点前数据
  29. while(str[i]>='0'&&str[i]<='9'){
  30. P=P*10+str[i]-'0';
  31. i++;
  32. }
  33. //提取小数点后数据
  34. if(str[i]=='.'){
  35. i++;
  36. while(str[i]>='0'&&str[i]<='9'){
  37. c*=0.1;
  38. P+=c*(str[i]-'0');
  39. i++;
  40. }
  41. }
  42. c=1;
  43. if(str[i]=='k'){
  44. P*=1000;
  45. i++;
  46. }
  47. elseif(str[i]=='m'){
  48. P/=1000;
  49. i++;
  50. }
  51. elseif(str[i]=='M'){
  52. P*=1000000;
  53. i++;
  54. }
  55. }
  56. //U
  57. elseif(str[i]=='U'&&str[i+1]=='='){
  58. i+=2;
  59. //提取小数点前数据
  60. while(str[i]>='0'&&str[i]<='9'){
  61. U=U*10+str[i]-'0';
  62. i++;
  63. }
  64. //提取小数点后数据
  65. if(str[i]=='.'){
  66. i++;
  67. while(str[i]>='0'&&str[i]<='9'){
  68. c*=0.1;
  69. U+=c*(str[i]-'0');
  70. i++;
  71. }
  72. }
  73. c=1;
  74. if(str[i]=='k'){
  75. U*=1000;
  76. i++;
  77. }
  78. elseif(str[i]=='m'){
  79. U/=1000;
  80. i++;
  81. }
  82. elseif(str[i]=='M'){
  83. U*=1000000;
  84. i++;
  85. }
  86. }
  87. //I
  88. elseif(str[i]=='I'&&str[i+1]=='='){
  89. i+=2;
  90. //提取小数点前数据
  91. while(str[i]>='0'&&str[i]<='9'){
  92. I=I*10+str[i]-'0';
  93. i++;
  94. }
  95. //提取小数点后数据
  96. if(str[i]=='.'){
  97. i++;
  98. while(str[i]>='0'&&str[i]<='9'){
  99. c*=0.1;
  100. I+=c*(str[i]-'0');
  101. i++;
  102. }
  103. }
  104. c=1;
  105. if(str[i]=='k'){
  106. I*=1000;
  107. i++;
  108. }
  109. elseif(str[i]=='m'){
  110. I/=1000;
  111. i++;
  112. }
  113. elseif(str[i]=='M'){
  114. I*=1000000;
  115. i++;
  116. }
  117. }
  118. i++;
  119. }
  120. //输出
  121. printf("Problem#%d\n",index);
  122. if(P>0&&I>0){
  123. printf("U=%.2lfV\n",P/I);
  124. }
  125. elseif(P>0&&U>0){
  126. printf("I=%.2lfA\n",P/U);
  127. }
  128. elseif(U>0&&I>0){
  129. printf("P=%.2lfW\n",U*I);
  130. }
  131. printf("\n");
  132. index++;
  133. }
  134. }
  135. return0;
  136. }
  137. </span>

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics