分子量(Molar Mass, ACM/ICPC Seoul 2007, UVa1586)小弟不才,欢迎来改错
程序员文章站
2024-03-19 19:13:44
...
#include<iostream>
#include<stack>
#include<string>
using namespace std;
int main()
{
string s;
cin >> s;
double sum = 0;//注意不要声明为整型
for (int i = 0; i < s.length(); i++)
{
if (s[i + 1] > '1' && s[i + 1] <= '9')
{
if (s[i] == 'C')
sum += 12.01*(s[i+1]-48);
else if (s[i] =='H')
sum += 1.008*(s[i+1]-48);
else if (s[i] == 'O')
sum += 16.00*(s[i+1]-48);
else
sum += 14.01*(s[i+1]-48);
i++;
}
else
{
if (s[i] == 'C')
sum += 12.01 ;
else if (s[i] == 'H')
sum += 1.008;
else if (s[i] == 'O')
sum += 16.00;
else
sum += 14.01;
}
}
printf("%.3f", sum);
}
我也是服了,之前一直不通过是因为==我以为=;
还是要培养编程思维与计算机思维
上一篇: LeetCode 110. Balanced Binary Tree
下一篇: 509. 斐波那契数