2 colorlesssilver ColorlessSilver 于 2014.12.17 14:16 提问

一道算法基础题 uva1586 5C

题目链接在这儿 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]);
        }
    }
}

1个回答

feiyun0112
feiyun0112   Rxr 2014.12.18 14:49
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!