UVA 1586 Molar mass
An organic compound is any member of a large class of chem- ical compoundswhose molecules contain carbon. The molar mass of an organic compound is the mass of one mole of the organic compound. The molar mass of an organic compound can be computed from the standardatomic weights of the elements.
When an organic compoundis given as a molecular for- mula, Dr. CHON wants to find its molar mass. A molecular formula, such as C3H4O3, identifies each constituent element by its chemical symbol and indicates the numberof atoms of each element found in each discrete moleculeof that compound.If a moleculecontains more than one atom of a particularele-
ment, this quantityis indicated using a subscript after the chemical symbol.
In this problem, we assume that the molecularformula is represented by only four elements, ‘C’ (Carbon), ‘H’ (Hydrogen), ‘O’ (Oxygen), and ‘N’(Nitrogen) without parentheses.
The following table shows that the standardatomic weights for ‘C’,‘H’, ‘O’, and ‘N’.
Atomic Name | Carbon | Hydrogen | Oxygen | Nitrogen |
Standard Atomic Weight | 12.01 g/mol | 1.008 g/mol | 16.00 g/mol | 14.01 g/mol |
For example, the molar mass of a molecular formula C6H5OH is 94.108 g/mol which is computed by 6 ×(12.01 g/mol) + 6 × (1.008 g/mol) + 1 × (16.00 g/mol).
Given a molecular formula, write a programto compute the molar mass of the formula.
Input
Your program is to read from standardinput. The input consists of T test cases.The number of test cases T is given in the first line of the input.Each test case is given in a single line, which contains a molecularformula as a string. The chemical symbol is given by a capitalletter and the length of the string is greater than 0 and less than 80. The quantity number n which is represented after the chemical symbol would be omitted when the number is 1 (2 ≤ n ≤ 99).
Output
Your program is towrite to standard output. Printexactly one line for eachtest case. The line should contain the molar mass of the given molecular formula.
Sample Input
4
C C6H5OH
NH2CH2COOH C12H22O11
Sample Output
12.010
94.108
75.070
342.296
题意:
给定一个只含有 C H O N 的化学式,求出相对分子质量。
思路:
模拟的题目,挺有趣
AC CODE:
#include<stdio.h>
#include<cstring>
#include<algorithm>
#define HardBoy main()
#define ForMyLove return 0;
using namespace std;
const int MYDD = 1103;
int HardBoy {
int n, cnt[27] = {0};
double Ar[27] = {0.0};
Ar['C'-'A'] = 12.01;/*对应符号的相对原子质量*/
Ar['H'-'A'] = 1.008;
Ar['O'-'A'] = 16.00;
Ar['N'-'A'] = 14.01;
scanf("%d", &n);
while(n--) {
char ch[MYDD];
scanf("%s", ch);
int shu = 0;
memset(cnt, 0, sizeof(cnt));
char zi;
for(int j = 0; j < strlen(ch); j++) {
if(ch[j] > 'A' && ch[j] <= 'Z') {
cnt[ch[j] - 'A']++;
zi = ch[j];
} else {/*统计字母之后的数字大小*/
while(ch[j] >= '0' && ch[j] <= '9') {
shu = ch[j] - '0' + shu*10;
j++;
}
j--;
}
cnt[zi - 'A'] += shu;
if(shu > 1)
cnt[zi - 'A']--;
shu = 0;
}
double ans = 0.0;
for(int j = 0; j < 27; j++) {
// if(cnt[j])
// printf("%c : cnt -> %d, Ar -> %.3lf\n", j+'A', cnt[j], Ar[j]);
ans += cnt[j]*Ar[j];
}
printf("%.3lf\n", ans);
}
ForMyLove
}
/*
7
C6H5OH
*/