第四次机考C.古代密码
#include <iostream>
#include <string.h>
#include <string>
#include <ctime>
using namespace std;
void bubble_sort(string &a, int len) { //冒泡排序 排序交换一定要记得按地址传递!!!
int pos = len - 1;
int bound;
while (pos) {
bound = pos;
pos = 0;
for (int i = 0; i < bound; i++)
{
if (a[i] > a[i + 1])
{
char tmp = a[i];
a[i] = a[i + 1];
a[i + 1] = tmp;
pos = i;
}
}
}
}
int main()
{
string a;
string b;
cin >> a >> b;
bool flag2 = 0;//判断是否输出NO
for (int i = 1; i < 26; i++)
{
int cnt = 0;
string c = b;
//因为本题的第一种加密方法不一定是往后加一位,平移1-25位都有可能,所以用循环试试
for (int j = 0; j < b.length(); j++)
{
if (c[j]+i<='Z')
{
c[j] += i;
}
else
{
c[j] = 'A' + (c[j] + i - 'Z'-1);
}
}
//按字典序重构a,b
bubble_sort(a, a.length());
bubble_sort(c, c.length());
bool result = a == c;
//string类可以直接用大于号小于号等于号比较大小 又是一个很强的功能
if (result) { flag2=1; cout << "YES"; break; }
else
{
cnt++;
if (cnt == b.length()) { flag2 = 0; }
}
}
if (!flag2)
{
cout << "NO";
}
return 0;
}