UVa1586
题目链接
https://vjudge.net/problem/UVA-1586
代码
#include<iostream>
#include<string.h>
int main()
{
int T, count;
char s[100]; // 字符串最大长度为 80,所以这里就稍微多分配了一点空间
double res; // 用来保存最后的结果
scanf("%d", &T);
while (T--)
{
scanf("%s", s);
int len = strlen(s);
count = -1;
res = 0;
double cell = 0;
for (int i = 0; i < len; i++)
{
char c = s[i];
switch (c)
{
case 'C':
if (count == 0)
count = 1;
res += cell * count;
count = 0;
cell = 12.01;
break;
case 'H':
if (count == 0)
count = 1;
res += cell * count;
count = 0;
cell = 1.008;
break;
case 'O':
if (count == 0)
count = 1;
res += cell * count;
count = 0;
cell = 16.00;
break;
case 'N':
if (count == 0)
count = 1;
res += cell * count;
count = 0;
cell = 14.01;
break;
default:
count = count * 10 + c - '0';
}
}
if (count == 0)
count = 1;
res += count * cell;
printf("%.3lf\n", res);
}
return 0;
}