# 1253 - Cifra de César

## Descrição

{% embed url="<https://www.urionlinejudge.com.br/judge/pt/problems/view/1253>" %}

## Solução

Basta fazer um for deslocando cada letra as posições para **esquerda** (lembre-se que a descrição diz como que criptografa, aqui a gente quer descriptografar). Daí, só utilizar o seguinte artifício para deslocar uma letra `x` para esquerda dentro do intervalo `[0,25]`, ou seja, no intervalo de `'A'` a `'Z'`:

```
Esquerda(x) = ((ASCII(x) - ASCII('A') - deslocamento + 26) % 26) + ASCII('A')
```

Dê uma olhada nas páginas [Tabela ASCII](/solucoes-da-beecrowd/base-teorica/strings/tabela-ascii.md) e [Deslocar dentro de um intervalo \[0, N - 1\]](/solucoes-da-beecrowd/base-teorica/matematica/macetes-matematicos.md#deslocar-dentro-de-um-intervalo-0-n-1) para entender por que essa operação acima funciona. Ao final teremos um número no intervalo `[0,25]` onde basta adicionarmos novamente o código ASCII da letra A para descobrirmos a letra resultante do deslocamento.

{% tabs %}
{% tab title="C99" %}

```c
#include <string.h>
#include <stdio.h>

int main(){
    char alfabeto[51];
    int N, deslocamento;

    scanf("%d\n", &N);

    for(int i = 0; i < N; ++i){
        scanf("%[^\n]\n", &alfabeto);
        scanf("%d\n", &deslocamento);

        for(int j = 0; j < strlen(alfabeto); ++j){
            alfabeto[j] = ((alfabeto[j] - 'A' - deslocamento + 26) % 26) + 'A';
        }

        printf("%s\n", alfabeto);
    }

    return 0;
}
```

{% endtab %}

{% tab title="C++17" %}

```cpp
#include <iostream>

using namespace std;

int main(){
    string alfabeto;
    int N, deslocamento;

    cin >> N;

    for(int i = 0; i < N; ++i){
        cin >> alfabeto >> deslocamento;

        for(int j = 0; j < alfabeto.length(); ++j){
            alfabeto[j] = ((alfabeto[j] - 'A' - deslocamento + 26) % 26) + 'A';
        }

        cout << alfabeto << endl;
    }

    return 0;
}
```

{% endtab %}

{% tab title="JavaScript 12.18" %}

```javascript
var input = require('fs').readFileSync('/dev/stdin', 'utf8');
var lines = input.split('\n');

let N = parseInt(lines.shift());

for(let i = 0; i < N; ++i){
    let alfabeto = lines.shift();
    let deslocamento = parseInt(lines.shift());

    let resposta = alfabeto.trim().split('').map((x) => String.fromCharCode(((x.charCodeAt(0) - 65 - deslocamento + 26) % 26) + 65)).join('');

    console.log(resposta);
}
```

{% endtab %}

{% tab title="Python 3.9" %}

```python
N = int(input())

for _ in range(N):
    alfabeto = input()
    deslocamento = int(input())

    resposta = ''.join([chr(((ord(letra) - 65 - deslocamento + 26) % 26) + 65) for letra in alfabeto])

    print(resposta)
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://xtecna.gitbook.io/solucoes-da-beecrowd/strings/1253-cifra-de-cesar.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
