题目:537 - Artificial Intelligence?
题目大意:
通过题目字符串中的信息,计算未出现的I或P或U(m=0.001,k=1000,M=1000000)
解题思路:
将等号与单位‘W’,‘A’,‘V’,之间的数字或是字符提取出来放到一个数组里,然后判断提取出来的是否有换算单位,有的话进行换算,最后在计算没给的另一个(P,U,I)的大小.
#include<stdio.h> #include<math.h> #include<string.h> const int N = 100; int n, m , t; float ih, ph, uh; char str[N]; char s[3][N]; int judge (int x); float change (int x ,char *s); void store (int x) { char ch; int i; ch = str[x]; switch(ch) { case 'A': for ( i = 0; i < n; i++) { s[0][i]=str[x-n+i]; } ih = change(x, s[0]); break; case 'V': for ( i = 0; i < n; i++) { s[1][i]=str[x-n+i]; } uh = change(x, s[1]); break; case 'W': for (i = 0; i < n; i++) { s[2][i]=str[x-n+i]; } ph = change(x, s[2]); } } int judge (int x) { char ch = str[x-1]; switch(ch) { case 'm' : return -3; case 'k' : return 3; case 'M' : return 6; default: return 0; } } float change (int x ,char *s) { float i; m = judge(x); if (m != 0) s[x-1]='\0'; else s[x] = '\0'; sscanf(s,"%f",&i); i *= pow(10, m); return i; } void caculate() { if(ih == -1) { ih = ph/uh; printf("I=%0.2fA\n\n",ih); } if (uh == -1) { uh = ph/ih; printf("U=%0.2fV\n\n",uh); } if (ph == -1) { ph= uh*ih; printf("P=%0.2fW\n\n",ph); } } int main() { scanf("%d%*c",&t); for(int k = 0; k < t; k++ ){ memset(str,0,sizeof(str)); memset(s,-1,sizeof(s)); gets(str); ih = ph = uh = -1; for (int i = 0; i < sizeof(str); i++) { if (str[i] == '=') { n = 0; int j; for (j = i+1; str[j] != 'A' && str[j] != 'V' && str[j] != 'W'; j++) n++; store(j); } } printf("Problem #%d\n", k+1); caculate(); } return 0; }