http://acm.fzu.edu.cn/problem.php?pid=2111
 Problem 2111 Min Number

Accept: 572    Submit: 1106
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
 
分析:
 
由于数字较大10^100 , 所以考虑字符串解决,只需判断是否为首字符,是的话和后面的最小的靠后的非‘0’字符交换,否的话和后面的最小的字符交换即可。
 
AC代码:
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <stdlib.h>
  4. #include <algorithm>
  5. #include <string.h>
  6. #include <string>
  7. #include <math.h>
  8. #include <map>
  9. #include <set>
  10. #include <vector>
  11. #include <stack>
  12. #include <queue>
  13.  
  14. using namespace std;
  15.  
  16. const int INF = 0x3f3f3f3f;
  17. const int MAX = + ;
  18. const double eps = 1e-;
  19. const double PI = acos(-1.0);
  20.  
  21. char str[MAX];
  22. int len;
  23.  
  24. int judge(int n)
  25. {
  26. int temp = n , i;
  27. if(n == )
  28. {
  29. char min = str[n];
  30. for(i = ;i < len;i ++)
  31. {
  32. if(str[i] != '' && str[i] <= min)
  33. {
  34. min = str[i];
  35. temp = i;
  36. }
  37. }
  38. }
  39. else
  40. {
  41. char min = str[n];
  42. for(i = n + ;i < len ;i ++)
  43. {
  44. if(str[i] <= min)
  45. {
  46. min = str[i];
  47. temp = i;
  48. }
  49. }
  50. }
  51. return temp;
  52. }
  53.  
  54. int main()
  55. {
  56. int T , n;
  57. scanf("%d",&T);
  58. while(T --)
  59. {
  60. scanf("%s %d",str , &n);
  61. len = strlen(str);
  62. int i = ;
  63. while(n --)
  64. {
  65. int ji = judge(i);
  66. if(ji == i)
  67. {
  68. n ++;
  69. i ++;
  70. }
  71. else
  72. {
  73. str[i] = (str[ji] ^ str[i] ^ (str[ji] = str[i]));
  74. i ++;
  75. }
  76. if(i == len)
  77. break;
  78. }
  79. for(i = ;i < len ;i ++)
  80. printf("%c",str[i]);
  81. puts("");
  82. }
  83. return ;
  84. }

fzu 2111 Min Number的更多相关文章

  1. foj 2111 Problem 2111 Min Number

    Problem 2111 Min Number Accept: 1025    Submit: 2022Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

  2. Problem 2111 Min Number

                                                                                                        ...

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

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

  4. FZU 1649 Prime number or not米勒拉宾大素数判定方法。

    C - Prime number or not Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & % ...

  5. FZOJ2111:Min Number

    Problem Description Now you are given one non-negative integer n in 10-base notation, it will only c ...

  6. FZU - 2109 Mountain Number 数位dp

    Mountain Number One integer number x is called "Mountain Number" if: (1) x>0 and x is a ...

  7. FZU 2109 Mountain Number

    http://acm.fzu.edu.cn/problem.php?pid=2109 题意:找出区间[l,r]内满足奇数位的数字大于相邻偶数位数字的个数. 典型的数位dp了,记录一下当前位是奇数位还是 ...

  8. FZU Problem 1853 Number Deletion

    Problem 1853 Number Deletion Accept: 80    Submit: 239 Time Limit: 1000 mSec    Memory Limit : 32768 ...

  9. FZU——2111Min Number(多次交换得到最小数,水题)

    Problem 2111 Min Number Accept: 760    Submit: 1516 Time Limit: 1000 mSec    Memory Limit : 32768 KB ...

随机推荐

  1. epoll 实际使用

    void DataHandle::recv() {     sleep(2);     _data_router -> readInfoHw(&mInfo);     ALOGD(SYS ...

  2. 【转】Unity 解析Json字符串

    http://blog.csdn.net/qq_15267341/article/details/52013190 LitJSON使用很简单,两个步骤: 1 将LitJSON.dll文件拖动到unit ...

  3. hbase体系结构以及说明

    HMaster:数据库总控节点 HRegionServer:通常是一个物理节点即一台单独的计算机,一个HRegionServer包含多个HRegion,假如一个表有一亿行数据,那么可能会分散在一个Re ...

  4. dubbo,gradle,spring build from source

    https://github.com/alibaba/dubbo http://www.csdn.net/article/2012-11-13/2811827-alibaba-Dubbo-Distri ...

  5. 答CsdnBlogger问-关于VR取代安卓的问题

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 安卓未来的发展和命运几何? 现在VR和AR各种火爆,是否安卓能够在洪流中屹立不倒呢? 你好,其实这个 ...

  6. Linq&Lumda---LINQ to DataSet的DataTable操作

    1. DataTable读取列表 DataSet ds = new DataSet();// 省略ds的Fill代码DataTable products = ds.Tables["Produ ...

  7. 用户控件UserControl图片资源定位(一)---Xaml引用图片

    MEF编程实现巧妙灵活松耦合组件化编程,一些细节需要花费不小心思去处理: 其中组件中若包含用户控件,且需要访问图片资源,那么Xaml引用资源需要做以下设置 1. 用户控件(usercontrol)所在 ...

  8. 1069 Nim游戏

    1069 Nim游戏 基准时间限制:1 秒 空间限制:131072 KB 有N堆石子.A B两个人轮流拿,A先拿.每次只能从一堆中取若干个,可将一堆全取走,但不可不取,拿到最后1颗石子的人获胜.假设A ...

  9. Ubuntu deb包使用

    deb是debian linus的安装格式,跟red hat的rpm非常相似,最基本的安装命令是:dpkg -i file.deb dpkg 是Debian Package的简写,是为Debian 专 ...

  10. sqlserver 中含有某字符串

    查找 sqlserver 中字符串的ascii码SET TEXTSIZE 0-- Create variables for the character string and for the curre ...