博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
fzu 2111 Min Number
阅读量:4957 次
发布时间:2019-06-12

本文共 2466 字,大约阅读时间需要 8 分钟。

 
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 
2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 #include
9 #include
10 #include
11 #include
12 #include
13 14 using namespace std;15 16 const int INF = 0x3f3f3f3f;17 const int MAX = 100 + 10;18 const double eps = 1e-7;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 == 0)28 {29 char min = str[n];30 for(i = 1;i < len;i ++)31 {32 if(str[i] != '0' && str[i] <= min)33 {34 min = str[i];35 temp = i;36 }37 }38 }39 else40 {41 char min = str[n];42 for(i = n + 1;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 = 0;63 while(n --)64 {65 int ji = judge(i);66 if(ji == i)67 {68 n ++;69 i ++;70 }71 else72 {73 str[i] = (str[ji] ^ str[i] ^ (str[ji] = str[i]));74 i ++;75 }76 if(i == len)77 break;78 }79 for(i = 0;i < len ;i ++)80 printf("%c",str[i]);81 puts("");82 }83 return 0;84 }
View Code

 

转载于:https://www.cnblogs.com/jeff-wgc/p/4449377.html

你可能感兴趣的文章
SQLite移植手记1
查看>>
C# windows程序应用与JavaScript 程序交互实现例子
查看>>
HashMap详解
查看>>
js05-DOM对象二
查看>>
mariadb BINLOG_FORMAT = STATEMENT 异常
查看>>
C3P0 WARN: Establishing SSL connection without server's identity verification is not recommended
查看>>
iPhone在日本最牛,在中国输得最慘
查看>>
动态方法决议 和 消息转发
查看>>
WPF自定义搜索框代码分享
查看>>
js 基础拓展
查看>>
C#生成随机数
查看>>
iOS CoreData介绍和使用(以及一些注意事项)
查看>>
Android应用程序与SurfaceFlinger服务的连接过程分析
查看>>
Java回顾之多线程
查看>>
sqlite
查看>>
机电行业如何进行信息化建设
查看>>
9、总线
查看>>
Git 笔记 - section 1
查看>>
HDU6409 没有兄弟的舞会
查看>>
2018 Multi-University Training Contest 10 - TeaTree
查看>>