题解

1 条题解

  • 1
    @ 2024-06-20 18:27:33
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    void encrypt(char *s, char *result, char *key); // encryption
    void decrypt(char *s, char *result, char *key); // decryption
    
    int main() {
        ios::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0); // Close the synchronized stream
        int t;
        cin >> t;
        while (t--) {
            char s[100] = ""; // plaintext or ciphertext
            char key[100] = ""; // key
            char result[100] = ""; // store the result after encryption or decryption
            bool op;
            cin >> s >> key >> op; // input
            if (!op) { // encrypt
                encrypt(s, result, key);
                cout << result << '\n'; // output
            } else { // decrypt
                decrypt(s, result, key);
                cout << result << '\n'; // output
            }
        }
        return 0;
    }
    
    void encrypt(char *s, char *result, char *key) { // encryption
        int i, j = 0, z = 0;
        for (i = 0; i < (int)strlen(s); i++) {
            if (s[i] >= 'a' && s[i] <= 'z') result[z++] = (s[i] - 'a' + key[j++] - 'a') % 26 + 'a';
            // lowercase letters
            else result[z++] = (s[i] - 'A' + key[j++] - 'a') % 26 + 'A';
            // uppercase letters
            if (j >= (int)strlen(key)) j = 0; // loop through the key
        }
    }
    
    void decrypt(char *s, char *result, char *key) { // decryption
        int i, j = 0, z = 0;
        for (i = 0; i < (int)strlen(s); i++) {
            if (s[i] >= 'a' && s[i] <= 'z') result[z++] = (s[i] - key[j++] + 26) % 26 + 'a';
            // lowercase letters
            else result[z++] = (s[i] - key[j++] + 58) % 26 + 'A';
            // uppercase letters
            if (j >= (int)strlen(key)) j = 0; // loop over the key
        }
    }
    
  • 1

信息

ID
1010
难度
(无)
分类
模拟 | 字符串 点击显示
标签
(无)
递交数
0
已通过
0
通过率
?
上传者