南阳理工学院个人积分赛第四场——J - Min Number【条理清晰】

Problem 2111 Min Number

Accept: 747    Submit: 1500
Time Limit: 1000 mSec    Memory Limit : 32768 KB

 Problem Description

Now you are given one non-negative integer n in 10-base notation, it will only contain digits ('0'-'9'). You are allowed to choose 2 integers i and j, such that: i!=j, 1≤i<j≤|n|, here |n| means the length of n’s 10-base notation. Then we can swap n[i] and n[j].

For example, n=9012, we choose i=1, j=3, then we swap n[1] and n[3], then we get 1092, which is smaller than the original n.

Now you are allowed to operate at most M times, so what is the smallest number you can get after the operation(s)?

Please note that in this problem, leading zero is not allowed!

 Input

The first line of the input contains an integer T (T≤100), indicating the number of test cases.

Then T cases, for any case, only 2 integers n and M (0≤n<10^1000, 0≤M≤100) in a single line.

 Output

For each test case, output the minimum number we can get after no more than M operations.

 Sample Input

39012 09012 19012 2

 Sample Output

901210921029

 Source

“高教社杯”第三届福建省大学生程序设计竞赛


对就是一道简单到暴力的题目,可是因为前面卡在一道题上浪费了太多时间,这个没多少时间做,其实还是有时间做的,可是因为前面卡题了,后面这道居然反了低级错误,知道结束也没调试出来,多写题目啊,代码水平才能上

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
char ma[1010];
int main()
{int t,n,i,j,len,m,mj;
char b;
scanf("%d",&t);
while(t--)
{
    scanf("%s%d",ma,&n);
    len=strlen(ma);
    for(i=0;i<n&&i<len;i++)//交换次数
    {m=ma[i]-48;
       mj=i;
        for(j=i;j<len;j++)//找到第i位之后的最小值
        {
            if(i==0&&ma[j]-48<m&&ma[j]-48>0)//首位不能为0
            {
                m=ma[j]-48;
                mj=j;
            }
            else if(i>0&&ma[j]-48<m)//其他位
            {
                m=ma[j]-48;
                mj=j;
            }

        }
        if(mj==i)n++;
        else  swap(ma[i],ma[mj]);

        


    }
    printf("%s\n",ma);
}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值