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 ...
随机推荐
- Android中activity保存数据和状态在哪个方法实现
以前只知道在Activity销毁之前,要把数据保存在 onSaveInstanceState(Bundle)方法中,后来学习了别人的微博,学到了很多细节问题,所以整理了一下,希望能帮到大家. 如果看官 ...
- 【JavaScript】==与===对比
1.对于string,number等基础类型,==和===是有区别的 1)不同类型间比较,==之比较“转化成同一类型后的值”看“值”是否相等,===如果类型不同,其结果就是不等 2)同类型比较,直接进 ...
- 数据库迁移(SQL SERVER导入数据到MySql)
地址:http://blog.csdn.net/jiaohougenyang/article/details/44937801 背景:项目最开始时使用的是SQL Server数据库,业务需求现要将数据 ...
- CodeForces 276D – Little Girl and Maximum XOR 贪心
整整10个月后第二次搞这个问题才搞懂........第一次还是太随意了. 解题思路: 经过打表可得规律答案要么是0 要么是2的N次 - 1 要得到最大的XOR值,其值一定是2的N次 - 1 即在 l ...
- 使用Android studio下载github上的工程及问题解决
Android studio内置了github的插件,可以直接下载github上的工程,感觉好爽啊.具体怎么做呢?1.如图所示操作,如果是初次使用会提示输入用户名密码. 2.等android stud ...
- 基于visual Studio2013解决算法导论之012计数排序
题目 计数排序 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <malloc.h> #in ...
- vnc server配置、启动、重启与连接,图形管理linux系统
环境:RedHat Linux 5企业版.Xwindows:gnome (红帽默认安装的图形界面) 尽管我们可以使用SSH连接远程通过字符界面来操作Linux,但是对于更多熟悉图形人来说是很不方便的, ...
- 用php 把数组中偶数,选择出来
我有这种一个小算法,把数组中的全部的偶数或技术分别选择出来.非常多人可能,会循环这个数组,而我恰恰不循环数组就能做到这一点.代码例如以下. function odd($var) { // return ...
- BZOJ 3011: [Usaco2012 Dec]Running Away From the Barn( dfs序 + 主席树 )
子树操作, dfs序即可.然后计算<=L就直接在可持久化线段树上查询 -------------------------------------------------------------- ...
- javascript每日一练(七)——事件二:键盘事件
一.键盘事件 onkeydown触发, keyCode键盘编码 ctrlKey altKey shiftKey 键盘控制div移动 <!doctype html> <html> ...