题目链接:点击打开链接
题目给P,U,I中任意2个求第三个变量
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
char data[1000];
double x[2],unit[2];
int locate(int i,int len,int num)
{
int po,dimo=0,flag=0;x[num]=0;
for(int j=i;j<len;j++)
{
if(data[j]=='=')
{
po=j;
j++;
while((data[j]>='0'&&data[j]<='9')||data[j]=='.')
{
if(data[j]!='.')
x[num]=x[num]*10+data[j]-'0';
else flag=1; if(flag) dimo++;
j++;
}
if(dimo) x[num]=x[num]/pow(10,dimo-1);
if(data[j]=='m')unit[num]=1e-3;
else if(data[j]=='k')unit[num]=1e3;
else if(data[j]=='M')unit[num]=1e6;
else unit[num]=1;
return po;
}
}
}
int main()
{
double ans=0;
int t,p1,p2,len;
scanf("%d",&t);
getchar();
for(int i=1;i<=t;i++)
{
gets(data);
len=strlen(data);
p1=locate(0,len,0);p2=locate(p1+2,len,1);
if((data[p1-1]=='U'&&data[p2-1]=='I')||(data[p2-1]=='U'&&data[p1-1]=='I'))
{
ans=(double)x[0]*x[1]*unit[1]*unit[0];
printf("Problem #%d\nP=%.2lfW\n\n",i,ans);
}
else if(data[p1-1]=='P'&&data[p2-1]=='I')
{
ans=(double)x[0]*unit[0]/x[1]/unit[1];
printf("Problem #%d\nU=%.2lfV\n\n",i,ans);
}
else if(data[p2-1]=='P'&&data[p1-1]=='I')
{
ans=(double)x[1]*unit[1]/x[0]/unit[0];
printf("Problem #%d\nU=%.2lfV\n\n",i,ans);
}
else if(data[p1-1]=='P'&&data[p2-1]=='U')
{
ans=(double)x[0]*unit[0]/x[1]/unit[1];
printf("Problem #%d\nI=%.2lfA\n\n",i,ans);
}
else if(data[p2-1]=='P'&&data[p1-1]=='U')
{
ans=(double)x[1]*unit[1]/x[0]/unit[0];
printf("Problem #%d\nI=%.2lfA\n\n",i,ans);
}
}
return 0;
}