`

zoj 1006 do the untwist

阅读更多

题目见zoj 1006poj 1317 

简单的解密算法,直接套用题目中公式即可。

 

/* zoj 1006 Do the Untwist */
#include <stdio.h>
#include <string.h>

#define MAXLEN 80
#define MAGICNUM 28

char 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;
  else
    return c - 'a' + 1;
}
char num2Char(int n)
{
  if(n == 0)
    return '_';
  else if(n == 27)
    return '.';
  else
    return 'a' + n - 1;
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics