#include<bits/stdc++.h>
using namespace std;
int main() {
string str;
string strA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string stra = "abcdefghijklmnopqrstuvwxyz";
getline(cin, str);
int i, j, k, length;
length = str.size();
int ary[length];
//string to number consistenly
for (i = 0; i < length; i++) {
for (j = 0; j < 26; j++) {
if (str[i] == strA[j] || str[i] == stra[j]) {
ary[i] = j;
break;
} else {
ary[i] = 100;
}
}
}
//decoding
for (k = 0; k < 26; k++) {
for (i = 0; i < length; i++) {
if (ary[i] != 100) {
int a = i;
int b = (i + 1) % length;
int c = (i + 2) % length;
int d = (i + 3) % length;
a = (ary[a] + k ) % 26;
b = (ary[b] + k ) % 26;
c = (ary[c] + k ) % 26;
d = (ary[d] + k ) % 26;
if (a == 11 && b == 14 && c == 21 && d == 4) {
cout << k;
k = 26;
break;
}
}
}
}
return 0;
}
另一個問題:t dm為輸入字串時,你的
a = (ary[a] + k ) % 26;
b = (ary[b] + k ) % 26;
c = (ary[c] + k ) % 26;
d = (ary[d] + k ) % 26;
if (a == 11 && b == 14 && c == 21 && d == 4)
剛好成立(因為你對ary[b] + k取餘數(mod 26)時,數字剛好==14),所以你的程式會神奇的輸出18,但這明顯不是答案。如果你把不是英文字母的值設成1000,可能就會對了(?
反正建議多做一個判斷 if (a == 11 && b == 14 && c == 21 && d == 4 && ary[b] != 100 && ary[c] != 100 && ary[d] != 100) // ary[a]就不用做了 因為前面判斷過
不然就賭測資太爛吧(X
另一個問題:t dm為輸入字串時,你的
a = (ary[a] + k ) % 26;
b = (ary[b] + k ) % 26;
c = (ary[c] + k ) % 26;
d = (ary[d] + k ) % 26;
if (a == 11 && b == 14 && c == 21 && d == 4)
剛好成立(因為你對ary[b] + k取餘數(mod 26)時,數字剛好==14),所以你的程式會神奇的輸出18,但這明顯不是答案。如果你把不是英文字母的值設成1000,可能就會對了(?
反正建議多做一個判斷 if (a == 11 && b == 14 && c == 21 && d == 4 && ary[b] != 100 && ary[c] != 100 && ary[d] != 100) // ary[a]就不用做了 因為前面判斷過
不然就賭測資太爛吧(X
#include<bits/stdc++.h>
using namespace std;
int main() {
srand(time(NULL));
string str;
string strA = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string stra = "abcdefghijklmnopqrstuvwxyz";
getline(cin, str);
int i, j, k, length;
length = str.size();
int ary[length];
//string to number consistenly
for (i = 0; i < length; i++) {
for (j = 0; j < 26; j++) {
if (str[i] == strA[j] || str[i] == stra[j]) {
ary[i] = j;
break;
} else {
ary[i] = rand()+27;
}
}
}
//decoding
for (k = 0; k < 26; k++) {
for (i = 0; i < length; i++) {
if (ary[i] < 27) {
int a = i;
int b = (i + 1) % length;
int c = (i + 2) % length;
int d = (i + 3) % length;
a = (ary[a] + k ) % 26;
b = (ary[b] + k ) % 26;
c = (ary[c] + k ) % 26;
d = (ary[d] + k ) % 26;
if (a == 11 && b == 14 && c == 21 && d == 4) {
cout << k;
k = 26;
break;
}
}
}
}
return 0;
}
剛剛送了,是AC的,對的概率80%吧(我猜),測資沒有剛剛說的第一種情況