找出關鍵字U=???V或P=???W或I=???A的其中兩個,並從等式P=I*U算出另一個值。
想法:
函數分析時依序從整數,小數,以及指數的順序,在回傳兩個值後計算另一個即可。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstdio> | |
using namespace std; | |
double num (char line[],int i) | |
{ | |
double a = 0; | |
while (line[i]>='0' && line[i]<='9'){ | |
a *= 10; | |
a += (line[i]-'0'); | |
i++; | |
} | |
if (line[i] == '.'){ | |
i++; | |
int j = 10; | |
while (line[i]>='0' && line[i]<='9'){ | |
a += ((double)(line[i]-'0')/j); | |
j *= 10; | |
i++; | |
} | |
} | |
if (line[i] == 'm') a /= 1000; | |
else if (line[i] == 'k') a *= 1000; | |
else if (line[i] == 'M') a *= 1000000; | |
return a; | |
} | |
int main() | |
{ | |
// freopen ("input.txt","rt",stdin); | |
int Case; | |
char line[1000]; | |
scanf("%d",&Case); | |
getchar(); | |
for (int k=1; k<=Case; k++){ | |
gets(line); | |
double P=0,U=0,I=0; | |
for (int i=0; line[i+1]; i++){ | |
if (line[i]=='U' && line[i+1]=='=') U = num (line,i+2); | |
if (line[i]=='P' && line[i+1]=='=') P = num (line,i+2); | |
if (line[i]=='I' && line[i+1]=='=') I = num (line,i+2); | |
} | |
printf ("Problem #%d\n",k); | |
if (U && I) printf("P=%.2fW\n",U*I); | |
else if (P && U) printf ("I=%.2fA\n",P/U); | |
else printf("U=%.2fV\n",P/I); | |
printf("\n"); // 這題連最後一個problem都要空行 | |
} | |
return 0; | |
} |
沒有留言:
張貼留言