在一个数中去掉指定个数的数字使其结果最小

Description
Given you one n-digital positive integer a,After removing any of them k( k < n) digits, the remaining figures form a new positive integer according to the origin order. For a given n-digital positive integers a and positive integer k. Now ask you to find a algorithm to minimize the remaining integer.
Input
The first line contains one integer t, represents the number of test cases.
Then following t lines,each line contain two numbers a,k (0 < a < 10^1000, k is less than the length of a).

Output
Output the mininum number after the deletion.(the number can not contain leading zero)”> it means that we should ignore the leading zeros of the output number
Sample Input
1
178543 4
Sample Output
13

思路:
从前往后优先将高位上大于的低位上数字的高位去掉,并且去掉一个就要重头开始搜,然后从后往前将末尾的数字删除

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        char ss[1009];
        int s[1009];
        scanf("%s",ss);
        int len=strlen(ss);
        for(int i=0; i<len; i++)
            s[i]=ss[i]-'0';
        int k;
        cin>>k;
        while(k)
        {
            int flag=0;
            for(int i=0; i<len-1; i++)
            {
                if(s[i]!=-1)
                {
                    int j=i+1;
                    while(j<len&&s[j]==-1)//寻找删除数字后与其相邻的数字
                        j++;
                    if(s[i]>s[j])
                    {
                        s[i]=-1,k--,flag=1;
                        break;
                    }
                }
            }
            if(flag==0)
                break;
        }
        if(k)
        {
            for(int i=len-1; i>=0; i--)
            {
                if(s[i]!=-1)
                    s[i]=-1,k--;
                if(k==0)
                    break;
            }
        }
        int fla=0;
        int la=0;
        for(int kk=0; kk<len; kk++)
        {
            if(s[kk]!=-1&&s[kk]!=0)
                fla=1;
            if(s[kk]==0)
                la=1;
            if(fla&&s[kk]!=-1)
                printf("%d",s[kk]);
        }
        if(fla==0&&la==1)
            printf("0");
        printf("\n");

    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值