# UvaOJ 537 - Artificial Intelligence?

119 篇文章 0 订阅
23 篇文章 0 订阅
11 篇文章 0 订阅

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<iomanip>
using namespace std;

double P, I, U;

double cal(int p, int i, int u)
{
if(!u) return P/I;
else if(!p) return U*I;
else if(!i) return P/U;
}
double s2d(string s)
{
double ret = 0.0;
int ispre = 0;
int isdot = 0;
int dot = 1;
for(int i=0; i<(int)s.length(); i++)
{
if(s[i]=='m' || s[i]=='k' || s[i]=='M') { ispre=1; break; }
if(s[i] == '.') { isdot = 1; continue; }
if(isdot) dot*=10;
ret = ret*10+s[i]-'0';
}
ret/=(double)dot;
if(ispre)
{
switch(s[s.length()-1])
{
case 'm': ret/=1000; break;
case 'k': ret*=1000; break;
case 'M': ret*=1000000; break;
}
}
return ret;
}
int isp, isi, isu;
double solve(string s)
{
isp=0, isi=0, isu=0;
int p1 = s.find("U=", 0);
int p2 = s.find("I=", 0);
int p3 = s.find("P=", 0);
if(p1 != -1)
{
isu = 1;
int p = s.find('V', p1);
string t = s.substr(p1+2, p-p1-2);
U = s2d(t);
}
if(p2 != -1)
{
isi = 1;
int p = s.find('A', p2);
string t = s.substr(p2+2, p-p2-2);
I = s2d(t);
}
if(p3 != -1)
{
isp = 1;
int p = s.find('W', p3);
string t = s.substr(p3+2, p-p3-2);
P = s2d(t);
}
return cal(isp, isi, isu);
}
int main()
{
int t; cin>>t;
cin.get();
for(int i=1; i<=t; i++)
{
string s;
getline(cin, s);
cout<<"Problem #"<<i<<endl;
/*                              用cout，直接输出函数返回值，WA
if(!isp) cout<<"P";
else if(!isi) cout<<"I";
else if(!isu) cout<<"U";
cout<<"="<<setiosflags(ios::fixed)<<setprecision(2)<<solve(s);
if(!isp) cout<<"W";
else if(!isi) cout<<"A";
else if(!isu) cout<<"V";
cout<<endl;
cout<<endl;
*/                             // 用cout，先把函数返回值赋给ans，再输出ans，AC
double ans = solve(s);
if(!isp) cout<<"P";
else if(!isi) cout<<"I";
else if(!isu) cout<<"U";
cout.setf(ios::fixed);
cout.precision(2);
cout<<"="<<ans;
if(!isp) cout<<"W";
else if(!isi) cout<<"A";
else if(!isu) cout<<"V";
cout<<endl;
cout<<endl;
/*                                      用 printf AC。
double ans = solve(s);
if(!isp) printf("P=%.2fW\n", ans);
else if(!isi) printf("I=%.2fA\n", ans);
else if(!isu) printf("U=%.2fV\n", ans);
printf("\n");
*/
}
}

• 0
点赞
• 0
收藏
• 打赏
• 0
评论
10-11 2101
07-31 876
12-03 680
10-10 1万+
10-29 5765
03-28 1534
03-03 1414
12-05 1021
07-05 421
02-05 3641
03-29 1533
03-28 1063

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。