UVa1586 分子量

题目详情https://vjudge.net/problem/UVA-1586

#include<cstdio>
#include<cstring>
#include<ctype.h>//包含isalpha函数判断是否为字母,否返回0,大写返回1,小写返回2
#define N 95
char a[N];
using namespace std;
double r(char ch)//将相应的字符转化为数字
{   
	if(isalpha(ch))
	{
		if(ch=='C') return 12.01;
		if(ch=='H') return 1.008;
		if(ch=='O') return 16.00;
		if(ch=='N') return 14.01;
	 } 
	 else return ch-'0';
}
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		double sum=0;		
		scanf("%s",a);
		int n=strlen(a);	 
		for(int i=0;i<n;i++)
		{ 
		    int ceng=0;
		    if(isalpha(a[i]))
		    {
		    	for(int j=i+1;j<n;j++)//当啊a[i]为字母时,向后判断数字,若没有则为1
			{
					if(!isalpha(a[j]))
			 	{
			 		ceng*=10;
			 		ceng+=r(a[j]);						 		    
				}
				else break;
			}
			if(!ceng) ceng=1;	
			 sum+=r(a[i])*ceng;	
		     }
		}								 			 			   		  
		printf("%.3lf\n",sum);  		    	
	}
	return 0; 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值