版本比较
加密器
正文
增加 27 行删除 27 行
23 行未改动24252627282829303131323334353637373839404142424344454647484950505152535354555657585959606162636464656667686970717172737475767777787980818283848585868788893 行未改动93949596979798991001011021021031041051065 行未改动11211311411511611611711811912018 行未改动1391401411421431431441451461471481491501511511521531541551551561571581591601611621631641641651661661671681691691701711721733 行未改动17717817918018118118218318418518618718818919019019119219319410 行未改动20520620720820920921021121221321421521621721821921922022122222322422522622722722822923023123223223323423523623723723823924023 行未改动
// 大数乘法:num * 36string multiplyBy36(const string& num) { if (num == "0") return "0";
string result; int carry = 0;
for (int i = num.size() - 1; i >= 0; i--) { int digit = (num[i] - '0') * 36 + carry; result.push_back(digit % 10 + '0'); carry = digit / 10; }
while (carry > 0) { result.push_back(carry % 10 + '0'); carry /= 10; }
reverse(result.begin(), result.end()); return result;}
// 大数加法:num + valuestring addValue(const string& num, int value) { if (num.empty()) return to_string(value);
string result = num; int carry = value;
for (int i = result.size() - 1; i >= 0 && carry > 0; i--) { int sum = (result[i] - '0') + carry; result[i] = sum % 10 + '0'; carry = sum / 10; }
while (carry > 0) { result.insert(result.begin(), carry % 10 + '0'); carry /= 10; }
return result;}
// 转换36进制数字部分string convertBase36Segment(const string& segment) { string decimal = "0";
for (size_t i = 0; i < segment.size(); i++) { decimal = multiplyBy36(decimal); int value = charToValue(segment[i]); decimal = addValue(decimal, value); }
return decimal;}
// 处理完整字符串,保留非字母数字字符string processMixedString(const string& input) { string result; string currentSegment;
for (char c : input) { if (isBase36Char(c)) { currentSegment += c; } else {3 行未改动 } result += c; } }
// 处理末尾的36进制段 if (!currentSegment.empty()) { result += convertBase36Segment(currentSegment); }
return result;}
int main() {5 行未改动 } catch (const invalid_argument& e) { cerr << "错误: " << e.what() << endl; return 1; }
return 0;}```破密器:18 行未改动// 大数除法,返回商和余数pair<string, int> divideBy36(const string& num) { string quotient; int remainder = 0;
for (string::const_iterator it = num.begin(); it != num.end(); ++it) { int current = remainder * 10 + (*it - '0'); if (!quotient.empty() || current >= 36) { quotient.push_back(current / 36 + '0'); } remainder = current % 36; }
if (quotient.empty()) { quotient = "0"; }
return make_pair(quotient, remainder);}
// 十进制字符串转三十六进制字符串string decimalToBase36(const string& decimal) { if (!isValidDecimal(decimal)) { throw invalid_argument("无效的十进制输入"); }
if (decimal == "0") return "0";
string num = decimal; string result;
while (num != "0") { pair<string, int> division = divideBy36(num); char c; if (division.second < 10) {3 行未改动 } result.push_back(c); num = division.first; }
reverse(result.begin(), result.end()); return result;}
// 处理混合字符串,保留非数字字母字符string processMixedInput(const string& input) { string result; string currentNumber;
for (string::const_iterator it = input.begin(); it != input.end(); ++it) { if (isdigit(*it)) { currentNumber += *it; } else {10 行未改动 // 添加非数字字符 result += *it; } }
// 处理末尾的数字 if (!currentNumber.empty()) { try { result += decimalToBase36(currentNumber); } catch (const invalid_argument& e) { cerr << "错误: " << e.what() << endl; return ""; } }
return result;}
int main() { try { string ciphertext; getline(cin, ciphertext);
string plaintext = processMixedInput(ciphertext); if (!plaintext.empty()) { cout << "明文: " << plaintext << endl; }
} catch (const exception& e) { cerr << "错误: " << e.what() << endl; return 1; }
return 0;}```