//#include <iostream>
//#include <iomanip>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <cstring>
//using namespace std;
int main() {
//ios::sync_with_stdio(false);
//FILE *f = fopen("in.txt", "r");
char c[85];
int n, i;
scanf("%d", &n);
float result = 0;
for(; n > 0; n--) {
scanf("%s", c);
for(i = 0; i < strlen(c); i++) {
//相对原子质量
float mass;
switch(c[i]) {
case 'C':
mass = 12.01;
break;
case 'H':
mass = 1.008;
break;
case 'O':
mass = 16.00;
break;
case 'N':
mass = 14.01;
break;
default:
mass = 0;
}
//判断i往后位上是否为数字,得到数字的长度
int numberlength = 0;
int j = i + 1;
for(;; j++) {
if(isdigit(c[j])) {
numberlength++;
} else {
break;
}
}
//将char数组中数字转换为int
int amount = 0;
int k = 0;
if(numberlength != 0) {
for(; k < numberlength; k++) {
amount += (int) pow(10, numberlength - k - 1) * (c[i + k + 1] - '0');
}
} else amount = 1;
//计算总结果
result += mass * amount;
//跳过数字
i += numberlength;
}
printf("%.3f\n", result);
result = 0;
}
//fclose(f);
return 0;
}
UVa 1586 Molar Mass
最新推荐文章于 2022-12-26 00:23:51 发布