/*===================
From : Zoj1006
Author : zscas08220
Algorithm :
ciphercode[i] = (plaincode[ki mod n] - i) % 28 --->
plaincode[ki % n] = (ciphercode[i]+i) % 28。
===================*/
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std ;
char CC [] = "_abcdefghijklmnopqrstuvwxyz." ;
char Msg [ 72 ];
int k , n, PT [ 72 ];
void ToPlainCode ()
{
for (int i = 0 ;i < n;i ++ )
{
switch (Msg [ i ])
{
case '_' :
PT [ i ] = 0 ;break ;
case '.' :
PT [ i ] = 27 ;break ;
default :
PT [ i ] = Msg [ i ] - 'a' + 1 ;
}
}
}
void Decrypt ()
{
int i ;
int tmp [ 72 ];
for (i = 0 ;i < n;i ++ ) tmp [ i ] = PT [ i ];
for (i = 0 ;i < n;i ++ )
{
PT [( k * i )% n] = (tmp [ i ] + i )% 28 ;
}
}
void ToText ()
{
for (int i = 0 ;i < n;i ++ )
{
Msg [ i ] = CC [ PT [ i ]];
}
}
int main ()
{
while (cin >> k && k )
{
cin >> Msg ;
n= strlen (Msg );
ToPlainCode ();
Decrypt ();
ToText ();
cout << Msg << endl ;
}
return 0 ;
}
From : Zoj1006
Author : zscas08220
Algorithm :
ciphercode[i] = (plaincode[ki mod n] - i) % 28 --->
plaincode[ki % n] = (ciphercode[i]+i) % 28。
===================*/
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std ;
char CC [] = "_abcdefghijklmnopqrstuvwxyz." ;
char Msg [ 72 ];
int k , n, PT [ 72 ];
void ToPlainCode ()
{
for (int i = 0 ;i < n;i ++ )
{
switch (Msg [ i ])
{
case '_' :
PT [ i ] = 0 ;break ;
case '.' :
PT [ i ] = 27 ;break ;
default :
PT [ i ] = Msg [ i ] - 'a' + 1 ;
}
}
}
void Decrypt ()
{
int i ;
int tmp [ 72 ];
for (i = 0 ;i < n;i ++ ) tmp [ i ] = PT [ i ];
for (i = 0 ;i < n;i ++ )
{
PT [( k * i )% n] = (tmp [ i ] + i )% 28 ;
}
}
void ToText ()
{
for (int i = 0 ;i < n;i ++ )
{
Msg [ i ] = CC [ PT [ i ]];
}
}
int main ()
{
while (cin >> k && k )
{
cin >> Msg ;
n= strlen (Msg );
ToPlainCode ();
Decrypt ();
ToText ();
cout << Msg << endl ;
}
return 0 ;
}
Zoj1006密码解密算法
本文介绍了一种基于Zoj1006题目的密码解密算法实现,该算法通过特定的数学运算完成密文到明文的转换。文章提供了完整的C++代码示例,包括将字符转换为代码、解密过程及还原为原文的方法。
925

被折叠的 条评论
为什么被折叠?



