传送门
思路:
- 题意:将所给字符计算成十进制数。
- 常规的 递归处理 + 高精度 即可。
- 嗐,我太太太菜了,又是躺舒服的一天,模拟模拟不得行,递归也不行,只有赛后看看大佬们的代码学习。
代码实现:
#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {
{
1, 0}, {
-1, 0}, {
0, 1}, {
0, -1}};
using namespace std;
const int inf = 0x7fffffff;
const double PI = acos(-1.0);
const double eps = 1e-6;
const ll bi = 1e8;
const ll mod = 1e9 + 7;
const int N = 2e5 + 5;string s;
bool vis[600];
ll ans[50], b[50];int dfs(int &i){
int res = 0;i ++;while(s[i] != ')'){
if(s[i] == '2'){
if(s[i+1] == '(') res += 1 << dfs(++i); // 这里计算2的幂次方else if(s[i+1] == '+') res += 2, i ++; // else if(s[i+1] == ')') {
res += 2, i ++; break;}}if(s[i] == '0') {
i ++; break;}if(s[i] == '+') i ++;}i ++;return res;
}signed main(){
cin >> s;for(int i = 0; i < s.size(); i ++){
if(s[i] == '2'){
if(s[i+1] == '(') vis[dfs(++ i)] = 1;else vis[1] = 1;}}b[0] = 1; //将大整数分拆成1e8的小数储存,最后再买次直接(压位)输出1e8的数for(int i = 0; i < 600; i ++){
int tmp = 0;if(vis[i]){
for(int i = 0; i < 40; i ++){
ans[i] += b[i] + tmp;tmp = ans[i]/bi;ans[i] %= bi;}}tmp = 0;for(int i = 0; i < 40; i ++){
b[i] = b[i]*2 + tmp;tmp = b[i]/bi;b[i] %= bi;}}for(int i = 39, l = 0; ~i; i --){
if(l) printf("%08lld", ans[i]); //压位输出if(!l && ans[i]) l = 1, printf("%lld", ans[i]);}return 0;
}
队友AC的Py代码:
s = input()
res = 0
cnt = 0
list = []
for i in s:if i =='(':list.append(i)elif i =='+':list.append(i)elif i==')':top = list.pop()tmp = 0a = 0while(top!='('):if top != '+':tmp += toptop = list.pop()top = list.pop()top = top**tmplist.append(top)else :list.append(int(i))
for i in list:if i != '+':res += i
print(res)