HDU4690_EBCDIC 两种做法

好吧。。其实这道题比较有技巧的做法是对网页进行处理。

当然也可以选择直接暴力打表用map建立一个string to string 的映射,然后直接对给定字符串进行两两转换OK。

贴上两个代码:


map映射:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include<cmath>
#include <string>
#include<iomanip>
using namespace std;
map<string, string> maps;
int main()
{
    string s;
    maps["2E"]="06";
    maps["2F"]="07";
    maps["16"]="08";
    maps["05"]="09";
    maps["25"]="0A";
    maps["0B"]="0B";
    maps["0C"]="0C";
    maps["0D"]="0D";
    maps["D8"]="51";
    maps["D9"]="52";
    maps["E2"]="53";
    maps["E3"]="54";
    maps["E4"]="55";
    maps["E5"]="56";
    maps["E6"]="57";
    maps["E7"]="58";
    maps["E8"]="59";
    maps["83"]="63";
    maps["84"]="64";
    maps["85"]="65";
    maps["86"]="66";
    maps["87"]="67";
    maps["88"]="68";
    maps["0E"]="0E";
    maps["0F"]="0F";
    maps["10"]="10";
    maps["11"]="11";
    maps["12"]="12";
    maps["13"]="13";
    maps["3C"]="14";
    maps["3D"]="15";
    maps["D7"]="50";
    maps["89"]="69";
    maps["91"]="6A";
    maps["92"]="6B";
    maps["93"]="6C";
    maps["94"]="6D";
    maps["95"]="6E";
    maps["96"]="6F";
    maps["97"]="70";
    maps["98"]="71";
    maps["99"]="72";
    maps["A2"]="73";
    maps["A3"]="74";
    maps["A4"]="75";
    maps["A5"]="76";
    maps["A6"]="77";
    maps["A7"]="78";
    maps["A8"]="79";
    maps["A9"]="7A";
    maps["C0"]="7B";
    maps["4F"]="7C";
    maps["D0"]="7D";
    maps["A1"]="7E";
    maps["07"]="7F";
    maps["00"]="00";
    maps["E9"]="5A";
    maps["BA"]="5B";
    maps["E0"]="5C";
    maps["BB"]="5D";
    maps["B0"]="5E";
    maps["6D"]="5F";
    maps["79"]="60";
    maps["81"]="61";
    maps["82"]="62";
    maps["01"]="01";
    maps["02"]="02";
    maps["03"]="03";
    maps["37"]="04";
    maps["2D"]="05";
    maps["32"]="16";
    maps["26"]="17";
    maps["18"]="18";
    maps["19"]="19";
    maps["3F"]="1A";
    maps["27"]="1B";
    maps["1C"]="1C";
    maps["1D"]="1D";
    maps["1E"]="1E";
    maps["1F"]="1F";
    maps["40"]="20";
    maps["5A"]="21";
    maps["7F"]="22";
    maps["7B"]="23";
    maps["5B"]="24";
    maps["6C"]="25";
    maps["50"]="26";
    maps["7D"]="27";
    maps["4D"]="28";
    maps["5D"]="29";
    maps["5C"]="2A";
    maps["4E"]="2B";
    maps["6B"]="2C";
    maps["60"]="2D";
    maps["4B"]="2E";
    maps["61"]="2F";
    maps["F0"]="30";
    maps["F1"]="31";
    maps["F2"]="32";
    maps["F3"]="33";
    maps["F4"]="34";
    maps["F5"]="35";
    maps["F6"]="36";
    maps["F7"]="37";
    maps["F8"]="38";
    maps["F9"]="39";
    maps["7A"]="3A";
    maps["5E"]="3B";
    maps["4C"]="3C";
    maps["7E"]="3D";
    maps["6E"]="3E";
    maps["6F"]="3F";
    maps["7C"]="40";
    maps["C1"]="41";
    maps["C2"]="42";
    maps["C3"]="43";
    maps["C4"]="44";
    maps["C5"]="45";
    maps["C6"]="46";
    maps["C7"]="47";
    maps["C8"]="48";
    maps["C9"]="49";
    maps["D1"]="4A";
    maps["D2"]="4B";
    maps["D3"]="4C";
    maps["D4"]="4D";
    maps["D5"]="4E";
    maps["D6"]="4F";
    while(cin >> s)
    {
        int l=s.length();
        for(int i=0; i<l; i=i+2)
        {
            string temp=s.substr(i,2);
            cout << maps[temp];
        }
        cout << endl;
    }
}



字符串处理:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

//char        xxx[1][2][3] = {{{'a', 'b', '"'},{'c','d','f'}}};
char        EBCDIC[16][16][3] = {
{{'N','U','L'},{'S','O','H'},{'S','T','X'},{'E','T','X'},{' ',' ',' '},{'H','T',' '},{' ',' ',' '},{'D','E','L'},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{'V','T',' '},{'F','F',' '},{'C','R',' '},{'S','O',' '},{'S','I',' '}},
{{'D','L','E'},{'D','C','1'},{'D','C','2'},{'D','C','3'},{' ',' ',' '},{' ',' ',' '},{'B','S',' '},{' ',' ',' '},{'C','A','N'},{'E','M',' '},{' ',' ',' '},{' ',' ',' '},{'I','F','S'},{'I','G','S'},{'I','R','S'},{'I','U','S'}},
{{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{'L','F',' '},{'E','T','B'},{'E','S','C'},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{'E','N','Q'},{'A','C','K'},{'B','E','L'}},
{{' ',' ',' '},{' ',' ',' '},{'S','Y','N'},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{'E','O','T'},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{'D','C','4'},{'N','A','K'},{' ',' ',' '},{'S','U','B'}},
{{'S','P',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{'.',' ',' '},{'<',' ',' '},{'(',' ',' '},{'+',' ',' '},{'|',' ',' '}},
{{'&',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{'!',' ',' '},{'$',' ',' '},{'*',' ',' '},{')',' ',' '},{';',' ',' '},{' ',' ',' '}},
{{'-',' ',' '},{'/',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{',',' ',' '},{'%',' ',' '},{'_',' ',' '},{'>',' ',' '},{'?',' ',' '}},
{{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{'`',' ',' '},{':',' ',' '},{'#',' ',' '},{'@',' ',' '},{'\'',' ',' '},{'=',' ',' '},{'"',' ',' '}},
{{' ',' ',' '},{'a',' ',' '},{'b',' ',' '},{'c',' ',' '},{'d',' ',' '},{'e',' ',' '},{'f',' ',' '},{'g',' ',' '},{'h',' ',' '},{'i',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}},
{{' ',' ',' '},{'j',' ',' '},{'k',' ',' '},{'l',' ',' '},{'m',' ',' '},{'n',' ',' '},{'o',' ',' '},{'p',' ',' '},{'q',' ',' '},{'r',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}},
{{' ',' ',' '},{'~',' ',' '},{'s',' ',' '},{'t',' ',' '},{'u',' ',' '},{'v',' ',' '},{'w',' ',' '},{'x',' ',' '},{'y',' ',' '},{'z',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}},
{{'^',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{'[',' ',' '},{']',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}},
{{'{',' ',' '},{'A',' ',' '},{'B',' ',' '},{'C',' ',' '},{'D',' ',' '},{'E',' ',' '},{'F',' ',' '},{'G',' ',' '},{'H',' ',' '},{'I',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}},
{{'}',' ',' '},{'J',' ',' '},{'K',' ',' '},{'L',' ',' '},{'M',' ',' '},{'N',' ',' '},{'O',' ',' '},{'P',' ',' '},{'Q',' ',' '},{'R',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}},
{{'\\',' ',' '},{' ',' ',' '},{'S',' ',' '},{'T',' ',' '},{'U',' ',' '},{'V',' ',' '},{'W',' ',' '},{'X',' ',' '},{'Y',' ',' '},{'Z',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}},
{{'0',' ',' '},{'1',' ',' '},{'2',' ',' '},{'3',' ',' '},{'4',' ',' '},{'5',' ',' '},{'6',' ',' '},{'7',' ',' '},{'8',' ',' '},{'9',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}}

};
char        ASCII[18][16][3] = {
{{'N','U','L'},{'S','O','H'},{'S','T','X'},{'E','T','X'},{'E','O','T'},{'E','N','Q'},{'A','C','K'},{'B','E','L'},{'B','S',' '},{'H','T',' '},{'L','F',' '},{'V','T',' '},{'F','F',' '},{'C','R',' '},{'S','O',' '},{'S','I',' '}},
{{'D','L','E'},{'D','C','1'},{'D','C','2'},{'D','C','3'},{'D','C','4'},{'N','A','K'},{'S','Y','N'},{'E','T','B'},{'C','A','N'},{'E','M',' '},{'S','U','B'},{'E','S','C'},{'I','F','S'},{'I','G','S'},{'I','R','S'},{'I','U','S'}},
{{'S','P',' '},{'!',' ',' '},{'"',' ',' '},{'#',' ',' '},{'$',' ',' '},{'%',' ',' '},{'&',' ',' '},{'\'',' ',' '},{'(',' ',' '},{')',' ',' '},{'*',' ',' '},{'+',' ',' '},{',',' ',' '},{'-',' ',' '},{'.',' ',' '},{'/',' ',' '}},
{{'0',' ',' '},{'1',' ',' '},{'2',' ',' '},{'3',' ',' '},{'4',' ',' '},{'5',' ',' '},{'6',' ',' '},{'7',' ',' '},{'8',' ',' '},{'9',' ',' '},{':',' ',' '},{';',' ',' '},{'<',' ',' '},{'=',' ',' '},{'>',' ',' '},{'?',' ',' '}},
{{'@',' ',' '},{'A',' ',' '},{'B',' ',' '},{'C',' ',' '},{'D',' ',' '},{'E',' ',' '},{'F',' ',' '},{'G',' ',' '},{'H',' ',' '},{'I',' ',' '},{'J',' ',' '},{'K',' ',' '},{'L',' ',' '},{'M',' ',' '},{'N',' ',' '},{'O',' ',' '}},
{{'P',' ',' '},{'Q',' ',' '},{'R',' ',' '},{'S',' ',' '},{'T',' ',' '},{'U',' ',' '},{'V',' ',' '},{'W',' ',' '},{'X',' ',' '},{'Y',' ',' '},{'Z',' ',' '},{'[',' ',' '},{'\\',' ',' '},{']',' ',' '},{'^',' ',' '},{'_',' ',' '}},
{{'`',' ',' '},{'a',' ',' '},{'b',' ',' '},{'c',' ',' '},{'d',' ',' '},{'e',' ',' '},{'f',' ',' '},{'g',' ',' '},{'h',' ',' '},{'i',' ',' '},{'j',' ',' '},{'k',' ',' '},{'l',' ',' '},{'m',' ',' '},{'n',' ',' '},{'o',' ',' '}},
{{'p',' ',' '},{'q',' ',' '},{'r',' ',' '},{'s',' ',' '},{'t',' ',' '},{'u',' ',' '},{'v',' ',' '},{'w',' ',' '},{'x',' ',' '},{'y',' ',' '},{'z',' ',' '},{'{',' ',' '},{'|',' ',' '},{'}',' ',' '},{'~',' ',' '},{'D','E','L'}}

};
//char        s[4], t[15];
char          tmp[4];

int main()
{
    //freopen("1.txt", "r", stdin);
    int         i, j, x, y, flag, k;
    char        c;
    bool        ans;
    flag = 1;
    while (scanf("%c", &c) != EOF)
    {
        if (flag == 1)
        {
            switch(c)
            {
                case 'A':x = 10; break;
                case 'B':x = 11; break;
                case 'C':x = 12; break;
                case 'D':x = 13; break;
                case 'E':x = 14; break;
                case 'F':x = 15; break;
                default:
                    x = (int)c-(int)'0';
                    break;
            }
            flag = 2;
            continue;
        }
        else
        {
            switch(c)
            {
                case 'A':y = 10; break;
                case 'B':y = 11; break;
                case 'C':y = 12; break;
                case 'D':y = 13; break;
                case 'E':y = 14; break;
                case 'F':y = 15; break;
                default:
                    y = (int)c-(int)'0';
                    break;
            }
            flag = 1;
        }
        ans = false;
        for (i = 0; i <= 2; ++i) tmp[i] = EBCDIC[x][y][i];
        for (i = 0; i <= 8; ++i)
        {
            for (j = 0; j <= 15; ++j)
            {
                for (k = 0; k <= 2; ++k)
                {
                    if (tmp[k] != ASCII[i][j][k]) break;
                    if (k == 2)
                        { ans = true; break; }
                }
                if (ans) break;
            }
            if (ans) break;
        }
        printf("%d", i);
        if (j >= 10) printf("%c", char(j-10+(int)'A'));
            else printf("%d", j);
    }
    printf("\n");
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值