Problem Description
Relative atomic mass is a dimensionless physical quantity, the ratio of the average mass of atoms of an element (from a single given sample or source) to
12of
the mass of an atom of carbon-12 (known as the unified atomic mass unit).
You need to calculate the relative atomic mass of a molecule, which consists of one or several atoms. In this problem, you only need to process molecules which contain hydrogen atoms, oxygen atoms, and carbon atoms. These three types of atom are written as ’H’,’O’ and ’C’ repectively. For your information, the relative atomic mass of one hydrogen atom is 1, and the relative atomic mass of one oxygen atom is 16 and the relative atomic mass of one carbon atom is 12. A molecule is demonstrated as a string, of which each letter is for an atom. For example, a molecule ’HOH’ contains two hydrogen atoms and one oxygen atom, therefore its relative atomic mass is 18 = 2 * 1 + 16.
You need to calculate the relative atomic mass of a molecule, which consists of one or several atoms. In this problem, you only need to process molecules which contain hydrogen atoms, oxygen atoms, and carbon atoms. These three types of atom are written as ’H’,’O’ and ’C’ repectively. For your information, the relative atomic mass of one hydrogen atom is 1, and the relative atomic mass of one oxygen atom is 16 and the relative atomic mass of one carbon atom is 12. A molecule is demonstrated as a string, of which each letter is for an atom. For example, a molecule ’HOH’ contains two hydrogen atoms and one oxygen atom, therefore its relative atomic mass is 18 = 2 * 1 + 16.
Input
The first line of input contains one integer N(N ≤ 10), the number of molecules. In the next N lines, the i-th line contains a string, describing the i-th molecule. The length of each string would not
exceed 10.
Output
For each molecule, output its relative atomic mass.
Sample Input
5 H C O HOH CHHHCHHOH
Sample Output
1 12 16 18 46
AC代码:
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <math.h>
using namespace std;
int main()
{
int t,i,a,b,c,n;
char s[1005];
scanf("%d",&t);
getchar();
while(t--)
{
a=0;b=0;c=0;
gets(s);
n=strlen(s);
for(i=0;i<n;i++)
{
if(s[i]=='H')
{
a++;
}
else if(s[i]=='O')
{
b++;
}
else if(s[i]=='C')
{
c++;
}
}
printf("%d\n",a+b*16+c*12);
}
return 0;
}
本文介绍了一种计算分子相对原子质量的方法,特别是针对含有氢、氧、碳三种原子的分子。通过解析输入字符串来计数不同类型的原子数量,并利用已知的相对原子质量进行计算。
1012

被折叠的 条评论
为什么被折叠?



