537 - Artificial Intelligence?
一开始没看清题(uva的题目就是坑爹) WA了好多次。原因:k,m,M是通用单位,PUI都可以用。
#include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> using namespace std; double readnum(char *a,int begin,int *end) { int i=begin,j=0; char tmp[1001]; double num; memset(tmp, '\0', 1001); while(!isalpha(a[i])) { tmp[j]=a[i]; i++;j++; } tmp[j]='\0'; *end=i; sscanf(tmp,"%lf",&num); //printf("a: %stmp:%s num:%lf\n",a,tmp,num); if(a[i]=='m') num=num/1000.0; else if(a[i]=='k') num=num*1000.0; else if(a[i]=='M') num=num*1000000.0; return num ; } int main() { int k; scanf("%d",&k);fgetc(stdin);//吃一个回车 for(int i=1;i<=k;i++) { double P=0,U=0,I=0; char problem[1001],concept; char prefix; double realnum; int end; fgets(problem,1000,stdin); for(int j=0;j<strlen(problem);j++) { if(problem[j]=='=') { concept=problem[j-1]; realnum=readnum(problem,j+1,&end); prefix=problem[end]; //printf("realnum:%lf\n",realnum); if (concept=='I')I=realnum; if (concept=='U')U=realnum; if (concept=='P')P=realnum; } } printf("Problem #%d\n",i); //printf("%lf %lf %lf\n",P,U,I); if(I==0) {double ans=P/U;printf("I=%.2fA\n",ans);} else if(U==0) {double ans=P/I;printf("U=%.2fV\n",ans);} else if(P==0) {double ans=I*U;printf("P=%.2fW\n",ans);} printf("\n"); } return 0; }