ZOJ 3930 Dice Notation(模拟)

题意:

$给定规则让你展开式子,就读3个小圆点就可以了$
$1.ndX\Rightarrow(\underbrace{[dX] + \cdots + [dX])}_n,dX\Rightarrow [dX],+号左右各1个空格$
$2.给+-*/的左右各添加1个空格$
$3.最后添加,$”$ = [Result]$”

分析:

$有个坑,就是数字别解析,ndX的X也别解析$
$然后就能过了,赛上用BigInt逗比了,还是太懒,估计是把前导0给我去了$

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//
// Created by TaoSama on 2016-04-10
// Copyright (c) 2016 TaoSama. All rights reserved.
//
#pragma comment(linker, "/STACK:102400000,102400000")
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <set>
#include <vector>
using namespace std;
#define pr(x) cout << #x << " = " << x << " "
#define prln(x) cout << #x << " = " << x << endl
const int N = 1e5 + 10, INF = 0x3f3f3f3f, MOD = 1e9 + 7;
int n, idx;
char s[N], tmp[N];
char op[] = "+-*/";
bool isoperator(char c) {
return c && strchr(op, c);
}
void display(int n, char* tmp) {
if(n > 1) putchar('(');
printf("[d%s]", tmp);
for(int i = 1; i < n; ++i)
printf(" + [d%s]", tmp);
if(n > 1) putchar(')');
}
void dfs() {
while(s[idx] && s[idx] != ')') {
if(s[idx] == '(') {
putchar('('); ++idx;
dfs();
putchar(')'); ++idx;
}
if(isdigit(s[idx])) {
int l = 0, r = 0;
while(isdigit(s[idx])) tmp[l++] = s[idx++]; tmp[l] = 0;
if(s[idx] != 'd') {
printf("%s", tmp); //just digit
continue;
}
l = 0;
for(int i = 0; tmp[i]; ++i) l = l * 10 + tmp[i] - '0';
++idx; //jump d
while(isdigit(s[idx])) tmp[r++] = s[idx++]; tmp[r] = 0;
display(l, tmp);
}
if(isoperator(s[idx])) printf(" %c ", s[idx++]);
}
}
int main() {
#ifdef LOCAL
freopen("C:\\Users\\TaoSama\\Desktop\\in.txt", "r", stdin);
// freopen("C:\\Users\\TaoSama\\Desktop\\out.txt","w",stdout);
#endif
ios_base::sync_with_stdio(0);
int t; scanf("%d ", &t);
while(t--) {
char buf[2005]; gets(buf);
n = 0;
for(int i = 0; buf[i]; ++i) {
if(isspace(buf[i])) continue;
if(buf[i] == 'd' && (!s[n] || s[n] && !isdigit(s[n]))) s[++n] = '1';
s[++n] = buf[i];
}
s[n + 1] = 0;
idx = 1;
dfs();
printf(" = [Result]\n"); //puts("");
}
return 0;
}


1. 除非注明,本博文即为原创,转载请注明链接地址
2. 本博文只代表博主当时的观点或结论,请不要恶意攻击
3. 如果本文帮到了您,不妨点一下 下面分享到 按钮,让更多的人看到