You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
52 lines
2.1 KiB
52 lines
2.1 KiB
#include <bits/stdc++.h>
|
|
|
|
using namespace std;
|
|
#define maxn 85
|
|
char s[maxn];
|
|
|
|
int main() {
|
|
int T;
|
|
cin >> T;
|
|
while (T--) {
|
|
int num_c = 0, num_h = 0, num_o = 0, num_n = 0; //分别记录各个化学元素的个数
|
|
float sum;
|
|
cin >> s;
|
|
int n = strlen(s);
|
|
s[n] = 1;
|
|
for (int i = 0; i < n; i++) {
|
|
if (isalpha(s[i])) { //判断是否是字母
|
|
switch (s[i]) {
|
|
case 'C':
|
|
if (isdigit(s[i + 1])) { //判断字母后面2位是否为数字
|
|
if (isdigit(s[i + 2])) num_c += (s[i + 1] - '0') * 10 + (s[i + 2] - '0');
|
|
else num_c += (s[i + 1] - '0');
|
|
} else num_c++;
|
|
break;
|
|
case 'H':
|
|
if (isdigit(s[i + 1])) {
|
|
if (isdigit(s[i + 2])) num_h += (s[i + 1] - '0') * 10 + (s[i + 2] - '0');
|
|
else num_h += (s[i + 1] - '0');
|
|
} else num_h++;
|
|
break;
|
|
case 'O':
|
|
if (isdigit(s[i + 1])) {
|
|
if (isdigit(s[i + 2])) num_o += (s[i + 1] - '0') * 10 + (s[i + 2] - '0');
|
|
else num_o += (s[i + 1] - '0');
|
|
} else num_o++;
|
|
break;
|
|
case 'N':
|
|
if (isdigit(s[i + 1])) {
|
|
if (isdigit(s[i + 2])) num_n += (s[i + 1] - '0') * 10 + (s[i + 2] - '0');
|
|
else num_n += (s[i + 1] - '0');
|
|
} else num_n++;
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
} else continue; //当前数组元素为数字,则直接进入下次循环
|
|
}
|
|
sum = 12.01 * num_c + 1.008 * num_h + 16.00 * num_o + 14.01 * num_n;
|
|
cout << fixed << setprecision(3) << sum << endl; //小数点后面保留三位数字
|
|
}
|
|
return 0;
|
|
} |