FZOJ2111:Min Number
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
Sample Input
Sample Output
#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的更多相关文章
- fzu 2111 Min Number
http://acm.fzu.edu.cn/problem.php?pid=2111 Problem 2111 Min Number Accept: 572 Submit: 1106Tim ...
- lintcode 中等题:Min stack 最小栈
题目 带最小值操作的栈 实现一个带有取最小值min方法的栈,min方法将返回当前栈中的最小值. 你实现的栈将支持push,pop 和 min 操作,所有操作要求都在O(1)时间内完成. 解题 可以定义 ...
- Java基础(四):Java Number & Math 类、Character 类、String 类、StringBuffer & StringBuilder 类
一.Java Number & Math 类: 1.Number类: 一般地,当需要使用数字的时候,我们通常使用内置数据类型,如:byte.int.long.double 等.然而,在实际开发 ...
- Java-Runoob:Java Number & Math 类
ylbtech-Java-Runoob:Java Number & Math 类 1.返回顶部 1. Java Number & Math 类 一般地,当需要使用数字的时候,我们通常使 ...
- foj 2111 Problem 2111 Min Number
Problem 2111 Min Number Accept: 1025 Submit: 2022Time Limit: 1000 mSec Memory Limit : 32768 KB ...
- Problem 2111 Min Number
...
- Java 学习(6):java Number & Math & String & 数组...常用类型
目录 --- Number & Math类 --- Character 类 --- String 类 --- StringBuffer 类 --- 数组 Number & Math类: ...
- LeetCode之“排序”:Largest Number
题目链接 题目要求: Given a list of non negative integers, arrange them such that they form the largest numbe ...
- Linux kernel的中断子系统之(三):IRQ number和中断描述符
返回目录:<ARM-Linux中断系统>. 总结: 二描述了中断处理示意图,以及关中断.开中断,和IRQ number重要概念. 三介绍了三个重要的结构体,irq_desc.irq_dat ...
随机推荐
- AlertDialog具体解释
对话框介绍与演示样例 对话框在程序中不是必备的,可是用好对话框能对我们编写的应用增色不少.採用对话框能够大大添加应用的友好性.比較经常使用的背景是:用户登陆.网络正在下载.下载成功或者 ...
- linq中的临时变量
有一个字符串数组: string[]arrStr={"123","234","345","456"}; 现在想得到该数组 ...
- javascript面向对象创建高级 Web 应用程序
目录 JavaScript 对象是词典 JavaScript 函数是最棒的 构造函数而不是类 原型 静态属性和方法 闭包 模拟私有属性 从类继承 模拟命名空间 应当这样编写 JavaScript ...
- 树莓派安装ftp服务器
在树莓派安装ftp服务器,可上载\下载文件 vsftpd是开源的轻量级的常用ftp服务器. 1,安装vsftpd服务器 (约400KB)sudo apt-get install vsftpd 2,启动 ...
- 【 D3.js 入门系列 — 1 】 第一个程序 HelloWorld
记得以前刚上大一学 C 语言的时候,写的第一个程序就是在控制台上输出 HelloWorld .当时很纳闷,为什么要输出这个.老师解释说所有学编程入门的第一个程序都是在屏幕上输出 HelloWorld, ...
- (Problem 17)Number letter counts
If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + ...
- Column store index 列数据如何匹配成行数据?
SQL Server 2012引入了列存储索引,对每列的数据进行分组和存储,然后联接所有列以完成整个索引.这不同于传统索引,传统索引对每行的数据进行分组和存储,然后联接所有行以完成整个索引. 在访问基 ...
- PHP - Cookie 应用
效果: 代码: <?php //设置编码 header("content-type:text/html; charset=utf-8"); //接收提交的数据 //判断是否接 ...
- DotNet命名规范参考(转)
来自:http://www.cnblogs.com/w-y-f/archive/2012/05/30/2526254.html DotNet命名规范参考 一.命名规范 注意事项:使用英文命名规则,尽量 ...
- Android 播放声音
public static void PlayAlarmRing(Context mContext) { Uri alert = RingtoneManager.getDefaultUri(Ringt ...