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 @ May 31, 2016 09:24:44 AM in 刷题记录 with tags HDU 小高考 瞎搞 , 969 阅读

题意:

给你一大串代码让你排版

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

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

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

代码:

#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