原题连接:
https://vjudge.net/problem/UVA-537
AC代码:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
getchar();
for (int t = 1; t <= n; t++)
{
cout << "Problem #" << t << endl;
char str[500];
fgets(str, 500, stdin);
double p = 0, u = 0, i = 0;
int cnt = 0; //记录变量,两个就break
for (int j = 0; str[j] != '\0'; j++)
{
if (cnt == 2)
break;
if (str[j] == 'U' && str[j + 1] == '=')
{
cnt++;
char num[10];
int z = 0;
j = j + 2;
while (str[j] != 'm' && str[j] != 'k' && str[j] != 'M' && str[j] != 'V')
{
num[z++] = str[j++];
}
num[z] = '\0';
u = atof(num);
if (str[j] == 'm')
u *= 0.001;
else if (str[j] == 'k')
u *= 1000.0;
else if (str[j] == 'M')
u *= 1000000.0;
continue;
}
if (str[j] == 'I' && str[j + 1] == '=')
{
cnt++;
char num[10];
int z = 0;
j = j + 2;
while (str[j] != 'm' && str[j] != 'k' && str[j] != 'M' && str[j] != 'A')
{
num[z++] = str[j++];
}
num[z] = '\0';
i = atof(num);
if (str[j] == 'm')
i *= 0.001;
else if (str[j] == 'k')
i *= 1000.0;
else if (str[j] == 'M')
i *= 1000000.0;
continue;
}
if (str[j] == 'P' && str[j + 1] == '=')
{
cnt++;
char num[100];
int z = 0;
j = j + 2;
while (str[j] != 'm' && str[j] != 'k' && str[j] != 'M' && str[j] != 'W')
{
num[z++] = str[j++];
}
num[z] = '\0';
p = atof(num);
if (str[j] == 'm')
p *= 0.001;
else if (str[j] == 'k')
p *= 1000.0;
else if (str[j] == 'M')
p *= 1000000.0;
continue;
}
}
if (p == 0)
{
printf("P=%.2lfW\n\n", u * i);
}
else if (u == 0)
{
printf("U=%.2lfV\n\n", (p / i));
}
else if (i == 0)
{
printf("I=%.2lfA\n\n", (p / u));
}
}
return 0;
}
这题一开始脑子抽了 前缀没管是在分子还是分母都给乘后面了,虽然样例过了但死活不对,后来改了改样例才发现问题在哪里 着实无情