uva537

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=7&problem=478&mosmsg=Submission+received+with+ID+8990394

前一个代码本人觉得很漂亮,但是却过不了,希望有人能给以解释,后面的代码是ac的字符模拟

1 #include <stdio.h> 2 int main() 3 { 4     int sum; 5     char key,last,unit; 6     scanf("%d\n",&sum); 7     for(int k=1;k<=sum;k++) 8     { 9         double num=0.0,U=0.0,I=0.0,P=0.0;10         for(int counter=0;counter<2;counter++)11         {12             while((key=getchar())!='=') last=key;13             scanf("%lf%c",&num,&unit);14             switch(last)15             {16                 case 'U':if(unit=='k') num*=1000;U=num;break;17                 case 'I':if(unit=='m') num/=1000;I=num;break;18                 case 'P':if(unit=='M') num*=1000000;P=num;break;19                 default:break;20             }21         }22         printf("Problem #%d\n",k);23         if(!U) printf("U=%.2fV\n",P/I);24         if(!I) printf("I=%.2fA\n",P/U);25         if(!P) printf("P=%.2fW\n",U*I);26         printf("\n");27     }28     return 0;29 }
1 #include <math.h> 2 #include <stdio.h> 3 #include <string.h> 4 #include <stdlib.h> 5 void find(char s[],int c,double &key) 6 { 7     while(s[c]>='0'&&s[c]<='9') 8      key=key*10+(s[c++]-'0');                  //处理小数点前的整数 9     if(s[c]=='.')10     {11         int k=0;12         c++;13         while(s[c]>='0'&&s[c]<='9')14          key=key*10+(s[c++]-'0'),k++;15         key/=pow(10,k);16     }                                        //处理小数点后的整数17     switch(s[c])18     {19         case 'm':key/=1000;break;20         case 'k':key*=1000;break;21         case 'M':key*=1000000;break;22         default:break;23     }                                        //处理单位24 }25 int main()26 {27     int sum;28     scanf("%d\n",&sum);29     for(int k=1;k<=sum;k++)30     {31         char s[10000];32         gets(s);33         double U=0,P=0,I=0;34         int c=0;35         while(c<strlen(s)-1)36         {37             if(s[c+1]=='=')38             {39                 switch(s[c])40                 {41                     case 'U':find(s,c+2,U);break;42                     case 'P':find(s,c+2,P);break;43                     case 'I':find(s,c+2,I);break;44                     default:break;45                 }46             }47             c++;48         }49         printf("Problem #%d\n",k);50         if(!U) printf("U=%.2fV\n",P/I);51         if(!I) printf("I=%.2fA\n",P/U);52         if(!P) printf("P=%.2fW\n",U*I);53         printf("\n");54     }55     return 0;56 }

原文链接: https://www.cnblogs.com/mengxm-lincf/archive/2011/06/26/2090886.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/27687

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 上午5:17
下一篇 2023年2月8日 上午5:17

相关推荐