AcWing 502. Vigenère 密码 简洁易懂
原题链接
简单
作者:
偷月亮的喵
,
2025-01-15 23:48:12
,
所有人可见
,
阅读 1
#include <iostream>
#include <cstring>
using namespace std;
const int N = 1010;
string s1, s2, s;
int main()
{
cin >> s1 >> s2;//求的是明钥,明钥并且与密钥大小写一致
int m = s1.size(), n = s2.size();
for (int i = 0, j = 0; i < n; i++)
{
if (j == m)
j = 0;
if ((s1[j] - 'a' >= 0) && s2[i] - 'a' >= 0) //小写的密文,小写的密钥
{
char c = s2[i] + 'a' - s1[j];
if(c < 'a') c += 26;
cout << c;
}
else if ((s1[j] - 'a' < 0) && s2[i] - 'a' < 0)//大写的密文 ,大写的密钥
{
char c = s2[i] + 'A' - s1[j];
if(c < 'A') c += 26;
cout << c;
}
else if ((s1[j] - 'a' < 0) && s2[i] - 'a' >= 0)//小写的密文 ,大写的密钥
{
char c = s2[i] + 'A' - s1[j];
if(c < 'a') c += 26;
cout << c; }
else if ((s1[j] - 'a' >= 0) && s2[i] - 'a' < 0)//大写的密文 ,小写的密钥
{
char c = s2[i] + 'a' - s1[j];
if(c < 'A') c += 26;
cout << c;
}
j++;
}
}