[ITSA] ITSA 50 Problem 1 - 字母往前移

題目

Link: 智?題

Code

C++

#include <string>
#include <iostream>

int main()
{
  std::string content;
  int key;

  std::ios::sync_with_stdio(false);

  std::getline(std::cin, content);
  std::cin >> key;

  for (auto& ch : content)
  {
    if (ch >= 'A' && ch <= 'Z')
    {
      ch += key % 26;
      if (ch > 'Z') ch -= 26;
    }
    else if (ch >= 'a' && ch <= 'z')
    {
      ch += key % 26;
      if (ch > 'z' || ch < 0) ch -= 26;
    }
    else if (ch >= '0' && ch <= '9')
    {
      ch += key % 10;
      if (ch > '9') ch -= 10;
    }
  }

  std::cout << content << std::endl;

  return 0;
}

Show Comments