ColorlessSilver 2014-12-17 06:16 采纳率: 0%
浏览 3336
已结题

一道算法基础题 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]);
        }
    }
}
  • 写回答

1条回答

报告相同问题?

悬赏问题

  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度