题解

2 条题解

  • 2
    @ 2018-02-12 10:08:42

    某些语言中有一个函数叫做 eval(string)

    ;(function () {
        var str = readline();
        if (str.match(/[^\d\.\(\)\+\-\*\/]/g))
            return print("FormulaException");
        str = "var k = "+str;
        if (str.match(/[^\d\.]0\d/g) ||
                str.match(/[\*\/\+\-][\*\/\+\-]/g) ||
                str.match(/((\D\.\d)|(\d\.\D)|(\D\.\D))/g))
            return print("FormulaException");
        try {
            eval(str);
        } catch (e) {
            return print("FormulaException");
        }
        if (isNaN(k) || k == Infinity || k == -Infinity)
            return print("ZeroException");
        print(k.toFixed(2));
    })();
    
  • 0
    @ 2018-03-24 01:31:51

    @SW_Wind 的方法一样

    from decimal import Decimal
    
    s = raw_input()
    if len(s)>1 and s[0]=='0' and (s[1] not in '.+-*/'):
        print 'FormulaException'
    elif any(st in s for st in [sig1+sig2 for sig1 in '+-*/' for sig2 in '+-*/'] +
                             [sig+'0'+num for sig in '+-*/()' for num in '0123456789'] +
                             [x for x in 'abcdefghijklmnopqrstuvwxyz']):
        print 'FormulaException'
    else:
        try:
            print Decimal(eval(s)).quantize(Decimal('0.00'))
        except ZeroDivisionError:
            print 'ZeroException'
        except SyntaxError:
            print 'FormulaException'
        except NameError:
            print 'FormulaException'
        except ValueError:
            print 'FormulaException'
    
  • 1

信息

ID
2019
难度
8
分类
数据结构 | 字符串 | 表达式处理其他 | 数学 点击显示
标签
(无)
递交数
431
已通过
36
通过率
8%
被复制
1
上传者