当前位置: 代码迷 >> 综合 >> 2020牛客暑期多校训练营(第九场)A.Groundhog and 2-Power Representation
  详细解决方案

2020牛客暑期多校训练营(第九场)A.Groundhog and 2-Power Representation

热度:32   发布时间:2024-02-08 14:05:27.0

2020牛客暑期多校训练营(第九场)A.Groundhog and 2-Power Representation

题目链接

题目描述

Groundhog took a math class. In this class, his math teacher said:

Any positive integer can be represented by the power of 2 2 . For example: 137 = 2 7 + 2 3 + 2 0 137=2^7+2^3+2^0 .

And powers are expressed in parentheses.That is , a ( b ) {a(b)} stands for a b {a^b} .Therefore, 137 137 can be expressed as 137 = 2 ( 7 ) + 2 ( 3 ) + 2 ( 0 ) 137={2(7)+2(3)+2(0)} .

Further more,for 7 = 2 2 + 2 + 2 0 7=2^2+2+2^0 is expressed with 2 {2} 3 = 2 + 2 0 3=2+2^0 ,137 can be finally expressed as 137 = 2 ( 2 ( 2 ) + 2 + 2 ( 0 ) ) + 2 ( 2 + 2 ( 0 ) ) + 2 ( 0 ) {137=2(2(2)+2+2(0))+2(2+2(0))+2(0)} .

Another example: 1315 = 2 10 + 2 8 + 2 5 + 2 + 1 = 2 ( 2 ( 2 + 2 ( 0 ) ) + 2 ) + 2 ( 2 ( 2 + 2 ( 0 ) ) ) + 2 ( 2 ( 2 ) + 2 ( 0 ) ) + 2 + 2 ( 0 ) 1315=2^{10}+2^8+2^5+2+1 = 2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0) .

Groundhog feels amazing and wants you to write a program to simulate the above content.You need to read in an expression that is a power of {2}2 and calculate its value.

输入描述:

Given a string, indicating the power representation.

输出描述:

Output the original number.

示例1

输入

2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)

输出

1315

熟悉 p y t h o n python 的都知道 p y t h o n python 有个 e v a l eval 函数可以直接求表达式的值,那么我们只需要把 ( ( 替换成 ? ? ( **( 即可,因为在 python 中 2 ? ? x 2**x 代表 x x 幂,AC代码如下:

print(eval(input().replace('(','**(')))
  相关解决方案