一道算法基础题 uva1586

题目链接在这儿 http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=830&page=show_problem&problem=4461

我自己做的代码如下 但是通不过 测了好多数据都没问题

 #include<cstdio>
#include<cstring>
using namespace std;
int main()
{
    int n;
    char str[1000][1000];
    double arr[1000];
    while (scanf("%d", &n) != EOF)
    {
        getchar();
        for (int i = 0; i < n; i++){
            gets(str[i]);
            double sum = 0;
            double thisEle = 0;
            double orign = 0;
            for (int j = 0; j < strlen(str[i]); j++){
                if (str[i][j] == 'C'){
                    sum += thisEle;
                    thisEle = 12.01;
                }
                else if (str[i][j] == 'H'){
                    sum += thisEle;
                    thisEle = 1.008;
                }
                else if (str[i][j] == 'O'){
                    sum += thisEle;
                    thisEle = 16.00;
                }
                else if (str[i][j] == 'N'){
                    sum += thisEle;
                    thisEle = 14.01;
                }
                else{
                    if (str[i][j - 1] >= 48 && str[i][j - 1] <= 57){
                        double temp = 10 * thisEle;
                        thisEle = temp + (str[i][j] - 48)*orign;
                    }
                    else{
                        orign = thisEle;
                        thisEle *= (str[i][j] - 48);
                    }
                }
            }
            sum += thisEle;
            arr[i] = sum;
        }
        for (int i = 0; i < n; i++){
        printf("%.3lf\n", arr[i]);
        }
    }
}

https://github.com/svanegas/programming_solutions/blob/master/UVa/1586%20-%20Molar%20Mass/1586.cpp