题目见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[MAXLEN],plaintext[MAXLEN];int ciphercode[MAXLEN],plaincode[MAXLEN];int slen,i;while(scanf("%d", &key) == 1 && key != 0){scanf("%s", ciphertext);slen = strlen(ciphertext);for(i = 0; i < slen; i++){ciphercode[i] = char2Num(ciphertext[i]);plaincode[(key * i) % slen] = (i+ciphercode[i])%MAGICNUM;}for(i = 0; i < slen; i++)plaintext[i] = num2Char(plaincode[i]);plaintext[slen] = '\0';printf("%s\n",plaintext);}return 0; } int char2Num(char c) {if( c == '_')return 0;else if( c == '.')return 27;elsereturn c - 'a' + 1; } char num2Char(int n) {if(n == 0)return '_';else if(n == 27)return '.';elsereturn 'a' + n - 1; }