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 ...
随机推荐
- iOS开发App上传的三大步骤
上传流程 1.itunse connect中->“我的App”中新建创建应用,填写相关的信息 a.项目名称(多创建几个),避免重名 b.想好应用的类型 c.应用截图(5.5,4.7,4,3.5寸 ...
- UVa401 Palindromes
#include <stdio.h>#include <string.h> char mirror(char c){ static const char m[] = &q ...
- Redis中的value包含中文显示的问题?
linux 系统 redis不识别中文 如何显示中文 在Redis中存储的value值是中文“马拉斯加”Shell下get获取后展示的结果为:\xc2\xed\xc0\xad\xcb\xb9\xbc ...
- 用QT创建WINDOWS服务程序
恩, qtservice挺好的http://www.qtsoftware.com/products/appdev/add-on-products/catalog/4/Utilities/qtservi ...
- 如何抓取Thread Dump小结(转)
当系统性能出现问题时,需要从各个方面来查看网络环境.主机资源.查看最经变更的代码等.如果是想从代码层面解决问题,那么最有效的方法就是查看相关dump文件.如果是使用IBM JDK(我默认你是在aix环 ...
- php函数参数
函数的参数 通过参数列表可以传递信息到函数,即以逗号作为分隔符的表达式列表.参数是从左向右求值的. PHP 支持按值传递参数(默认),通过引用传递参数以及默认参数.也支持可变长度参数列表,更多信息参见 ...
- php抓取ajax页面返回图片。
要抓取的页面:http://pic.hao123.com/ 当我们往下滚动的时候,图片是用ajax来动态获取的.这就需要我们仔细分析页面了. 可以看到,异步加载的ajax文件为: http://pic ...
- Zabbix Step 1 : Install CentOS6.5 and Configration
[root@myzabbix Desktop]#rpm -ivh http://repo.zabbix.com/zabbix/2.2/rhel/6/x86_64/zabbix-release-2.2- ...
- UML九种图 之 包图和对象图
前言 对象图和包图依然是对系统的静态的描写叙述.UML九种图加上包图,事实上是十幅图. 包图 1.构成 2.包中的元素 类.接口.用例.构件.其他包等.( ...
- Data Annotation
Data Annotation 什么是Data Annotation ? 如何使用 ? 自定义Validate Attribute EF Db first中使用Data Annotation asp ...