hdu 5949 Relative atomic mass

本文介绍了一种通过遍历字符串来计算化学分子式的摩尔质量的方法。使用C++实现,针对碳(C)、氢(H)和氧(O)三种元素,通过简单的字符串解析和数学计算得出最终摩尔质量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值