Molar mass
UVA - 1586
题目传送门
题目大意:给你一个只包含C,H,O,N分子式,其中C,H,O,N的原子量分别为:12.01,1.008,16.00,14.01,求其分子量
AC代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <set>
#include <utility>
#include <iomanip>
using namespace std;
typedef long long ll;
#define inf 0x3f3f3f3f
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define lep(i,l,r) for(int i=l;i>=r;i--)
#define ms(arr) memset(arr,0,sizeof(arr))
//priority_queue<int,vector<int> ,greater<int> >q;
const int maxn = (int)1e5 + 5;
const ll mod = 1e9+7;
int main()
{//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout);ios::sync_with_stdio(0),cin.tie(0);int T;cin>>T;while(T--){map<char,int> m;string s;cin>>s;int len=s.size();int t=1,num=0;bool ju=false;for(int i=len-1;i>=0;i--) //从后往前搜索,便于将数字提取出来{if(s[i]>='0'&&s[i]<='9'){num+=t*((int)(s[i]-'0')); //提取数字t=t*10;ju=true; }else if(ju) //判断这个数字表示的元素{m[s[i]]+=num;ju=false;num=0;t=1;}elsem[s[i]]++;}double ans=0;ans=(double)m['C']*12.01+(double)m['H']*1.008+(double)m['O']*16.00+(double)m['N']*14.01;cout<<fixed<<setprecision(3)<<ans<<endl; //保留三位小数输出}return 0;
}