保留最大数

题目描述

给定一个十进制的正整数number,选择从里面去掉一部分数字,希望保留下来的数字组成的正整数最大。

输入描述:

输入为两行内容,第一行是正整数number,1 ≤ length(number) ≤ 50000。第二行是希望去掉的数字数量cnt 1 ≤ cnt < length(number)。

输出描述:

输出保留下来的结果。
示例1

输入

复制
325 1

输出

复制
35

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string a;
    int count;
    cin>>a>>count;
    int temp;

    for(int i=0;i<count;i++)
    {
       int length=a.length();//每次重新测量长度
       for(temp=0;temp<length-1;temp++)//每次都从零查找,貌似有点笨拙,但是好困啊,不写了狗命要紧。。。。。。。
       {
           if(a[temp]<a[temp+1])
           {
               a.erase(a.begin()+temp);//抹去这个元素,后面的会自动补齐。。。
               break;
           }
       }
       if(temp==length-1)
            a.erase(a.end()-1);//如果查找到倒数第二个都没有符合的,说明是个有序数列,不需要查找了,后续每次删最后一个就行了,有待简化。。。。。
    }
    cout<<a<<endl;
    return 0;
}



猜你喜欢

转载自blog.csdn.net/zhangao230/article/details/80753786