Luogu brush questions C++ language | P1914 Little Book Boy - Caesar Cipher

Learn C++ from a baby! Record the questions in the process of Luogu C++ learning and test preparation, and record every moment.

Attached is a summary post: Luogu Brush Questions C++ Language | Summary


【Description】

Although Konjac has forgotten the password, he still remembers that the password consists of a character string.  The password is formed by moving each letter of the original text string (consisting of no more than 50 lowercase letters) backward by  n bits. The next letter of z is a, and so on. Now he has found the original character string and  n before the move , please ask for the password.

【enter】

First line: n . Second row: A string of letters before moving.

【Output】

One line is the password of this konjac.

【Input example】

1 qwe

【Example of output】

rxf

【Code Explanation】

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    string s;
    cin >> n >> s;
    for (int i=0; i<s.length(); i++) {
        if (s[i]+n<='z') {
            s[i] += n;
        } else {
            s[i] = s[i] + (n-26)%26;
        }
    }
    cout << s;
    return 0;
}

【operation result】

1
qwe
rxf

Guess you like

Origin blog.csdn.net/guolianggsta/article/details/132688901