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;
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值