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


  • 相关阅读:
    protobufer安装
    flume sink processor
    flume 参考
    uniq 和 sort -u的不同
    XtraInputBox-How to set the XtraInputBox as password input?
    原型模式
    C#-IEditableObject Interface
    gridControl-The BindingSource.AddingNew event does not fire until I begin typing into the new row
    C#中Invoke 和 BeginInvoke 的区别
    C#启动时全屏显示窗体...
  • 原文地址:https://www.cnblogs.com/mofushaohua/p/7789484.html
Copyright © 2020-2023  润新知