POJ 2256 Artificial Intelligence?

本文提供了一个简单的算法,用于解决给定电压、电流或功率的物理电路问题,并通过实例展示了如何计算未知参数。

原题地址:http://poj.org/problem?id=2256


Artificial Intelligence?
Time Limit:1000MS
Memory Limit:65536K




Description

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=U*I. 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.

Input

The first line of the input will contain the number of test cases.
Each test case will consist of one line containing exactly two data fields and some additional arbitrary words. A data field will be of the form I=xA, U=xV or P=xW, where x is a real number. Directly before the unit (A,V or W) one of the prefixes m (milli), k (kilo) and M (Mega) may also occur. To summarize it: Data fields adhere to the following grammar:
DataField ::= Concept '=' RealNumber [Prefix] Unit
Concept   ::= 'P' | 'U' | 'I'

Prefix    ::= 'm' | 'k' | 'M'

Unit      ::= 'W' | 'V' | 'A'

Additional assertions:
  • The equal sign ('=') will never occur in an other context than within a data field.
  • There is no whitespace (tabs,blanks) inside a data field.
  • Either P and U, P and I, or U and I will be given.

Output

For each test case, print three lines:
  • a line saying "Problem #k" where k is the number of the test case
  • a line giving the solution (voltage, power or current, dependent on what was given), written without a prefix and with two decimal places as shown in the sample output
  • a blank line

Sample Input

3
If the voltage is U=200V and the current is I=4.5A, which power is generated?
A light-bulb yields P=100W and the voltage is U=220V. Compute the current, please.
bla bla bla lightning strike I=2A bla bla bla P=2.5MW bla bla voltage?

Sample Output

 
 
 

Problem #1
P=900.00W


Problem #2
I=0.45A


Problem #3
U=1250000.00V

由于开始以为是m是million的意思,WA了好多次,郁闷........

AC代码如下

/**************************************************************************
* Problem: POJ 2256 Artificial Intelligence?
* Copyright 2011 by Yan
* DATE:
* E-Mail: yming0221@gmail.com
************************************************************************/
#include <stdio.h>

char cache[2000];

char op1[50];
char op2[50];
double dop1,dop2;

int len;

void get_op(int p1,int p2)
{
	int index=p1;
	int op_index=0;
	int i;
	char tmp;/*存放单位字符*/

	while(cache[++index]!='A'&& cache[index]!='W'&& cache[index]!='V')
	{
		op1[op_index++]=cache[index];
	}

	op1[op_index]='\0';
	tmp=op1[op_index-1];

	if(tmp=='m'||tmp=='k'||tmp=='M') op1[op_index-1]='\0';/*从字符串中取出单位*/

	sscanf(op1,"%lf",&dop1);
	switch (tmp)
	{
		case 'k':dop1=dop1*1000.0;break;
		case 'm':dop1=dop1*0.001;break;
		case 'M':dop1=dop1*1000000.0;break;
	}

	index=p2;
	op_index=0;
	while(cache[++index]!='A'&& cache[index]!='W'&& cache[index]!='V')
	{
		op2[op_index++]=cache[index];
	}
	op2[op_index]='\0';
	tmp=op2[op_index-1];

	if(tmp=='m'||tmp=='k'||tmp=='M') op1[op_index-1]='\0';/*从字符串中取出单位*/

	sscanf(op2,"%lf",&dop2);
	switch (tmp)
	{
		case 'k':dop2=dop2*1000.0;break;
		case 'm':dop2=dop2*0.001;break;
		case 'M':dop2=dop2*1000000.0;break;
	}

}
int main()
{
	/*freopen("input","r",stdin);*/
	int n,i,j;
	int index[2];/*存放两个等号的位置*/
	int cnt;
	double ans;
	scanf("%d ",&n);
	for(i=1;i<=n;i++)
	{
		gets(cache);
		/*puts(cache);*/
		len=strlen(cache);

		cnt=0;

		for(j=0;j<len;j++)
		{
			if(cache[j]=='=') index[cnt++]=j;
			if(cnt==2) break;
		}/*扫描到两个等号*/

		get_op(index[0],index[1]);/*获取操作数和操作*/
		printf("Problem #%d\n",i);
		if( (cache[index[0]-1]=='U'&&cache[index[1]-1]=='I') || (cache[index[0]-1]=='I'&&cache[index[1]-1]=='U'))
		{
			printf("P=%.2fW\n\n",dop1*dop2);
		}
		else if( (cache[index[0]-1]=='P'&&cache[index[1]-1]=='I') || (cache[index[0]-1]=='I'&&cache[index[1]-1]=='P'))
		{
			if(cache[index[0]-1]=='P')
				printf("U=%.2fV\n\n",dop1/dop2);
			else printf("U=%.2fV\n\n",dop2/dop1);
		}
		else if( (cache[index[0]-1]=='U'&&cache[index[1]-1]=='P') || (cache[index[0]-1]=='P'&&cache[index[1]-1]=='U'))
		{
			if(cache[index[0]-1]=='P')
				printf("I=%.2fA\n\n",dop1/dop2);
			else printf("I=%.2fA\n\n",dop2/dop1);
		}
	}
	return 0;
}



## 软件功能详细介绍 1. **文本片段管理**:可以添加、编辑、删除常用文本片段,方便快速调用 2. **分组管理**:支持创建多个分组,不同类型的文本片段可以分类存储 3. **热键绑定**:为每个文本片段绑定自定义热键,实现一键粘贴 4. **窗口置顶**:支持窗口置顶功能,方便在其他应用程序上直接使用 5. **自动隐藏**:可以设置自动隐藏,减少桌面占用空间 6. **数据持久化**:所有配置和文本片段会自动保存,下次启动时自动加载 ## 软件使用技巧说明 1. **快速添加文本**:在文本输入框中输入内容后,点击"添加内容"按钮即可快速添加 2. **批量管理**:可以同时编辑多个文本片段,提高管理效率 3. **热键冲突处理**:如果设置的热键与系统或其他软件冲突,会自动提示 4. **分组切换**:使用分组按钮可以快速切换不同类别的文本片段 5. **文本格式化**:支持在文本片段中使用换行符和制表符等格式 ## 软件操作方法指南 1. **启动软件**:双击"大飞哥软件自习室——快捷粘贴工具.exe"文件即可启动 2. **添加文本片段**: - 在主界面的文本输入框中输入要保存的内容 - 点击"添加内容"按钮 - 在弹出的对话框中设置热键和分组 - 点击"确定"保存 3. **使用热键粘贴**: - 确保软件处于运行状态 - 在需要粘贴的位置按下设置的热键 - 文本片段会自动粘贴到当前位置 4. **编辑文本片段**: - 选中要编辑的文本片段 - 点击"编辑"按钮 - 修改内容或热键设置 - 点击"确定"保存修改 5. **删除文本片段**: - 选中要删除的文本片段 - 点击"删除"按钮 - 在确认对话框中点击"确定"即可删除
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值