ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 백준 10972 - 다음 순열
    백준 algorithm 2020. 6. 5. 22:12

    문제 설명

     

     

     

    => STL의 Algorithm 헤더에는 next_permutation과 prev_permutation 함수가 있습니다. (Library)

    => 해당 함수를 통해 구현할 수 있는 간단한 문제입니다.

    => 시간이 될 때 직접 구현해보는 방향으로 다시 풀어보겠습니다. 

     

     

     

    => 해당 함수는 vector든, 일반 배열이든 해당 배열의 처음과 끝 부분을 매개변수로 주면 그다음 함수가 있을 경우, true를 return 하고, 아닐 경우 false를 return 합니다. 

    => 또한 해당 배열을 다음 배열로 미리 바꾸어주는 동작을 합니다. 

     

     

    함수의 원형

     

     

     

     

     

    <code>

    #include <iostream>
    #include <algorithm>
    #include <string>
    #include <vector>
    
    
    using namespace std;
    
    
    int main()
    {
        ios_base::sync_with_stdio(false);
        cin.tie(nullptr);
        cout.tie(nullptr);
        
        vector<int> arr;
           int n;
           cin >> n;
           
           for(int i = 0; i < n; i++){
               int tmp;
               cin >> tmp;
               arr.push_back(tmp);
           }
           
           
        if(next_permutation(arr.begin(), arr.end()))
        {
            for(int i = 0; i < n; i++)
            {
                cout << arr[i]<<" ";
            }
        }
        else{
            cout << -1<<"\n";
        }
        
       
        return 0;
    }
    

     

     

     

     

     

    실행화면

     

     

    '백준 algorithm' 카테고리의 다른 글

    백준 2839 - 설탕 배달  (2) 2020.06.05
    백준 10973 - 이전 순열  (0) 2020.06.05
    백준 1316 - 그룹 단어 체커  (0) 2020.06.04
    백준 15651 - N과 M(3)  (0) 2020.06.03
    백준 15650 - N과 M(2)  (0) 2020.06.03

    댓글

Designed by Who.