How to get the least number after deleting k digits from the input number

前端 未结 11 1205
灰色年华
灰色年华 2020-12-11 05:34

For example, if the input number is 24635, the least number is 23 after deleting any 3 digits.

It\'s not the same as taking the two smalles

11条回答
  •  -上瘾入骨i
    2020-12-11 05:49

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #define mod 1000000007
    #define ll long long
    using namespace std;
    bool Compare (pair p1, pair p2)  {
        if (p1.first < p2.first) {
            return true;
        }
        return false;
    }
    int main() {
        priority_queue , vector  >, function, pair ) > > pq (Compare);
        int n, k;
        cin>>n>>k;
        int i = 1;
        while (n) {
            pq.push(make_pair(n%10, i));
            n = n/10;
            i++;
        }
        for(int j =1; j<=k;j++){
            pq.pop();
        }
        int number = pq.top().first;
        int index = pq.top().second;
        int digit = 1;
        pq.pop();
        while(!pq.empty()){
            int current_index = pq.top().second;
            digit = digit * 10;
            if(current_index < index) {
                number = number * 10 + pq.top().first;
            } else {
                number = digit * pq.top().first + number;
            }
            pq.pop();
        }
        cout<

    I solved the question using priority_queue with pair. please let me know if there is any better solution

提交回复
热议问题