• ZOJ 3542 2011大连现场赛D题(简单模拟)


    Hexadecimal View

    Time Limit: 2 Seconds       Memory Limit: 65536 KB

    Hexadecimal is very important and useful for computer programmers. You are requested to provide a hexadecimal view for given data. The hexadecimal view is made up of one or more rows. Every row except the last one represents 16 characters. Each row consists of three columns separated by a space:

    • addr: the 4-digit hexadecimal beginning address of this row.
    • dump: the hexadecimal representation of this row, separating every two characters by a whitespace. If there are less than 16 characters in the last row, pad it with spaces.
    • text: the ASCII translation of this row, with uppercase characters converted to lowercase and lowercase characters converted to uppercase.

    Use lowercase for the letter digits. See sample for more details.

     

    Input

    There are multiple test cases. Each line is a test case. The line is made up of no less than 1 and no more than 4096 printable characters including spaces.

    Output

    For each test case, output its hexadecimal view. Do not output any extra spaces after the last character of text.

    Sample Input

    Hex Dump
    #include <cstdio>
    printf("Hello, World!
    ");
    main = do getLine >>= print . sum . map read . words
    

    Sample Output

    0000: 4865 7820 4475 6d70                     hEX dUMP
    0000: 2369 6e63 6c75 6465 203c 6373 7464 696f #INCLUDE <CSTDIO
    0010: 3e                                      >
    0000: 7072 696e 7466 2822 4865 6c6c 6f2c 2057 PRINTF("hELLO, w
    0010: 6f72 6c64 215c 6e22 293b                ORLD!N");
    0000: 6d61 696e 203d 2064 6f20 6765 744c 696e MAIN = DO GETlIN
    0010: 6520 3e3e 3d20 7072 696e 7420 2e20 7375 E >>= PRINT . SU
    0020: 6d20 2e20 6d61 7020 7265 6164 202e 2077 M . MAP READ . W
    0030: 6f72 6473                               ORDS
    

                题目大意: 这个题目在看了A题觉得太复杂了,就开始看这个了。感觉应该是个比较简单的模拟,但是题目挂在HDU上面有问题。
    0000: 4865 7820 4475 6d70                     hEX dUMP
    0000: 2369 6e63 6c75 6465 203c 6373 7464 696f #INCLUDE &lt;CSTDIO
    0010: 3e                                      >
    0000: 7072 696e 7466 2822 4865 6c6c 6f2c 2057 PRINTF("hELLO, w
    0010: 6f72 6c64 215c 6e22 293b                ORLD!N");
    0000: 6d61 696e 203d 2064 6f20 6765 744c 696e MAIN = DO GETlIN
    0010: 6520 3e3e 3d20 7072 696e 7420 2e20 7375 E >>= PRINT . SU
    0020: 6d20 2e20 6d61 7020 7265 6164 202e 2077 M . MAP READ . W
    0030: 6f72 6473                               ORDS
    这是比赛时候的输出,当时看左简括号表示有点不解,其他的都想地差不多了。

          解题思路:给你一串字母,然后每次对16个字母转换成int再转化成16进制的,弄一行。每次输出地址0010,0020,最后一位是无效位。前三位表示地址。中间是dump,输出的是每个字母转换成16进制两位输出,每两个字母后跟一个空格,最后面跟的就是大小写转换即可。

         总结:开始WA了以后,我一直以为是地址的问题,长度4096会输出0000.4096 printable characters,这个还没有除以16,那才是地址。肯定这地方没错。每次交之前应该考虑特殊数据,比如长度为16的。。。

         题目地址:Hexadecimal View

    AC代码:
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <string>
    #include <map>
    #include <stack>
    #include <vector>
    #include <set>
    #include <queue>
    #define INF 0x3f3f3f3f
    #define maxn 105
    using namespace std;
    char a[5000];   //原串
    char b[5];     //最前面的地址
    int len,t1;
    char tex[20];   //最后面的大小写转换
    char dum[3];    //中间的字母转换成16进制的
    
    void addr(int x)  //最前面的地址
    {
        int i,p;
        p=0;
        int t[5];
        memset(t,0,sizeof(t));
        while(x)
        {
            t[p++]=x%16;
            x/=16;
        }
        for(i=0;i<=2;i++)
        {
            if(t[2-i]<10)
                b[i]=t[2-i]+'0';
            else
                b[i]=t[2-i]-10+'a';
        }
        /*for(i=2;i>=0;i--)
            cout<<t[i]<<" ";
        cout<<endl;*/
    }
    
    void dump(char x)  //中间的字母转换成16进制的
    {
        int i,p;
        p=0;
        int t[5];
        memset(t,0,sizeof(t));
        int l=int(x);
        while(l)
        {
            t[p++]=l%16;
            l/=16;
        }
        for(i=0;i<=1;i++)
        {
            if(t[1-i]<10)
                dum[i]=t[1-i]+'0';
            else
                dum[i]=t[1-i]-10+'a';
        }
    }
    
    void text(char x)   //最后面的大小写转换
    {
        /*if(x=='<')   //坑
        {
            tex[t1++]='&';
            tex[t1++]='l';
            tex[t1++]='t';
            tex[t1++]=';';
        }
        else*/
        if(x>='a'&&x<='z')
        {
            tex[t1++]=x-32;
        }
        else if(x>='A'&&x<='Z')
            tex[t1++]=x+32;
        else
            tex[t1++]=x;
    }
    
    int main()
    {
        int i,pp,j;
        while(gets(a))
        {
            len=strlen(a);
            pp=len/16;
            b[3]='0',b[4]='';  //地址最后一位无效直接为0
            for(i=0;i<pp;i++)
            {
                addr(i);
                cout<<b<<": ";   //最前面的地址
                t1=0;
                for(j=i*16;j<i*16+16;j++)
                {
                    dump(a[j]);
                    text(a[j]);
                    cout<<dum[0]<<dum[1]; 
                    if(j&1) cout<<" ";  //每两位一个空格
                }
                tex[t1]='';
                cout<<tex<<endl;  //大小写转换
            }
    
            t1=0;
            if(len>pp*16)   //开始这个地方写的>=,感谢吉吉debug
            {
                addr(i);
                cout<<b<<": ";
                for(j=i*16;j<len;j++)
                {
                    dump(a[j]);
                    text(a[j]);
                    cout<<dum[0]<<dum[1];
                    if(j&1) cout<<" ";
                }
                tex[t1]='';
                for(j=len;j<i*16+16;j++)
                {
                    cout<<"  ";  //补空格
                    if(j&1) cout<<" ";  
                }
                cout<<tex<<endl;
            }
        }
    
        /*int len=4096;
        addr(len);
        cout<<b<<endl;*/
        return 0;
    }
    
    //30ms 192KB
    


  • 相关阅读:
    MT【319】分段递推数列
    MT【318】分式不等式双代换
    Centos7环境变量
    VI快捷键
    Centos7 开机自动运行命令
    Centos7 编辑本地DNS解析配置文件
    Centos7修改主机名
    xadmin 自定义过滤器选项
    Centos7网卡配置文件
    Centos7 挂载
  • 原文地址:https://www.cnblogs.com/pangblog/p/3297158.html
Copyright © 2020-2023  润新知