记录编号 444754 评测结果 AAAAA
题目名称 [NOIP 2000PJ]计算器的改良 最终得分 100
用户昵称 GravatarHeHe 是否通过 通过
代码语言 C++ 运行时间 0.002 s
提交时间 2017-09-04 08:56:10 内存使用 0.65 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

inline char getc(void) { 
    static char buf[1 << 18], *fs, *ft;
    return (fs == ft && (ft = (fs = buf) + fread(buf, 1, 1 << 18, stdin)), fs == ft) ? EOF : *fs++;
}

inline unsigned read(char *s) { 
    char *p = s;
    while(!isgraph(*p = getc()));
    while(isgraph(*(++p) = getc()));
    *p = '\0'; 
    return p - s;
}

#define MAXN (10010)

char s[MAXN], X;
unsigned len;
int f1[MAXN], f2[MAXN];
int cnt1, cnt2, tmp;
int ans1, ans2;
double ans;

int main() { 
#ifndef LOCAL
    freopen("computer.in", "r", stdin);
    freopen("computer.out", "w", stdout);
#endif
    len = read(s);
    for(unsigned i = 0 ; i < len; ++i) 
        if(isalpha(s[i])) { 
            X = s[i];
            break;
        }
    unsigned i = 0;
    while(isdigit(s[i])) 
        tmp = ((tmp + (tmp << 2)) << 1) + (s[i++] ^ 0x30);
    if(tmp) { 
        if(isalpha(s[i])) f1[++cnt1] = tmp, ++i;
        else f2[++cnt2] = tmp;
    }
    while(i < len) 
        if(s[i] == '-') { 
            ++i;
            tmp = 0;
            while(isdigit(s[i])) 
                tmp = ((tmp + (tmp << 2)) << 1) + (s[i++] ^ 0x30);
            if(!tmp) tmp = -1;
            else tmp = -tmp;
            if(isalpha(s[i])) f1[++cnt1] = tmp, ++i;
            else f2[++cnt2] = tmp;
        }
        else if(s[i] == '+') { 
            ++i;
            tmp = 0;
            while(isdigit(s[i])) 
                tmp = ((tmp + (tmp << 2)) << 1) + (s[i++] ^ 0x30);
            if(!tmp) tmp = 1;
            if(isalpha(s[i])) f1[++cnt1] = tmp, ++i;
            else f2[++cnt2] = tmp;
        }
        else if(isalpha(s[i])) f1[++cnt1] = 1;
        else if(s[i] == '=') { 
            ++i; break;
        }
    for(int i = 1; i <= cnt1; ++i) ans1 += f1[i];
    for(int i = 1; i <= cnt2; ++i) ans2 -= f2[i];
    cnt1 = 0, cnt2 = 0, tmp = 0;
    while(isdigit(s[i])) 
        tmp = ((tmp + (tmp << 2)) << 1) + (s[i++] ^ 0x30);
    if(tmp) { 
        if(isalpha(s[i])) f1[++cnt1] = tmp, ++i;
        else f2[++cnt2] = tmp;
    }
    while(i < len) 
        if(s[i] == '-') { 
            ++i;
            tmp = 0;
            while(isdigit(s[i])) 
                tmp = ((tmp + (tmp << 2)) << 1) + (s[i++] ^ 0x30);
            if(!tmp) tmp = -1;
            else tmp = -tmp;
            if(isalpha(s[i])) f1[++cnt1] = tmp, ++i;
            else f2[++cnt2] = tmp;
        }
        else if(s[i] == '+') { 
            ++i;
            tmp = 0;
            while(isdigit(s[i])) 
                tmp = ((tmp + (tmp << 2)) << 1) + (s[i++] ^ 0x30);
            if(!tmp) tmp = 1;
            if(isalpha(s[i])) f1[++cnt1] = tmp, ++i;
            else f2[++cnt2] = tmp;
        }
        else if(isalpha(s[i])) f1[++cnt1] = 1, ++i;
    for(int i = 1; i <= cnt1; ++i) ans1 -= f1[i];
    for(int i = 1; i <= cnt2; ++i) ans2 += f2[i];
    ans = (double)ans2 / ans1;
    if(ans == -0) ans = 0;
    printf("%c=%.3lf", X, ans);
    return 0;
}