/*
*State:    4054    0MS    276K    1990 B    C++
*题目大意:
*        字符串处理,把大写变小写,小写变大写,然后用计算机内存的形式
*        写出来。
*解题思路:
*        模拟吧,原来进制转换,可以写得很快的。
*/
View Code
  1 #include <iostream>
  2 using namespace std;
  3 
  4 const int MAX = 5005;
  5 char Hex[MAX][4];
  6 
  7 void deal16(int t, char ans[])
  8 {
  9     int tmp[15], h = 0;
 10     while(t)
 11     {
 12         tmp[h++] = t % 16;
 13         t /= 16;
 14     }
 15     for(int j = h-1, k = 0; j >= 0; j--, k++)
 16     {
 17         if(tmp[j] < 10)
 18             ans[k] = tmp[j] + '0';
 19         else
 20             ans[k] = (tmp[j] - 10) + 'a';
 21     }
 22     ans[h] = '\0';
 23 }
 24 
 25 void to16(char str[], int len)
 26 {
 27     for(int i = 0; i < len; i++)
 28     {
 29         int t, h = 0;
 30         t = str[i];
 31         deal16(t, Hex[i]);
 32     }
 33 }
 34 
 35 void output(char curstr[])
 36 {
 37     int len = strlen(curstr);
 38     int row;
 39     if(len % 16 == 0)
 40         row = len / 16;
 41     else
 42         row = len / 16 + 1;
 43     int rr = 0;
 44     for(int i = 0, num = 0; i < row; i++, num += 16)
 45     {
 46         char tmp[15];
 47         deal16(num, tmp);
 48         int l = strlen(tmp);
 49         for(int j = 0; j < 4 - l; j++)
 50             printf("0");
 51         if(l != 0)
 52             printf("%s:", tmp);
 53         else
 54             printf(":");
 55         
 56         if(len >= 16)
 57         {
 58             for(int i = 0; i < 16; i += 2)
 59             {
 60                 printf(" %s%s", Hex[rr + i], Hex[rr + i + 1]);
 61             }
 62             printf(" %c", curstr[rr + 0]);
 63             for(int i = 1; i < 16; i++)
 64                 printf("%c", curstr[rr + i]);
 65             printf("\n");
 66             len -= 16;
 67             rr += 16;
 68         }
 69         else if(len != 0)
 70         {
 71             printf(" ");
 72             int cnt = 0;
 73             for(int i = 0; i < len; i++)
 74             {
 75                 printf("%s", Hex[rr + i]);
 76                 cnt += 2;
 77                 if(i&1)
 78                 {
 79                     printf(" ");
 80                     cnt++;
 81                 }
 82             }
 83             for(int i = 0; i < 39 - cnt; i++)
 84                 printf(" ");
 85             printf(" %c", curstr[rr + 0]);
 86             for(int i = 1; i < len; i++)
 87                 printf("%c", curstr[rr + i]);
 88             printf("\n");
 89         }
 90     }
 91 }
 92 
 93 int main(void)
 94 {
 95 #ifndef ONLINE_JUDGE
 96     //freopen("in.txt", "r", stdin);
 97 #endif
 98     char str[MAX], curstr[MAX];
 99     while(gets(str))
100     {
101         int len = strlen(str);
102         for(int i = 0; i < len; i++)
103         {
104             if(str[i] >= 'A' && str[i] <= 'Z')          
105                 curstr[i] = tolower(str[i]);        
106             else
107                 curstr[i] = toupper(str[i]);        
108         }
109         curstr[len] = '\0';
110         to16(str, len);
111         output(curstr);
112     }
113     return 0;    
114 }
posted on 2012-08-06 09:50  cchun  阅读(193)  评论(0编辑  收藏  举报