团体程序设计天梯赛(CCCC) L3019 代码排版 方法与编译原理密切相关,只有一个测试点段错误

 

团体程序设计天梯赛代码。体现代码技巧,比赛技巧。  https://github.com/congmingyige/cccc_code

  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <cmath>
  4 #include <cstring>
  5 #include <algorithm>
  6 using namespace std;
  7 
  8 const int maxn=1e3+10;
  9 
 10 char str[maxn],c=' ';
 11 int i,j,len,w=0;
 12 
 13 ///结构体:函数相互调用
 14 struct node
 15 {
 16     //void error()
 17     //{
 18     //    printf("wrong at %d",i);
 19     //    exit(0);
 20     //}
 21 
 22     void print(int x,int y)
 23     {
 24         for (int z=x;z<=y;z++)
 25             printf("%c",str[z]);
 26     }
 27 
 28     void pb()
 29     {
 30         for (int k=1;k<=w*2;k++)
 31             printf("%c",c);
 32     }
 33 
 34     void cls()
 35     {
 36         while (str[i]==' ')
 37             i++;
 38     }
 39 
 40     ///a line with end character ';'
 41     /**
 42     题目已经明确说明:
 43     顺序执行的程序体是以分号“;”结尾的,遇到分号就换行;
 44 
 45     另外的想法:
 46     如果使用注释//,则注释到底(代码只有一行),连'}'都会被注释掉
 47     如果使用#define,也不行,stray '#' in program
 48     **/
 49     void work5()
 50     {
 51         pb();
 52         j=i;
 53         ///这里代码有问题
 54 //        while (str[i]!=';')
 55         while (i<len && str[i]!=';')
 56             i++;
 57         if (i==len)
 58         {
 59             i--;
 60             exit(0);
 61         }
 62         print(j,i);
 63         i++;
 64         printf("\n");
 65     }
 66 
 67     ///condtion
 68     ///前后都可能有空格
 69     ///中间有可能有多个括号
 70     void work4()
 71     {
 72         printf("%c",c);
 73         cls();
 74         j=i;
 75 
 76         i++;
 77         int g=1;
 78         while (g!=0)
 79         {
 80             if (str[i]=='(')
 81                 g++;
 82             if (str[i]==')')
 83                 g--;
 84             i++;
 85             if (i>=len)
 86                 break;
 87         }
 88         i--;
 89 
 90         print(j,i),i++;
 91         cls();
 92         printf("%c",c);
 93     }
 94 
 95     ///for while
 96     void work3()
 97     {
 98         pb();
 99         if (str[i]=='f')
100             printf("for"),i+=3;
101         else
102             printf("while"),i+=5;
103         work4();
104         work1(str[i]=='{');
105     }
106 
107     ///if
108     void work2()
109     {
110         pb();
111         printf("if"),i+=2;
112         work4();
113         work1(str[i]=='{');
114 
115         ///如果可以没有else: if else 就近原则
116         cls();
117         if (len-i>=4 && str[i]=='e' && str[i+1]=='l' && str[i+2]=='s' && str[i+3]=='e')
118         {
119             pb();
120             printf("else%c",c),i+=4;
121             cls();
122             work1(str[i]=='{');
123         }
124     }
125 
126     ///{}
127     void work1(int cond)
128     {
129         cls();
130         if (cond==2)
131             pb();
132         printf("{\n");
133         if (cond!=0)
134             i++;
135         w++;
136         while (1)
137         {
138             ///段错误 / 运行超时 -> 答案错误
139 //            if (i>=len)
140 //                break;
141 
142             cls();
143             if (str[i]=='}' || i>=len)  ///|| i>=len test
144                 break;
145             cls();
146             if (len-i>=1 && str[i]=='{')
147                 work1(2);
148             else if (len-i>=2 && str[i]=='i' && str[i+1]=='f')
149                 work2();
150             else if ((len-i>=3 && str[i]=='f' && str[i+1]=='o' && str[i+2]=='r')
151                      ||
152                      (len-i>=5 && str[i]=='w' && str[i+1]=='h' && str[i+2]=='i' && str[i+3]=='l' && str[i+4]=='e'))
153                 work3();
154             else
155                 work5();
156             if (cond==0)
157                 break;
158         }
159         w--;
160         pb();
161         printf("}\n");
162         cls();
163         if (cond!=0)
164             i++;
165         if (i==len)
166             return;
167     //    if (str[i]=='{')
168     //        print(i,i);
169     //    else
170     //        error();
171     }
172 }f;
173 
174 
175 int main()
176 {
177     int j,k;
178     fgets(str,maxn,stdin);
179     len=strlen(str);
180 
181     ///special
182     if (str[len-1]=='\n')
183         len--,str[len]=0;
184 
185     for (i=0;i<len;i++)
186         if (str[i]=='{')
187             break;
188 
189     j=0;
190     while (str[j]==' ')
191         j++;
192     k=i-1;
193     while (str[k]==' ')
194         k--;
195     if (j<=k)
196         f.print(j,k);
197     printf("\n");
198     if (i!=len)
199         f.work1(2);
200     return 0;
201 }
202 
203 ///if else
204 /*
205 int main(){int i;if (3>2) { i=1; } else {i=2;} }
206 
207 int main(){int i;if (3>2) if (5>4) i=3; else i=1; else i=2;}
208 
209 int main(){int i=0; if (3>2) while (i<2) { if (3>2) y=1; else y=2 while (i<3) i++;} else i--; return 0;}
210 
211 int main(){int i=0; if (3>2) i++;else if (3>2) i++;else if (3>2)i++; else i+=2; return 0;}
212 
213 int main(){ if (3>2) while (3>2) while (4>3) i++; else while (4>3) j--; }
214 */
215 
216 ///if ...
217 /*
218 int main() {if (3>2) { y=1; } return 0;}
219 
220 int main() {if (3>2) y=1; return 0;}
221 
222 int main(){if (3>2) {if (4>3) { if (5>3) {int i=3;}   }}}
223 
224 int main(){if (3>2)  if (4>3) if (5>3) int i=3;}
225 
226 int main() {if (3>2) while (3>2) {if (3>2) y=1; if (3>2) y=1; } return 0;}
227 
228 int main() {  if (3>2) if (4>3) if (6>-2) y=3;    else if (3>2) y=3; else y=4;    return 0; }
229 
230 int main(){if (3>2)  while(3>2){if (3>2) return 3; else return 2; }else while(3>2){while(5>3) y=1;} return 0;}
231 
232 int main(){if (3>2)  while(3>2)if (3>2) return 3; else return 2; else while(3>2){while(5>3) y=1;} return 0;}
233 
234 int main(){int i=0; if (2<3) {if (3>2) while (i<2) while (i<3) i++; y=1; } return 0; }
235 
236 int main(){int i=0; if (3>2) i++;else if (3>2) i++;else if (3>2)i++; return 0;}
237 
238 */
239 ///while
240 /*
241 int main(){while (1>3)  while (3>2) while (5>3) i++; j--; k++;}
242 
243 int main(){while (1>3) if (3>2) y=3; else y=2 }
244 
245 int main(){while (1>3) if (3>2) y=3; }
246 
247 
248 */
249 ///for
250 /*
251 int main()  { for (int i=1;i<=3;i++) for (int j=1;j<=5;j++) k++; }
252 
253 int main() { for (int i=1;i<=3;i++) while (3>2) if(3>2) y=3;}
254 
255 int main() { for (int i=1;i<=3;i++) while (3>2) if(3>2) y=3;}
256 
257 int main() { for (int i=1;i<=3;i++) while (3>2) for (int i=1;i<=3;i++) while (3>2) if (3>2) j=1; else j=2; }
258 
259 int main() { for (int i=1;i<=3;i++) for (int i=1;i<=3;i++) while (3>2) for (int i=1;i<=3;i++) while (3>2) j=1; }
260 
261 int main() { for (int i=1;i<=3;i++) for (int i=1;i<=3;i++) if (3>2) for (int i=1;i<=3;i++) for (int i=1;i<=3;i++) j=1; else for (int i=1;i<=3;i++) for (int i=1;i<=3;i++) j=1; }
262 
263 int main() {for (i=1;i<=(1<<3|1);i+=2) j=1+(2+3)*5;   }
264 
265 int main() { for (i=1;i<=3;i++) { for (j=1;j<=3;j+=1+(2*(3*4))) while (1<3) { j=4; for (k=1;k<=3;k++) l=1; i=3; } i=1; j=2; k=3;}  }
266 */
267 ///{}
268 /*
269 int main() { {int i=2;} if (3>2) if (4>3) else 5>3; else i=1; }
270 
271 int main(){ { i=3; {i=5;} } }
272 
273 int main() { if (3>2) i++; {int i=2;} {int i=2;} }
274 
275 int main() { {int i=2;} { {} } {} }
276 
277 int main() { {int i=2;} { {i=3; {}} {} } }
278 
279 int main() {{{{}}}}
280 
281 int main() {{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{ }}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}}
282 {}
283 */
284 ///blank
285 /*
286    int main()  {   return 0;   }
287 int main(){}
288 
289 int main()  {    if           (3>2)        i++;    else    j--;    for (i=1;i<=5;i++)              j++;      while          (3>2)    i-;        }
290 
291 int main()  {    if           (3>2)   {     i++;   }  else   {   j--;  }  for (i=1;i<=5;i++)   {           j++;    }  while          (3>2)   {  i-;   }     }
292 
293 int main() { if (  3>2   )  i++;  else if (   5>3 &2<3) j--;  for (  i=1;i<=3;i++   )    i--;   while (1>3   )  j--; }
294 
295 int main() { if ( if (3>2) i++;  ) i++;}
296 
297 int main() { for (i=1;i<=3;i++) if (i<2) break; else j=1; }
298 
299 int main() { for (i=1;i<=n;i++) for (j=1;j<=n;j++) if (i!=j) for (k=1;k<=n;k++) if (i!=k && j!=k) { a=1; } else b=2; else c=3; }
300 */
301 ///其它 不必实现
302 /*
303 
304 //仅多个空格
305 
306 const int maxn=10;
307 
308 #include <cstdio>
309 
310 void work1(); void work2();
311 
312 ///not need, can't work
313 
314 const int maxn=10; int main() { i=3; }
315 
316 int work1() { return 1; } int main() { printf("%d",work1()); }
317 
318 int main(){void work1() {i=1; void work2(){ i=2; } }}
319 
320 */
321 ///wrong cases
322 /*
323 int main(){ //a=1 }
324 
325 int main(){ #define a=1 }
326 */

 

posted @ 2019-04-16 22:15  congmingyige  阅读(508)  评论(0编辑  收藏  举报