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 ...
随机推荐
- java IO回想小结
java IO原理 IO流用来处理设备之间的传输数据 输入(input):读取外部数据(磁盘.等存储设备)到程序() (内存)中 输出(output):将程序(内存)数据输出到磁盘等存储设备 java ...
- Spring IOC和DI原理讲解并制作LazyCoder版的Spring (一)
转载请注意出处:http://blog.csdn.net/zcm101 写在前面的话 最近,给项目组成员培训了Spring 控制反转和依赖注入的原理,并自己做了个Lazy Coder版的Spring, ...
- java 简单的数据增删该查
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...
- Android面试题整理(1)
1.Activity的生命周期 onCreate(Bundle saveInstanceState):创建activity时调用. onStart():activity可见时调用 ...
- 用Flask实现视频数据流传输
Flask 是一个 Python 实现的 Web 开发微框架.这篇文章是一个讲述如何用它实现传送视频数据流的详细教程. 我敢肯定,现在你已经知道我在O’Reilly Media上发布了有关Flask的 ...
- 【转】opencv-在图像上显示字符(不包括中文)
原文参见:http://blog.csdn.net/ycc892009/article/details/6516528 1 #include <cv.h> #include <hig ...
- BIOS和CMOS的联系与区别
BIOS为何物? BIOS就是(Basic Input/Output System,基本输入/输出系统的缩写)在电脑中起到了最基础的而又最重要的作用.是电脑中最基础的而又最重要的程序.把这一段程序放在 ...
- 你跟大牛之间仅仅差一个google
google在中国被墙的厉害,http://209.116.186.231/ 这个地址能够訪问google.另外.有VPN或者某个奇妙的浏览器也能够. 非技术人员,还能够凑合着用百度,可是技术人员必须 ...
- 使用gson(一)
1.数组和json的转换 package com.test.gson; import com.google.gson.Gson; public class ArrayToJson { public s ...
- C++算术运算符与算术表达式
基本的算术运算符 在本章中主要介绍算术运算符与算术表达式,赋值运算符与赋值表达式,逗号运算符与逗号表达式,其他运算符将在以后各章中陆续介绍. 常见算数运算符 运算符 说明 举例 + 加法运算符,或正值 ...