2208:编码解码。
Theres no place like home on a snowy night
t o i o y
h p k n n
e l e a i
r a h s g
e c o n h
s e m o t
n l e w x
竖排列成n列方阵,用x补足。从头开始蛇形输出,完成加密。
现给出密码,解密。
SampleInput
5
toioynnkpheleaigshareconhtomesnlewx
3
ttyohhieneesiaabss
0
SampleOutput
theresnoplacelikehomeonasnowynightx
thisistheeasyoneab
思路:按块处理。奇数组的块和偶数组的块顺序有规律。对于补足的x单独处理。
Theres no place like home on a snowy night
t o i o y
h p k n n
e l e a i
r a h s g
e c o n h
s e m o t
n l e w x
竖排列成n列方阵,用x补足。从头开始蛇形输出,完成加密。
现给出密码,解密。
SampleInput
5
toioynnkpheleaigshareconhtomesnlewx
3
ttyohhieneesiaabss
0
SampleOutput
theresnoplacelikehomeonasnowynightx
thisistheeasyoneab
思路:按块处理。奇数组的块和偶数组的块顺序有规律。对于补足的x单独处理。
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int main()
{
char encrypt[201];
int n;
int len;
int num;
int pad;
while(1)
{
cin>>n;
pad=0;
if(n==0)
break;
cin>>encrypt;
len=strlen(encrypt);
num=len/n;
for(int i=len-1;i>0;i=i-n)
{
if(encrypt[i]=='x')
pad++;
else
break;
}
//每块个数
for(int i=0;i<n;i++)
{
//分块个数
for(int j=0;j<num;j++)
{
//
if(!(i==n-1&&j>num-1-pad))
{
if(j%2==0)
cout<<encrypt[n*j+i];
else
cout<<encrypt[n*j+n-1-i];
}
}
}
for(int i=0;i<pad;i++)
cout<<'x';
cout<<endl;
}
}