题解 | #字符串加密#
字符串加密
http://www.nowcoder.com/practice/e4af1fe682b54459b2a211df91a91cf3
#include<stdio.h>
#include<string.h>
//Autor:ding
int main()
{
char mima[30];
while(scanf("%s",&mima)!=EOF)
{
int zifu[26]={0};
char a[1000];//原文
scanf("%s",&a);
int len_a =strlen(a);//得到a的长度
int len_mima = strlen(mima);
for(int i = 0;i<len_mima;i++)
{
for(int j = i+1;j<len_mima;j++)
{
if(mima[j]==mima[i])
{
for(int k =j;k<len_mima-1;k++)
{
mima[k] = mima[k+1];
}
len_mima--;
j--;
}
}
}//得到符合条件的秘钥
char zhuanhuan[26];
for(int i = 0;i<len_mima;i++)
{
zhuanhuan[i] = mima[i];
if(zhuanhuan[i]-'a'>=0&&zhuanhuan[i]-'a'<=26)//统计字符类型
zifu[zhuanhuan[i]-'a']++;
else
zifu[zhuanhuan[i]-'A']=zifu[zhuanhuan[i]-'A']+2;
}
int next_len = len_mima;
for(int k =0;k<26;k++)
{
if(zifu[k]==0)
{
zhuanhuan[next_len] = 'a'+k;
next_len++;
}
else if(zifu[k]==1)
{
continue;
}
else if(zifu[k]==2)
{
continue;
}
}//得到最新的转换表
for(int i=0;i<len_a;i++)
{
if(a[i]>='a'&&a[i]<='z')
{
printf("%c",zhuanhuan[a[i]-'a']);
}
else
{
printf("%c",zhuanhuan[a[i]-'A']);
}
}
printf("\n");//输出加密后的密文
}//此程序只完成了小写字母加密。
}