习题3-2
https://vjudge.net/problem/UVA-1586
用3-1的输入模式,即一个字符一个字符读遇到\n结束,会wa
用字符串处理确是对的,很是奇怪
#include<iostream>
#include<cctype>
#include<string>
using namespace std;
int main() {
int n;
cin >> n;
getchar();
while (n--) {
char e;
int cnt = 0;
double sum = 0.0, per = 0.0;
string s;
cin >> s;
for(int i=0;i<s.size();++i){
e = s[i];
if (isalpha(e)) {
if (!cnt) cnt = 1;
sum += cnt*per;
cnt = 0;
switch (e)
{
case 'C':per = 12.01;
break;
case 'H':per = 1.008;
break;
case 'O':per = 16.00;
break;
default:per = 14.01;
break;
}
}
else if(isdigit(e))
cnt = cnt * 10 + e - '0';
}
if (!cnt) cnt = 1;
sum += cnt*per;
printf("%.3lf\n", sum);
}
}