http://acm.split.hdu.edu.cn/showproblem.php?pid=5949
给出一串字符串,C代表碳,H代表氢,O代表氧,求摩尔质量。遍历即可。
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#include <cmath>
using namespace std;
char c[10005];
int main()
{
int n;
cin>>n;
while(n--)
{
cin>>c;
int n=strlen(c);
int a=0,b=0,d=0;
for(int i=0;i<n;i++)
{
if(c[i]=='H')
{
a++;
}
if(c[i]=='C')
{
b++;
}
if(c[i]=='O')
{
d++;
}
}
cout<<a+b*12+d*16<<endl;
}
return 0;
}