【UVa】[1586]Molar mass

文章字数:269

问题描述

问题分析

题目英文说的很复杂
其实就是给一个分子式求它的分子量

读取字母判断原子量
需要注意CO、H2O等原子量为1的情况
(话说在下终于会用markdown写下标了)

在这里也是给自己加了点难度
用了递归的函数来查找原子个数

写的过程中也是遇到了点状况
好在最后还是一次AC了

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<stdio.h>
#include<string.h>
int t,l;
char s[100];
int find(int m) {
	if((s[m+1]>'A'&&s[m+1]<'Z')||(m+1==l)) {
		if(t==0)
			t=1;
		return t;
	} else {
		t=t*10+s[m+1]-'0';
		return find(++m);
	}
}
int main() {
	int n;
	double sum;
	scanf("%d",&n);
	while(n--) {
		scanf("%s",s);
		l=strlen(s);
		for(int i=sum=0; i<l; i++) {
			if(s[i]=='C') {
				t=0;
				sum+=12.01*find(i);
			} else if(s[i]=='H') {
				t=0;
				sum+=1.008*find(i);
			} else if(s[i]=='O') {
				t=0;
				sum+=16.00*find(i);
			} else if(s[i]=='N') {
				t=0;
				sum+=14.01*find(i);
			} else
				continue;
		}
		printf("%.3lf\n",sum);
	}
	return 0;
}

题目地址:【UVa】[1586]Molar mass

加载中...