fzu 2111 Min Number

本文介绍了一种通过字符串操作来获取给定10进制数在特定操作次数下的最小可能值的方法。通过选择合适的字符进行交换,实现从原始数到最小数的转换。
 
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 = 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     else
40     {
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             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 = 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值