欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[Molar Mass, ACM/ICPC Seoul 2007, UVA1586]

程序员文章站 2022-06-09 16:51:10
...

[Molar Mass, ACM/ICPC Seoul 2007, UVA1586]

[Molar Mass, ACM/ICPC Seoul 2007, UVA1586]

主要是固定位 的思想,这是最后的代码,不过中间一直wrong answer,一直找不出,原来是15写成了14.

#include <stdio.h>
#include <string.h>

int isnum(char c) {
    return c>='0' && c<='9';
}

int main() {
    int T;
    char s[85];
    //freopen("test.txt", "r", stdin);
    double wei[15];
    wei['C'- 'A'] = 12.01;
    wei['H'- 'A'] = 1.008;
    wei['O'- 'A'] = 16.00;
    wei['N'- 'A'] = 14.01;
    scanf("%d", &T);
    while(T--) {
        scanf("%s", s);
        int len = strlen(s);
        double sum = 0;
        for(int i=0; s[i]!='\0'; ++i) {
            if(i+3 == len) {
                if(isnum(s[i+1]) && isnum(s[i+2])) {
                    sum += wei[s[i] - 'A'] * ((s[i+1] - '0')*10 + (s[i+2] - '0'));
                    break;
                }
                else if(isnum(s[i+1])) {
                    sum += wei[s[i] - 'A'];
                    i++;
                }
            }
            if(i+2 == len && isnum(s[i+1])) {
                sum += wei[s[i] - 'A'] * (s[i+1] - '0');
                break;
            }

            if(isnum(s[i+1]) && isnum(s[i+2])) {
                sum +=  wei[s[i] - 'A'] * ((s[i+1] - '0')*10 + (s[i+2] - '0'));
                i+=2;
            }
            else if(isnum(s[i+1])) {
                sum +=  wei[s[i] - 'A'] * (s[i+1] - '0');
                i++;
            }
            else {
                sum +=  wei[s[i] - 'A'];
            }
        }
        printf("%.3f\n", sum);
    }
    return 0;
}
相关标签: uva