zoukankan      html  css  js  c++  java
  • UVA

    //收获:运用isdigit()函数,解决了元素后面的原子个数可能有两位的情况。同时,每次遇到元素字母,计数工作都由函数完成,简化代码
    //值得一提的是,这题后来又被我写了一次,充分利用了数组之间的一一对应,减少了许多代码...简洁许多啦!
    //借鉴了下面blog的部分思路: http://blog.csdn.net/u014800748/article/details/32718177
    
    
    


    #include <iostream>
    #include <cstring>
    #include <iomanip>
    #include <cctype>
    using namespace std;
    const int maxn = 200;
    char s[maxn];
    
    void getnumber(char*s, int i, int &a)
    {
    	if ( isdigit(s[i + 1]) && isdigit(s[i + 2]) )
    		a += (s[i + 1] - '0') * 10 + (s[i + 2] - '0');
    	else
    		a += (isdigit(s[i + 1]) ? (s[i + 1] - '0') : 1);		
    }
    
    int main()
    {
    	int t;
    	cin >> t;
    	while (t--)
    	{
    		cin >> s;
    		int len = strlen(s), a[4];
    		double mass[4] = { 12.01, 1.008, 16.00, 14.01 }, sum = 0;
    		char element[4] = { 'C', 'H', 'O', 'N'};
    		memset(a, 0, sizeof (a));
    		for (int i = 0; i < len; i++)
    		{
    //			if (s[i] == 'C')
    //			getnumber(s, i, a[0]);
    //			
    //			else if (s[i] == 'H')
    //			getnumber(s, i, a[1]);
    //			
    //			else if (s[i] == 'O')
    //			getnumber(s, i, a[2]);
    //			
    //			else if (s[i] == 'N')
    //			getnumber(s, i, a[3]);		
    			for (int j = 0; j < 4; j++)
    			if (s[i] == element[j])
    			{
    				getnumber(s, i, a[j]);
    				continue;
    			}
    		}
    		
    		for (int i = 0; i < 4; i++)
    			sum += (mass[i] * a[i]);
    			cout << fixed << setprecision(3) << sum << endl;
    	}
    	
    	return 0;
    }


  • 相关阅读:
    linux 操作系统 基础
    [HAOI2011]Problem A
    [HNOI2015] 菜肴制作
    [P3676]小清新数据结构题
    [NOI2016]区间
    [BOI2007]Mokia 摩基亚
    [NOI2012]美食节
    [CQOI2015]网络吞吐量
    [六省联考2017]期末考试
    [HNOI2015]亚瑟王
  • 原文地址:https://www.cnblogs.com/mofushaohua/p/7789484.html
Copyright © 2011-2022 走看看