UVA 537 - Artificial Intelligence?

本文介绍了一种通过解析输入字符串来计算电力系统中未知参数的方法。利用C++编程语言实现了对功率、电流、电压三项中任意两项的数值进行读取,并根据单位前缀m、k、M进行相应的数值转换。最后计算并输出未知的第三项参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目大意:输入字符串,其中包含功率,电流,电压其中两项的数值,m,k,M分别表示0.001,1000,1000000倍。求未知的第三项的数值。

解题思路:用atof函数读数值,注意有m,k,M时换算,如果不用atof函数,会麻烦一些,还要注意负数的情况。

ac代码:

#include <iostream>
#include <stdlib.h>
#include <cstring>
#include <map>
using namespace std;
char a[1005], temp[105];
int n, cnt=1, len, t1, t2, t3;
double I, P, U, temp2;
map <char, double>ma;
int main(){
	ma['m'] = 0.001, ma['k'] = 1000, ma['M'] = 1000000;
	scanf("%d", &n);
	getchar();
	while (n--){
		gets(a);
		len = strlen(a);
		t1 = t2 = t3 = 0;
		for (int i=0; i<len; i++)
			if (a[i-1] == '='){
				memset(temp, 0, sizeof(temp));
				for (int j=0; !isalpha(a[i]); j++,i++)
					temp[j] = a[i];
				temp2 = atof(temp);
				if (a[i] == 'm' || a[i] == 'k' || a[i] == 'M')
					temp2 = temp2 * ma[ a[i++] ];
				if (a[i] == 'W')
					P = temp2, t1 = 1;
				else if (a[i] == 'V')
					U = temp2, t2 = 1;	
				else
					I = temp2, t3 = 1;
			}
		printf("Problem #%d\n", cnt++);
		if (!t1)
			printf("P=%.2lfW\n", U*I);
		else if (!t2)
			printf("U=%.2lfV\n", P/I);
		else
			printf("I=%.2lfA\n", P/U);	
		printf("\n");
	}
return 0; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值