BOJ 9086번 문자열 문제

string을 입력받아서 맨 앞자리와 맨 끝자리를 출력하면 된다.

끝자리 출력할 때 인덱스 부분 설정에만 주의하면 된다.

9086.cpp

#include <bits/stdc++.h>
using namespace std;
int main() {
    string s;
    int n;
    cin >> n;
    while (n--) {
        cin >> s;
        int end = s.size();
        cout << s[0] << s[end - 1] << '\n';
    }
}

'알고리즘 & SQL > 백준(BOJ)' 카테고리의 다른 글

백준 16360번 : Go Latin C++  (0) 2018.11.04
백준 14753번 : MultiMax C++  (0) 2018.11.02
백준 1427번 : 소트인사이드 C++  (0) 2018.11.02
백준 2851번 : 슈퍼 마리오 C++  (1) 2018.11.02
백준 2998번 : 8진수 C++  (0) 2018.10.31

BOJ 1427번 소트인사이드 문제

sort를 사용해서 쉽게 풀 수 있는 문제였다.

그냥 쓸 줄만 알면 풀수 있다.

1427.cpp

#include <bits/stdc++.h>
using namespace std;
int main() {
    string s;
    cin >> s;
    sort(s.begin(), s.end());
    reverse(s.begin(), s.end());
    cout << s;
}

'알고리즘 & SQL > 백준(BOJ)' 카테고리의 다른 글

백준 14753번 : MultiMax C++  (0) 2018.11.02
백준 9086번 : 문자열 C++  (0) 2018.11.02
백준 2851번 : 슈퍼 마리오 C++  (1) 2018.11.02
백준 2998번 : 8진수 C++  (0) 2018.10.31
백준 10987번 : 단위 C++  (0) 2018.10.30

Visual Studio Code에서 <bits/stdc++.h> 헤더 파일을 사용하는 방법

  • MinGW를 설치했을경우

    • C:\MinGW\lib\gcc\mingw32\6.3.0\include\c++\mingw32\bits 폴더를 C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\include에 복사하면 쓸 수 있다.
    • 위 경로는 사용자에 따라 달라질 수 있으니 참고만 하자.
  • MinGW를 설치하지 않았을 경우

    • C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.15.26726\include에 들어가서 bits 폴더를 생성하고, stdc++.h 파일을 만든다.

    • 아래를 복사해서 입력한다.

      // C++ includes used for precompiling -*- C++ -*-
      
      // Copyright (C) 2003-2013 Free Software Foundation, Inc.
      //
      // This file is part of the GNU ISO C++ Library.  This library is free
      // software; you can redistribute it and/or modify it under the
      // terms of the GNU General Public License as published by the
      // Free Software Foundation; either version 3, or (at your option)
      // any later version.
      
      // This library is distributed in the hope that it will be useful,
      // but WITHOUT ANY WARRANTY; without even the implied warranty of
      // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      // GNU General Public License for more details.
      
      // Under Section 7 of GPL version 3, you are granted additional
      // permissions described in the GCC Runtime Library Exception, version
      // 3.1, as published by the Free Software Foundation.
      
      // You should have received a copy of the GNU General Public License and
      // a copy of the GCC Runtime Library Exception along with this program;
      // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
      // <http://www.gnu.org/licenses/>.
      
      /** @file stdc++.h
      *  This is an implementation file for a precompiled header.
      */
      
      // 17.4.1.2 Headers
      
      // C
      #ifndef _GLIBCXX_NO_ASSERT
      #include <cassert>
      #endif
      #include <cctype>
      #include <cerrno>
      #include <cfloat>
      #include <ciso646>
      #include <climits>
      #include <clocale>
      #include <cmath>
      #include <csetjmp>
      #include <csignal>
      #include <cstdarg>
      #include <cstddef>
      #include <cstdio>
      #include <cstdlib>
      #include <cstring>
      #include <ctime>
      
      #if __cplusplus >= 201103L
      #include <ccomplex>
      #include <cfenv>
      #include <cinttypes>
      #include <cstdalign>
      #include <cstdbool>
      #include <cstdint>
      #include <ctgmath>
      #include <cwchar>
      #include <cwctype>
      #endif
      
      // C++
      #include <algorithm>
      #include <bitset>
      #include <complex>
      #include <deque>
      #include <exception>
      #include <fstream>
      #include <functional>
      #include <iomanip>
      #include <ios>
      #include <iosfwd>
      #include <iostream>
      #include <istream>
      #include <iterator>
      #include <limits>
      #include <list>
      #include <locale>
      #include <map>
      #include <memory>
      #include <new>
      #include <numeric>
      #include <ostream>
      #include <queue>
      #include <set>
      #include <sstream>
      #include <stack>
      #include <stdexcept>
      #include <streambuf>
      #include <string>
      #include <typeinfo>
      #include <utility>
      #include <valarray>
      #include <vector>
      
      #if __cplusplus >= 201103L
      #include <array>
      #include <atomic>
      #include <chrono>
      #include <condition_variable>
      #include <forward_list>
      #include <future>
      #include <initializer_list>
      #include <mutex>
      #include <random>
      #include <ratio>
      #include <regex>
      #include <scoped_allocator>
      #include <system_error>
      #include <thread>
      #include <tuple>
      #include <typeindex>
      #include <type_traits>
      #include <unordered_map>
      #include <unordered_set>
      #endif

BOJ 2851번 슈퍼마리오 문제

특별한 알고리즘이 필요한게 아닌 구현 문제이다.

첫번째 버섯부터 무조건 먹는건데 중간에 왜 갑자기 생각을 잘못했는지 중간부터 먹을 수 있게 구현해서 계속 틀려버렸다.

일단 배열에 10개 입력을 받는다.

하나 하나 더해가면서, 100이 넘는 경우에 100에 더하기 전이 가까운지 더한 후가 가까운지 조건을 주고 더 작은 값을 출력한다. 만약 같다면 더한 숫자를 골라야한다.

2851.cpp

#include <iostream>
using namespace std;
int main() {
    int a[10];
    for (int i = 0; i < 10; i++) {
        cin >> a[i];
    }
    int sum = 0;
    for (int i = 0; i < 10; i++) {
        int afterSum = sum + a[i];
        if (afterSum >= 100) {
            if (afterSum - 100 <= 100 - sum) {
                cout << afterSum;
            } else {
                cout << sum;
            }
            return 0;
        }
        sum = afterSum;
    }
    cout << sum;
}

'알고리즘 & SQL > 백준(BOJ)' 카테고리의 다른 글

백준 14753번 : MultiMax C++  (0) 2018.11.02
백준 9086번 : 문자열 C++  (0) 2018.11.02
백준 1427번 : 소트인사이드 C++  (0) 2018.11.02
백준 2998번 : 8진수 C++  (0) 2018.10.31
백준 10987번 : 단위 C++  (0) 2018.10.30

Visual Studio Code에서 CodeRunner와 MinGW를 이용해서 C,C++ 컴파일, 디버깅하기 on Windows

  • VScode extension에서 CodeRunner를 설치합니다.

CodeRunner 설치

  • 설치를 한 후에 다시 로드를 눌러서 vscode를 껐다가 켜줍니다.

  • Ctrl + ,를 눌러 설정에 들어가고, 우측 메뉴바에서 setting.json을 열어줍니다.
    Image

  • 우측을 통해 설정을 덮어 쓸수 있는데, 다음을 입력합니다.

    • C와 CPP만 다룬다면 밑에 "c", "cpp" 부분만 입력해도 됩니다.

    • 맨 밑줄을 입력해야 scanf, cin과 같이 입력을 할 수 있습니다.

      "code-runner.executorMap": {
        "javascript": "node",
        "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
        "c": "cd $dirWithoutTrailingSlash gcc $fileName -g -o $fileNameWithoutExt && ./$fileNameWithoutExt.exe",
      
        // 데이터값 바로 입력원할때.
        "cpp": "cd $dirWithoutTrailingSlash && g++ $fileName -g -o $fileNameWithoutExt && ./$fileNameWithoutExt.exe < d.dat",
      
        // 직접 입력할때
        //"cpp": "cd $dirWithoutTrailingSlash && g++ $fileName -g -o $fileNameWithoutExt && ./$fileNameWithoutExt.exe",
        "objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "php": "php",
        "python": "python",
        "perl": "perl",
        "perl6": "perl6",
        "ruby": "ruby",
        "go": "go run",
        "lua": "lua",
        "groovy": "groovy",
        "powershell": "powershell -ExecutionPolicy ByPass -File",
        "bat": "cmd /c",
        "shellscript": "bash",
        "fsharp": "fsi",
        "csharp": "scriptcs",
        "vbscript": "cscript //Nologo",
        "typescript": "ts-node",
        "coffeescript": "coffee",
        "scala": "scala",
        "swift": "swift",
        "julia": "julia",
        "crystal": "crystal",
        "ocaml": "ocaml",
        "r": "Rscript",
        "applescript": "osascript",
        "clojure": "lein exec",
        "haxe": "haxe --cwd $dirWithoutTrailingSlash --run $fileNameWithoutExt",
        "rust": "cd $dir && rustc $fileName && $dir$fileNameWithoutExt",
        "racket": "racket",
        "ahk": "autohotkey",
        "autoit": "autoit3",
        "dart": "dart",
        "pascal": "cd $dir && fpc $fileName && $dir$fileNameWithoutExt",
        "d": "cd $dir && dmd $fileName && $dir$fileNameWithoutExt",
        "haskell": "runhaskell",
        "nim": "nim compile --verbosity:0 --hints:off --run"
      },
      "code-runner.runInTerminal": true,

      Image

  • 일단 CodeRunner 설정이 끝났고, 테스트를 합니다.

    • 테스트할 코드를 적고, 우클릭-Run Code(혹은 Ctrl+Alt+N)를 하면 됩니다.

      Image))png)

    • 하단 혹은 우측에 터미널이 나오고, 아래와 같이 결과가 나옵니다. 입력값이 필요한 코드였으면 입력을 할 수 있습니다.

      Image

Debugging 방법

  • MinGW가 이미 설치 되어있다고 가정하고 진행하겠습니다.

    • MinGW 설치는 다음에 기회가 되면 포스팅하겠습니다.
  • Vscode 좌측 메뉴에 디버그를 누릅니다.(Ctrl+Shfit+D)

    Image

  • 톱니바퀴 버튼을 누르고, 환경은 C++(GDB/LLDB)를 선택합니다.

  • launch.json 파일이 열리는데, 다음을 입력합니다.

      {
          "version": "0.2.0",
          "configurations": [
    
              {
                  "name": "(gdb) Launch",
                  "type": "cppdbg",
                  "request": "launch",
                  // 
                  // 이 밑 부분 파일경로, 파일명을 맞게 고쳐야합니다!
                  "program": "${workspaceFolder}/test.exe",
                  "args": [],
                  "stopAtEntry": false,
                  "cwd": "${workspaceFolder}",
                  "environment": [],
                  "externalConsole": true,
                  "MIMode": "gdb",
                  //
                  // MinGW 경로가 같은지 확인 !
                  "miDebuggerPath": "C:/MinGW/bin/gdb.exe",
                  "setupCommands": [
                      {
                          "description": "Enable pretty-printing for gdb",
                          "text": "-enable-pretty-printing",
                          "ignoreFailures": true
                      }
                  ]
    
              }
          ]
      }
  • 코드의 원하는 부분에 중단점을 넣고, F5를 눌러 디버깅을 진행합니다.

  • F11을 눌러 한단계씩 진행하며 디버깅을 한다.
    Image

이 방법에서 주의할 점은 디버깅을 하기 전에 한번 Run Code를 해줘야하는 것과 디버깅 경로를 그때 그때 맞게 정해야 한다는 것입니다. 혹시 더 좋은 방법이 있으면 알려주세요.

+ Recent posts