Problem 2111 Min Number

Accept: 760    Submit: 1516
Time Limit: 1000 mSec    Memory Limit : 32768 KB

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

Source

“高教社杯”第三届福建省大学生程序设计竞赛

无聊找找以前写过的题目,果然还是Too naïve,第一次提交RE了,回去看看是自定义函数可能会返回让数组越界的值....改完AC了

代码:

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<cstdio>
#include<string>
#include<deque>
#include<cmath>
#include<queue>
#include<set>
#include<map>
using namespace std;
int findmin(const string &s,const int &b)//查找起始点之后的最小数
{
int len=s.size(),index=b,t=s[b];
int i;
if(b==0)//前导零的情况
{
for (int i=b; i<len; i++)
{
if(s[i]<t&&s[i]>'0')
{
index=i;
t=s[i];
}
}
}
else
{
for (int i=b; i<len; i++)
{
if(s[i]<t)
{
index=i;
t=s[i];
}
}
}
return index;
}
int main (void)
{
int t,i,j,cnt,n,p,be,LEN;
string s;
cin>>t;
while (t--)
{
cin>>s>>n;
cnt=be=0;
LEN=s.size();
while (cnt<n)
{
if(be>=LEN)
break;
p=findmin(s,be);
if(s[be]==s[p])//若找到的位置本来就是自己,意味着不需要交换.
{
be++;//起始点+1
continue;
}
swap(s[be],s[p]);
cnt++;
}
cout<<s<<endl;
}
return 0;
}

FZU——2111Min Number(多次交换得到最小数,水题)的更多相关文章

  1. 13年山东省赛 The number of steps(概率dp水题)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud The number of steps Time Limit: 1 Sec  Me ...

  2. 数学 FZU 2074 Number of methods

    题目传送门 /* 数学:假设取了第i个,有C(n-1)(i-1)种取法 则ans = sum (C(n-1)(i-1)) (1<i<=n) 即2^(n-1) */ #include < ...

  3. hdu 1005:Number Sequence(水题)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  4. hdu 1018:Big Number(水题)

    Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  5. 水题 HDOJ 4727 The Number Off of FFF

    题目传送门 /* 水题:判断前后的差值是否为1,b[i]记录差值,若没有找到,则是第一个出错 */ #include <cstdio> #include <iostream> ...

  6. FZU 1343 WERTYU --- 水题

    FZU 1343 题目大意:手放在键盘上时,稍不注意就会往右错一位.这样Q就会输入成W,输入J就会变成K 给定一串大写敲错后输入,输出正确的输入(输入保证合法,如输入中不会出现Q,A,Z): 解题思路 ...

  7. FZU -2212 Super Mobile Charger(水题)

     Problem 2212 Super Mobile Charger Accept: 1033    Submit: 1944Time Limit: 1000 mSec    Memory Limit ...

  8. FZU 2102 Solve equation(水,进制转化)&& FZU 2111(贪心,交换使数字最小)

    C Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Pra ...

  9. FZU 2297 Number theory【线段树/单点更新/思维】

    Given a integers x = 1, you have to apply Q (Q ≤ 100000) operations: Multiply, Divide. Input First l ...

随机推荐

  1. Linux中根据访问日志统计访问量最高的前N个IP

    前段时间面试中被问到如上问题,日常不怎么注意积累,以此谨记. 访问IP 页面[nxuser@im440-zh test]$ vi log 135.252.172.181 page1 136.252.1 ...

  2. Python 求两个文本文件以行为单位的交集 并集 差集

    Python 求两个文本文件以行为单位的交集 并集 差集,来代码: s1 = set(open('a.txt','r').readlines()) s2 = set(open('b.txt','r') ...

  3. 【转】瓜娃(guava)的API快速熟悉使用

    http://www.cnblogs.com/snidget/archive/2013/02/05/2893344.html 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: I ...

  4. 爬虫遇到HTTP Error 403的问题

    # coding=gbk from bs4 import BeautifulSoup import requests import urllib x = 1 y = 1 def crawl(url): ...

  5. 第一单元OO总结

  6. Bootstrap历练实例:向列表组添加链接

    向列表组添加链接 通过使用锚标签代替列表项,我们可以向列表组添加链接.我们需要使用 <div> 代替 <ul> 元素.下面的实例演示了这点: <!DOCTYPE html ...

  7. UIKeyboardType

    typedef NS_ENUM(NSInteger, UIKeyboardType) { UIKeyboardTypeDefault, // Default type for the current ...

  8. 【概率dp 高斯消元】bzoj3270: 博物馆

    一类成环概率dp的操作模式 Description 有一天Petya和他的朋友Vasya在进行他们众多旅行中的一次旅行,他们决定去参观一座城堡博物馆.这座博物馆有着特别的样式.它包含由m条走廊连接的n ...

  9. 《零基础入门学习Python》【第一版】视频课后答案第001讲

    测试题答案: 0. Python 是什么类型的语言? Python是脚本语言 脚本语言(Scripting language)是电脑编程语言,因此也能让开发者藉以编写出让电脑听命行事的程序.以简单的方 ...

  10. Windows下新建多级文件夹

    使用system函数调用系统命令"md" 注意:字符串变量的话赋值时要使用双斜杠"\\": system("md C:\\newfolder\\&qu ...