24h購物| | PChome| 登入
2012-05-11 06:41:49| 人氣603| 回應0 | 上一篇 | 下一篇

[UVA][字串處理] 444 - Encoder and Decoder

推薦 0 收藏 0 轉貼0 訂閱站台


 Encoder and Decoder 

Being in charge of the computer department of the Agency of International Espionage, you are asked to write a program that will allow a spy to encode and decode their messages.

You can assume a spy's message is at most 80 characters long, and it includes all the upper and lowercase letters of the alphabet plus the space, and any of the following characters:

!  ,  .  :  ;  ?

The following is an ASCII table of the valid characters in a message:

     "A"  65   "a"  97   " "  32
     "B"  66   "b"  98   "!"  33
      .         .        ","  44
      .         .        "."  46
      .         .        ":"  58
     "Y"  89   "y"  121  ";"  59
     "Z"  90   "z"  122  "?"  63

The algorithm that you should use to encode messages is to take the ASCII value of each character in the message, starting with the last character in the message and ending with the first character in the message. You should then add on to the coded message this ASCII value written in reverse order. For example, if the ASCII value is 123, the encoded message should contain the string "321". There should be no spaces separating the numbers in the encoded message.

Input and Output

The input file consists of one or more lines with a normal (not encoded) or encoded message each.

Output file must have the same number of lines with the corresponding encoded message or the decoded one, respectively.

Sample Input

abc
798999
Have a Nice Day !

Sample Output

998979
cba
332312179862310199501872379231018117927




#include <stdio.h>
#include <string.h>
int main() {
    char str[999];
    char ascii[128] = {};
    int i;
    ascii[' '] = ascii['!'] = ascii[','] = 1;
    ascii['.'] = ascii[':'] = ascii[';'] = 1;
    ascii['?'] = 1;
    for(i = 'a'; i <= 'z'; i++)
        ascii[i] = 1;
    for(i = 'A'; i <= 'Z'; i++)
        ascii[i] = 1;
    while(gets(str)) {
        int i, j, tmp, idx = 0;
        char code[999];
        if(str[0] >= '0' && str[0] <= '9') {
            tmp = 0;
            for(i = strlen(str)-1; i >= 0; i--) {
                tmp = tmp*10 + str[i]-'0';
                if(ascii[tmp]) {
                    code[idx++] = tmp;
                    tmp = 0;
                }
            }
            if(ascii[tmp]) {
                putchar(tmp);
                tmp = 0;
            }
        } else {
            for(i = strlen(str)-1; i >= 0; i--) {
                while(str[i]) {
                    code[idx++] = str[i]%10+'0';
                    str[i] /= 10;
                }
            }
        }
        for(i = 0; i < idx; i++)
            putchar(code[i]);
        puts("");
    }
    return 0;
}

台長: Morris
人氣(603) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][字串搜尋] 10010 - Where's Waldorf?
此分類上一篇:[UVA] 11849 - CD

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文