• Home
  • About
    • on Weekend photo

      on Weekend

      ๐™Ž๐™ฉ๐™ช๐™™๐™ฎ๐™ž๐™ฃ๐™œ

    • Learn More
    • Instagram
    • Github
  • Archive
    • All Posts
    • All Tags
    • All Categories
  • Categories
    • Problem Solving
    • TIL
    • Study
    • Etc
    • ํ•„์‚ฌ
  • Projects

[๋ฐฑ์ค€] 11655 (C++)

16 Jan 2020

๋ฌธ์ œ๋Š” ์—ฌ๊ธฐ

์„ค๋ช…

C ๊ธฐ๋ณธ๊ธฐ๋ฅผ ์žŠ์–ด๊ฐ€๋Š” ๊ฒƒ ๊ฐ™์•„์„œ ์‚ฌ์†Œํ•œ ์‹ค์ˆ˜๋„ ์ ์–ด๋‘๊ณ  ์ข…์ข… ๋“ค์—ฌ๋‹ค๋ณด๋Š” ๊ฒƒ์ด ์ข‹์„ ๊ฒƒ ๊ฐ™๋‹ค.

int n = int(s[i]) ๋งŒ์œผ๋กœ๋„ ์ถฉ๋ถ„ํ•œ๋ฐ, ์Šต๊ด€์ ์œผ๋กœ atoi()๋ฅผ ์‚ฌ์šฉํ–ˆ๊ณ  ์ด๋กœ ์ธํ•ด const char* type error๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๋‹ค.

์ฆ‰ char*์ด ์•„๋‹˜์—๋„, atoi() ํ•จ์ˆ˜๋กœ ํ˜•๋ณ€ํ™˜์„ ์‹œ๋„ํ•˜์—ฌ์„œ ๋ฐœ์ƒํ•œ ์—๋Ÿฌ์ด๋‹ค.

โ€‹ ๋งŒ์•ฝ s[i]๊ฐ€ c++ string type์ธ ๊ฒฝ์šฐ์—๋Š” atoi()๋ฅผ ํ•  ๋•Œ, atoi(s[i].c_str());๋ฅผ ํ•ด์ฃผ๋ฉด ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜์ง€ ์•Š๋Š”๋‹ค.

๊ทธ๋Ÿฌ๋‚˜ ์ด ๊ฒฝ์šฐ์—๋Š” s[i]๋Š” ๋‹จ์ˆœํ•œ char type์ด๊ธฐ ๋•Œ๋ฌธ์— ํ˜•๋ณ€ํ™˜ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค. ๋‹น์—ฐํ•˜๊ฒŒ๋„ ๋‹จ์ˆœํžˆ int n = s[i];๋ฅผ ํ•ด์ฃผ๊ฑฐ๋‚˜ int n = int(s[i]);๋ฅผ ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.


[์‚ฌ์šฉ ํ™˜๊ฒฝ]

์•„์ดํŒจ๋“œ, C/C++ Compiler ์•ฑ ์‚ฌ์šฉ.

โ€‹ ์‚ฌ์‹ค์ƒ ํ•ด๋‹น ๋ฌธ์ œ์—์„œ ์š”๊ตฌํ•˜๋Š” ๋ฐ”๋Š” ์›ํ˜• ํ์— a~z, A~Z๋ฅผ ๋„ฃ์–ด๋‘๊ณ  +13์„ ํ•˜๋ฉฐ ์›ํ˜•์œผ๋กœ ์ ‘๊ทผํ•˜๋Š” ๊ฒƒ์ด๋‚˜, ๋ณด๋‹ค ์ต์ˆ™ํ•œ ์•„์Šคํ‚ค์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค.


์ฝ”๋“œ
#include <iostream>
#include <stdlib.h>
#include <string>
#include <cstring>
#include <deque>

using namespace std;

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  string s;
  getline(cin,s);
  for(int i=0;i<s.size();i++)
  {
    if((s[i] >= 'a') && (s[i]<='z'))
    {
      int n = s[i];
      if(n+13>'z') n='a'+(n+12-'z');
      else n+=13;
      cout << char(n);
    }
    else if((s[i] >= 'A') && (s[i]<='Z'))
    {
      int n = s[i];
      if(n+13>'Z') n = 'A'+(n+12-'Z');
      else n+=13;
      cout << char(n);
    }
    else cout << s[i];
  }
  return 0;
}


problem_solvingc++ Share Tweet +1