absi2011's Blog & Daily Life.

全新的开始       我要省选翻盘       I wanna AK in 高考\化学       自此,生无可恋
[破碎的状态] [-53] Hdu 5456 Matches Puzzle Game
[破碎的状态] [-52] Hdu 5478 Can you find it

[破碎的状态] [-52] Hdu 5472 Code Formatting

absi2011 posted @ 9 年前 in 刷题记录 with tags HDU 小高考 瞎搞 , 979 阅读

题意:

给你一大串代码让你排版

报警了我居然不知道还能有单引号....早知道这样的话我估计能..早点AC?

不过..我是醉了..这代码还是比较难弄的...

看了别人一个代码才发现还有单引号的问题是没救了..

代码:

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#include<set>
#include<map>
#include<list>
#include<queue>
#include<stack>
#include<math.h>
#include<string>
#include<time.h>
#include<bitset>
#include<vector>
#include<memory>
#include<utility>
#include<stdio.h>
#include<sstream>
#include<fstream>
#include<iostream>
#include<stdlib.h>
#include<string.h>
#include<algorithm>
using namespace std;
char a[100005];
string this_line;
void get_solve()
{
    int i;
    int flag=-1;
    int sum=0;
    int space_counter=0;
    string last_word;
    for (i=0;a[i]!='\0';i++)
    {
        if (a[i]=='#')
        {
            //#include<xxx>
            putchar(a[i]);
            i++;
            int sum=0;
            for (;;i++)
            {
                if (a[i]==' ') continue;
                putchar(a[i]);
                sum++;
                if (sum==7) break;
            }
            putchar(' ');
            i++;
            for (;;i++)
            {
                if (a[i]==' ')
                {
                    continue;
                }
                putchar(a[i]);
                if (a[i]=='>') break;
            }
            putchar('\n');
            flag=0;
        }
        else if (a[i]!=' ')
        {
            if (flag==0) putchar('\n');
            if (flag==0) flag=-1;
            if (a[i]=='\"')
            {
                this_line+=a[i];
                i++;
                for (;;i++)
                {
                    this_line+=a[i];
                    if (flag==2)
                    {
                        flag=-1;
                        continue;
                    }
                    if (a[i]=='\\')
                    {
                        if (flag==-1) flag=2;
                    }
                    if (a[i]=='\"')
                    {
                        break;
                    }
                }
            }
            else if (a[i]=='\'')
            {
                this_line+=a[i];
                i++;
                for (;;i++)
                {
                    this_line+=a[i];
                    if (flag==2)
                    {
                        flag=-1;
                        continue;
                    }
                    if (a[i]=='\\')
                    {
                        if (flag==-1) flag=2;
                    }
                    if (a[i]=='\'')
                    {
                        break;
                    }
                }
            }
            else
            {
                if (a[i]==',')
                {
                    last_word="";
                    this_line+=a[i];
                    this_line+=' ';
                }
                else if ((a[i]=='>')||(a[i]=='<')||(a[i]=='=')||(a[i]=='!')||(a[i]=='+')||(a[i]=='-')||(a[i]=='*')||(a[i]=='/'))
                {
                    last_word="";
                    if (a[i+1]=='=')
                    {
                        this_line+=' ';
                        this_line+=a[i];
                        this_line+='=';
                        this_line+=' ';
                        i++;
                    }
                    else
                    {
                        this_line+=' ';
                        this_line+=a[i];
                        this_line+=' ';
                    }
                }
                else if ((a[i]=='{')||(a[i]=='}'))
                {
                    last_word="";
                    int j;
                    if (this_line!="")
                    {
                        for (j=0;j<space_counter;j++) putchar(' ');
                        cout<<this_line<<'\n';
                        this_line="";
                    }
                    if (a[i]=='}') space_counter-=2;
                    for (j=0;j<space_counter;j++) putchar(' ');
                    putchar(a[i]);
                    if (a[i]=='{') space_counter+=2;
                    putchar('\n');
                }
                else if (a[i]=='(')
                {
                    last_word="";
                    this_line+=a[i];
                    sum++;
                }
                else if (a[i]==')')
                {
                    last_word="";
                    this_line+=a[i];
                    sum--;
                }
                else if (a[i]==';')
                {
                    last_word="";
                    this_line+=a[i];
                    if (sum==0)
                    {
                        int j;
                        for (j=0;j<space_counter;j++) putchar(' ');
                        cout<<this_line<<'\n';
                        this_line="";
                    }
                    else
                    {
                        this_line+=' ';
                    }
                }
                else if (a[i]!=' ')
                {
                    last_word+=a[i];
                    this_line+=a[i];
                }
            }
        }
        else
        {
            if ((last_word=="int")||(last_word=="void")||(last_word=="double")||(last_word=="char")||(last_word=="bool"))
            {
                last_word="";
                this_line+=' ';
            }
        }
    }
}
int main()
{
    #ifdef absi2011
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
    int zu;
    scanf("%d",&zu);
    int t;
    gets(a);
    for (t=0;t<zu;t++)
    {
        gets(a);
        printf("Case #%d:\n",t+1);
        get_solve();
    }
}

登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter