3-2 分子量 UVa1586

#include <vector>
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string mol="CHON";
	string word;
	cin >> word;

	int num = 0;
	vector<int> vec(4);
	for (int i = 0; i < (int)word.size(); i++)
	{
		if (isalpha(word[i]))
		{
			if (i == (int)word.size() - 1)
				num = 1;
			else
			{
				if (isdigit(word[i + 1]))
				{
					if (i + 2 < (int)word.size())
					{
						if (isdigit(word[i + 2]))
							num = word[i + 2] - '0' + (word[i + 1] - '0') * 10;
						else
							num = word[i + 1] - '0';
		
					}
				}
				else
					num = 1;
				
			}
			for (int j = 0; j < 4; j++)
				if (word[i] == mol[j])
					vec[j] += num;
		}
	}
	double sum = vec[0] * 12.01 + vec[1] * 1.008 + vec[2] * 16.00 + vec[3] * 14.01;
	cout << sum << endl;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/fuwu4087/article/details/80382005