Uva--10878 (字符串处理)

2014-05-31 19:41:32

题意 & 思路:有点密码学的味道(只是有点。。。呵呵),其实完全可以出的难一点,加入更多解密环节,这题就是单纯的给出ASCII码的二进制形式,让你输出字符。

从这个题目里还能洞察到细节,看懂解码方法的时间有待推敲,如果我们能熟悉记住各个字母所对应的数字、ASCII码,将会非常有帮助。

A B C D E F G

1  2 3 4  5 6 7 

H I  J    K   L  M  N

8 9 10 11 12 13 14

O   P  Q   R  S   T

15 16 17 18 19 20 (21 - 1)

U   V  W  X   Y   Z 

21 22 23 24 25 26 (28 - 2)

发现规律:前两组按照7倍数的规律,在第3,4组各消减1以保证26个字母,所以只用记住,“按7倍增,3,4组各减一”,即“7,14,20,26”。

ASCII 码更加方便,记住48、65、97即可,对应'0' 'A' 'a'。(还有32' ' 、 13'\n')

#include <stdio.h>
#include <iostream>
#include <string>
using namespace std;

int main(){
    //freopen("in","r",stdin);
    string line;
    int st = 1,head = 1,num = 0;
    while(getline(cin,line)){
        //judge
        if(line[0] == '_'){
            if(st)
                st = 0;
            else
                break;
            continue;
        }
        //read
        //cout << line << endl;
        int cnt = 0;
        if(line[9] != ' ')
            cnt += 1;
        if(line[8] != ' ')
            cnt += 2;
        if(line[7] != ' ')
            cnt += 4;
        if(line[5] != ' ')
            cnt += 8;
        if(line[4] != ' ')
            cnt += 16;
        if(line[3] != ' ')
            cnt += 32;
        if(line[2] != ' ')
            cnt += 64;
        printf("%c",cnt);
    }
    return 0;
}

 

posted @ 2014-05-31 19:43  Naturain  阅读(120)  评论(0编辑  收藏  举报