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

3
9012 0
9012 1
 9012 2

Sample Output

9012
1092
1029
 
题意:给出一个n和交换次数k,求对这个数进行k次交换后得到的最小的数是什么
思路:对这个数两头进行枚举即可
 
#include <stdio.h>
#include <string.h> int main()
{
int n,i,j,len,l,MIN,flag,ss;
char str[1005],min_c,t;
scanf("%d",&ss);
while(ss--)
{
scanf("%s%d",str,&n);
if(!n)
{
printf("%s\n",str);
continue;
}
len = strlen(str);
l = 0;
min_c = str[0];
for(i = len-1;i>0;i--)//找出整个串最小的,且不为0的放到第一位
{
if(str[i]!='0' && str[i]<min_c)
{
min_c = str[i];
flag = i;
}
}
if(min_c!=str[0])//交换
{
t = str[flag];
str[flag] = str[0];
str[0] = t;
n--;
}
for(i = 1;i<len;i++)//从第二位开始
{
if(!n)
break;
min_c = str[i];
for(j = len-1;j>i;j--)//从个位开始找,找到最小的如果小于第i位,即交换
{
if(str[j]<min_c)
{
min_c = str[j];
flag = j;
}
}
if(str[j]!=min_c)
{
t = str[flag];
str[flag] = str[i];
str[i] = t;
n--;
}
}
printf("%s\n",str);
} return 0;
}

FZOJ2111:Min Number的更多相关文章

  1. fzu 2111 Min Number

      http://acm.fzu.edu.cn/problem.php?pid=2111  Problem 2111 Min Number Accept: 572    Submit: 1106Tim ...

  2. lintcode 中等题:Min stack 最小栈

    题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...

  3. Java基础(四):Java Number & Math 类、Character 类、String 类、StringBuffer & StringBuilder 类

    一.Java Number & Math 类: 1.Number类: 一般地,当需要使用数字的时候,我们通常使用内置数据类型,如:byte.int.long.double 等.然而,在实际开发 ...

  4. Java-Runoob:Java Number & Math 类

    ylbtech-Java-Runoob:Java Number & Math 类 1.返回顶部 1. Java Number & Math 类 一般地,当需要使用数字的时候,我们通常使 ...

  5. foj 2111 Problem 2111 Min Number

    Problem 2111 Min Number Accept: 1025    Submit: 2022Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  6. Problem 2111 Min Number

                                                                                                        ...

  7. Java 学习(6):java Number & Math & String & 数组...常用类型

    目录 --- Number & Math类 --- Character 类 --- String 类 --- StringBuffer 类 --- 数组 Number & Math类: ...

  8. LeetCode之“排序”:Largest Number

    题目链接 题目要求: Given a list of non negative integers, arrange them such that they form the largest numbe ...

  9. Linux kernel的中断子系统之(三):IRQ number和中断描述符

    返回目录:<ARM-Linux中断系统>. 总结: 二描述了中断处理示意图,以及关中断.开中断,和IRQ number重要概念. 三介绍了三个重要的结构体,irq_desc.irq_dat ...

随机推荐

  1. IOS7 适配以及向下兼容问题

    1.所有的UIViewController加如下方法.     - (void) viewDidLayoutSubviews {         if ([[[UIDevice currentDevi ...

  2. myeclipse8.5如何注册,转自他出

    Step: 1.建立一个任意名称的Java Project 2.在该工程中建立一个名文MyEclipseGen的Java文件(MyEclipseGen.java) 3.运行下面的代码,会在控制台出现& ...

  3. 轻量级数据sqlite的C++调用示例

    原文地址:http://www.cnblogs.com/kfqcome/archive/2011/06/27/2136999.html #include "stdafx.h" #i ...

  4. CentOS 安装easy_install、pip的方法

    CentOS 安装easy_install的方法: wget -q http://peak.telecommunity.com/dist/ez_setup.py python ez_setup.py ...

  5. 【集训笔记】动态规划【HDOJ1159【HDOJ1003

    终于开始DP了] HDOJ_1159  Common  Subsequence 题目链接 Sample Input abcfbc abfcab programming contest abcd mnp ...

  6. django笔记(二)

    Model many-to-many可以通过through来定义详细信息. 表结构: property django可以用F和Q来配合查找,F可以用于同一个model不同field之间进行比较,可以对 ...

  7. docker学习笔记13:Dockerfile 指令 WORKDIR介绍

    Dockerfile中的WORKDIR指令用于指定容器的一个目录, 容器启动时执行的命令会在该目录下执行. 相当于设置容器的工作目录了.我们来看一个dockerfile文件 #test FROM ub ...

  8. nginx区分手机与电脑浏览器并进入相应站点

    本文要讲的的是如何使用nginx区分pc和手机访问不同的网站,是物理上完全隔离的两套网站(一套移动端.一套pc端),这样带来的好处pc端和移动端 的内容可以不一样,移动版网站不需要包含特别多的内容,只 ...

  9. Oracle Dedicated server 和 Shared server(专用模式 和 共享模式) 说明(转)

    一.  官网说明 在DBCA 建库的时候,有提示让我们选择连接类型,这里有两种类型:专用服务器模式和共享服务器模式.默认使用专用模式.如下图: Oracle 官方文档对这两种文档的说明如下: Abou ...

  10. 浅析Java中的反射机制原理

    反射反射,程序员的快乐! Java中反射机制使用的还是比较广泛的,系统的灵活性.可扩展性大都都是通过反射等方式来加载外部插件,使得系统与插件解耦的同时,增加了功能.但是很多人都只是会用,却是不知道它的 ...