您的位置:首页 > 其它

ZOJ 1009 转子加密

2010-05-11 10:01 357 查看
//这里有新的输入输出格式,注意其实输入格式跟输出格式是分开的,即系就算某一输出改变在另一输入前,
//只要输出格式符合要求就行
//用三重for做
#include<iostream>
#include<string>
using namespace std;
int st[3][30],sttemp[3][30];
char s[10000];

void insert(int n)
{
int i,j;
char s;
//先输入转子
for(i=0;i<3;i++)
for(j=0;j<n;j++)
{
cin>>s;
sttemp[i][j]=s-'A';
}

//把编码转子转为译码转子
for(i=0;i<3;i++)
for(j=0;j<n;j++)
st[i][sttemp[2-i][j]]=j; //其实就是数组值与下标的对应转换

for(i=0;i<3;i++)
for(j=0;j<n;j++)
sttemp[i][j]=st[i][j];

}

//此函数是让第a个转子移一下位,同时译码转子改变
//转子移一位其实是st[a][i]的值等于其前一个st[a][i+1]的值+1
void rotor(int a,int n)
{
int temp,i;
//由于有覆盖,要先保存st[a][n-1]
temp=st[a][n-1];
for(i=n-1;i>=1;i--)
st[a][i]=( st[a][i-1]+1 )%n;
st[a][0]=(temp+1)%n;
}

int main()
{
int n,count=1;
//最外这个循环是test循环
cin>>n;
while(n!=0)
{
cout<<"Enigma "<<count<<":"<<endl;
count++;
int crypnum;
insert(n);
cin>>crypnum;
//此循环是对同一test的不同数据
while(crypnum--)
{
int i,j,k,temp,len;
char t;
bool flags;
cin>>s;
len=strlen(s);

//利用三重for循环来方便对转子何时移位做判断
//flags记录当s[]每一位字符码都译好以后,退出循环
flags=1;
for(i=0;i<len;i++)
{
if(flags==0)
break;
for(j=0;j<n;j++)
{
if(flags==0)
break;
for(k=0;k<n;k++)
{
if(s[i*n*n+j*n+k]=='\0')
{
flags=0;
break;
}
temp=s[i*n*n+j*n+k]-'A';
//输出st的经过几个转子以后的原码
//字符要借助t才能输出
t=st[2][ st[1][ st[0][temp] ] ]+'a';
cout<<t;
rotor(2,n); //转子2移一位
}
rotor(1,n); //转子1移一位
}
rotor(0,n); //转子0移一位
}
cout<<endl;
//转子复原
for(i=0;i<3;i++)
for(j=0;j<n;j++)
st[i][j]=sttemp[i][j];

}

cin>>n; //为了输出时能在test之间空行
if(n!=0)
cout<<endl;

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: