zoj 1006 do the untwist
题目见zoj 1006 或poj 1317简单的解密算法,直接套用题目中公式即可。
/* zoj 1006 Do the Untwist */#include <stdio.h>#include <string.h>#define MAXLEN 80#define MAGICNUM 28char num2Char(int n);int char2Num(char c);int main(void){int key;char ciphertext,plaintext;int ciphercode,plaincode;int slen,i;while(scanf("%d", &key) == 1 && key != 0) { scanf("%s", ciphertext); slen = strlen(ciphertext); for(i = 0; i < slen; i++){ciphercode = char2Num(ciphertext);plaincode[(key * i) % slen] = (i+ciphercode)%MAGICNUM;} for(i = 0; i < slen; i++)plaintext = num2Char(plaincode); plaintext = '\0'; printf("%s\n",plaintext); }return 0;}int char2Num(char c){if( c == '_') return 0;else if( c == '.') return 27;else return c - 'a' + 1;}char num2Char(int n){if(n == 0) return '_';else if(n == 27) return '.';else return 'a' + n - 1;}
页:
[1]