UVA-537

UVA-537

题目介绍:
Physics teachers in high school often think that problems given as text are more demanding than pure
computations. After all, the pupils have to read and understand the problem first!
So they don’t state a problem like “U=10V, I=5A, P=?” but rather like “You have an electrical
circuit that contains a battery with a voltage of U=10V and a light-bulb. There’s an electrical current
of I=5A through the bulb. Which power is generated in the bulb?”.
However, half of the pupils just don’t pay attention to the text anyway. They just extract from the
text what is given: U=10V, I=5A. Then they think: “Which formulae do I know? Ah yes, P=UI.
Therefore P=10V
5A=500W. Finished.”
OK, this doesn’t always work, so these pupils are usually not the top scorers in physics tests. But
at least this simple algorithm is usually good enough to pass the class. (Sad but true.)
Today we will check if a computer can pass a high school physics test. We will concentrate on the
P-U-I type problems first. That means, problems in which two of power, voltage and current are given
and the third is wanted.
Your job is to write a program that reads such a text problem and solves it according to the simple
algorithm given above.

大意:
读问题,根据给的P I U 的其中两者判断第三者
P=I*U
I=P/U
U=P/I
读字符串,找到=之后判断前面的字符,确定给的是哪个值,给它赋跟在后面的值,注意单位(m:10的-3次方,k:10的3次方,M:10的6次方)。

AC代码:

#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;

int P,U,I;
double num[5];

int main(){
	int n;
	scanf("%d",&n);
	getchar();
	for(int i=0;i<n;i++){
		P=U=I=0;
		string s;
		getline(cin,s);
		int len=s.length();
		int t=0;
		for(int j=0;j<len;j++){
			if(s[j]=='='){
				t=0;
				switch(s[j-1]){
					case 'P':
						P=1;
						t=1;
						break;
					case 'U':
						U=1;
						t=2;
						break;
					case 'I':
						I=1;
						t=3;
						break;
					default:
						break;
				}
				num[t]=0;
			}
			if(t){
				double temp=0.1;
				int ok=0;
				do{
					j++;
					if(s[j]=='.')ok=1;
					else if(isdigit(s[j])){
						if(!ok)num[t]=num[t]*10+s[j]-'0';
						else{
							num[t]=num[t]+temp*(s[j]-'0');
							temp/=10;
						}
					}
				}while(!isalpha(s[j]));
				if(s[j]=='m')num[t]*=pow(10,-3);
				else if(s[j]=='k')num[t]*=pow(10,3);
				else if(s[j]=='M')num[t]*=pow(10,6);
				t=0;
			}
		}
		printf("Problem #%d\n",i+1);
		double ans;
		if(P&&I){
			ans=num[1]/num[3];
			printf("U=%.2lfV\n",ans);
		}
		else if(P&&U){
			ans=num[1]/num[2];
			printf("I=%.2lfA\n",ans);
		}
		else if(U&&I){
			ans=num[2]*num[3];
			printf("P=%.2lfW\n",ans);
		}
		cout<<endl;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值