原题地址:http://poj.org/problem?id=2256
Time Limit: 1000MS | Memory Limit: 65536K | |
Description
Input
Concept ::= 'P' | 'U' | 'I'
Prefix ::= 'm' | 'k' | 'M'
Unit ::= 'W' | 'V' | 'A'
Output
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
由于开始以为是m是million的意思,WA了好多次,郁闷........
AC代码如下
/************************************************************************** * Problem: POJ 2256 Artificial Intelligence? * Copyright 2011 by Yan * DATE: * E-Mail: [email protected] ************************************************************************/ #include <stdio.h> char cache[2000]; char op1[50]; char op2[50]; double dop1,dop2; int len; void get_op(int p1,int p2) { int index=p1; int op_index=0; int i; char tmp;/*存放单位字符*/ while(cache[++index]!='A'&& cache[index]!='W'&& cache[index]!='V') { op1[op_index++]=cache[index]; } op1[op_index]='\0'; tmp=op1[op_index-1]; if(tmp=='m'||tmp=='k'||tmp=='M') op1[op_index-1]='\0';/*从字符串中取出单位*/ sscanf(op1,"%lf",&dop1); switch (tmp) { case 'k':dop1=dop1*1000.0;break; case 'm':dop1=dop1*0.001;break; case 'M':dop1=dop1*1000000.0;break; } index=p2; op_index=0; while(cache[++index]!='A'&& cache[index]!='W'&& cache[index]!='V') { op2[op_index++]=cache[index]; } op2[op_index]='\0'; tmp=op2[op_index-1]; if(tmp=='m'||tmp=='k'||tmp=='M') op1[op_index-1]='\0';/*从字符串中取出单位*/ sscanf(op2,"%lf",&dop2); switch (tmp) { case 'k':dop2=dop2*1000.0;break; case 'm':dop2=dop2*0.001;break; case 'M':dop2=dop2*1000000.0;break; } } int main() { /*freopen("input","r",stdin);*/ int n,i,j; int index[2];/*存放两个等号的位置*/ int cnt; double ans; scanf("%d ",&n); for(i=1;i<=n;i++) { gets(cache); /*puts(cache);*/ len=strlen(cache); cnt=0; for(j=0;j<len;j++) { if(cache[j]=='=') index[cnt++]=j; if(cnt==2) break; }/*扫描到两个等号*/ get_op(index[0],index[1]);/*获取操作数和操作*/ printf("Problem #%d\n",i); if( (cache[index[0]-1]=='U'&&cache[index[1]-1]=='I') || (cache[index[0]-1]=='I'&&cache[index[1]-1]=='U')) { printf("P=%.2fW\n\n",dop1*dop2); } else if( (cache[index[0]-1]=='P'&&cache[index[1]-1]=='I') || (cache[index[0]-1]=='I'&&cache[index[1]-1]=='P')) { if(cache[index[0]-1]=='P') printf("U=%.2fV\n\n",dop1/dop2); else printf("U=%.2fV\n\n",dop2/dop1); } else if( (cache[index[0]-1]=='U'&&cache[index[1]-1]=='P') || (cache[index[0]-1]=='P'&&cache[index[1]-1]=='U')) { if(cache[index[0]-1]=='P') printf("I=%.2fA\n\n",dop1/dop2); else printf("I=%.2fA\n\n",dop2/dop1); } } return 0; }