• B


     
    Anton has a positive integer nn, however, it quite looks like a mess, so he wants to make it beautiful after kk swaps of digits. 
    Let the decimal representation of nn as (x1x2xm)10(x1x2⋯xm)10 satisfying that 1x191≤x1≤9, 0xi90≤xi≤9 (2im)(2≤i≤m), which means n=mi=1xi10min=∑i=1mxi10m−i. In each swap, Anton can select two digits xixi and xjxj (1ijm)(1≤i≤j≤m) and then swap them if the integer after this swap has no leading zero. 
    Could you please tell him the minimum integer and the maximum integer he can obtain after kk swaps?

    Input

    The first line contains one integer T indicating the number of test cases. 
    Each of the following T lines describes a test case and contains two space-separated integers n and k. 
    1T1001≤T≤100, 1n,k1091≤n,k≤109. 
    Output

    For each test case, print in one line the minimum integer and the maximum integer which are separated by one space. 
    Sample Input

    5
    12 1
    213 2
    998244353 1
    998244353 2
    998244353 3

    Sample Output

    12 21
    123 321
    298944353 998544323
    238944359 998544332
    233944859 998544332

    题意
    给一个数 N 每次可以交换两个数字的位置
    文交换 K 次后 可以得到的最大值和最小值

    解法
      应该老老实实开数组作各位分离写数据结构,
      但是脑子抽了筋偏要用算术运算的方法来实现交换位。
      我下次再也不瞎鸡乱写了。

      大概思路
      从高位开始向低位扫描,
      比较当前位与最大/最小值,
      如果有必要交换的话,
      每次把数中合法的(首位不为零)最大/最小数提到当前位置。


    错误代码(至今不知道wa在哪里,也不想改了)
      1 #include <bits/stdc++.h>
      2 #include <iostream>
      3 #include <cstring>
      4 #include <stack>
      5 #include <cstdlib>
      6 #include <queue>
      7 #include <cmath>
      8 #include <cstdio>
      9 #include <algorithm>
     10 #include <string>
     11 #include <vector>
     12 #include <list>
     13 #include <iterator>
     14 #include <set>
     15 #include <map>
     16 #include <utility>
     17 #include <iomanip>
     18 #include <ctime>
     19 #include <sstream>
     20 #include <bitset>
     21 #include <deque>
     22 #include <limits>
     23 #include <numeric>
     24 #include <functional>
     25 #include <ctime>
     26 
     27 #define gc getchar()
     28 #define mem(a) memset(a,0,sizeof(a))
     29 #define mod 1000000007
     30 #define sort(a,n,int) sort(a,a+n,less<int>())
     31 #define fread() freopen("in.in","r",stdin)
     32 #define fwrite() freopen("out.out","w",stdout)
     33 using namespace std;
     34 
     35 typedef long long ll;
     36 typedef char ch;
     37 typedef double db;  
     38 
     39 int gcd(int a,int b){
     40     if(a<b)swap(a,b);
     41     if(a%b==0)return b;
     42     else gcd(b,a%b);
     43 }
     44 
     45 double random(double start,double end)
     46 {
     47     return start + (end - start) * rand() / (RAND_MAX + 1.0);
     48 }
     49 const int maxn = 100+10;
     50 const int INF = 0x3f3f3f3f;
     51 double dis(double x1,double y1){
     52     return sqrt(x1 * x1 + y1 * y1);
     53 }
     54 
     55 int main()
     56 {
     57     ll t = 0;
     58     cin >> t;
     59     while(t--)
     60     {
     61         unsigned long long a = 0 , k = 0;
     62         unsigned long long res = 0;
     63         cin >> a >> k;
     64         //cout<<t<<endl;a = t;k = 2;
     65         res = a;
     66         unsigned long long a1 = a;
     67         int j = 0;
     68         for(int i = 0 , counter = 0;counter<k;)
     69         {
     70             unsigned long long s = a;
     71             int min = 9;
     72             int p = -1;
     73             for(j = 0;s/10 != 0;j++)s/=10;
     74             //cout<<j<<endl;         //
     75             if(j==i)break;
     76             if(j==1)
     77             {
     78                 if(a%10<a/10 && a%10!=0) 
     79                     res = (a%10) * 10 + a/10;
     80                 else res = a;
     81                 break;
     82             }
     83             s = a;
     84             for(int l = 0;l<j-i;l++)
     85             {
     86                 if(s % 10 < min)
     87                 {
     88                     if(i!=0 || s % 10!=0)
     89                     {
     90                         min = s % 10;
     91                         p = l;
     92                     }
     93                 }
     94                 s /= 10;
     95             }
     96             if(p==-1)
     97             {
     98                 res = a;
     99                 break; 
    100             }
    101             //cout<<i<<' '<<counter<<endl;//
    102             //cout<<p<<endl;    //
    103             s = a;
    104             int m = 0,n = 0;
    105             int k = 0;
    106             
    107             for(k = 0;k <=j-i;k++)
    108             {
    109                 if(k == p)
    110                 {
    111                     m = s % 10;
    112                 }
    113                 if(k == j-i)
    114                 {
    115                     n = s % 10;
    116                 }
    117                 s/=10;
    118             }
    119             //cout<<m<<' '<<n<<endl;    //
    120             if(n <= m)
    121             {
    122                 i++;
    123                 continue;
    124             }
    125             int mul = 1;
    126             for(k = 0;k <=j-i;k++)
    127             {
    128                 if(k == p)
    129                 {
    130                     a = a- m*mul +n*mul;
    131                 }
    132                 if(k == j-i)
    133                 {
    134                     a = a- n*mul +m*mul;
    135                 }
    136                 mul*=10;
    137             }
    138             res = a;
    139             //cout<<a<<endl;            //
    140             i++;
    141             counter++;
    142         }
    143         cout<<res<<' ';
    144         a = a1;
    145         res = a;
    146         for(int i = 0 , counter = 0;counter<k;)
    147         {
    148             if(i>j)
    149             {
    150                 res = a;
    151                 break;
    152             }
    153             ll s = a;
    154             int max = 0;
    155             int p = 0;
    156             for(j = 0;s/10 != 0;j++)s/=10;
    157             //cout<<j<<endl;         //
    158             if(j==i)break;
    159             if(j==1)
    160             {
    161                 if(a%10>a/10 && a%10!=0) 
    162                     res = (a%10)*10+a/10;
    163                 else res = a;
    164                 break;
    165             }
    166             
    167             s = a;
    168             for(int l = 0;l<j-i;l++)
    169             {
    170                 if(s % 10 > max)
    171                 {
    172                     max = s % 10;
    173                     p = l;
    174                 }
    175                 s /= 10;
    176             }
    177             //cout<<p<<endl;    //
    178             s = a;
    179             int m = 0,n = 0;
    180             int k = 0;
    181             //cout<<s<<endl;    //
    182             for(k = 0;k <=j-i;k++)
    183             {
    184                 if(k == p)
    185                 {
    186                     m = s % 10;
    187                 }
    188                 if(k == j-i)
    189                 {
    190                     n = s % 10;
    191                 }
    192                 s/=10;
    193             }
    194             //cout<<m<<' '<<n<<endl;    //
    195             if(n >= m)
    196             {
    197                 i++;
    198                 continue;
    199             }
    200             int mul = 1;
    201             for(k = 0;k <=j-i;k++)
    202             {
    203                 if(k == p)
    204                 {
    205                     a = a- m*mul +n*mul;
    206                 }
    207                 if(k == j-i)
    208                 {
    209                     a = a- n*mul +m*mul;
    210                 }
    211                 mul*=10;
    212             }
    213             res = a;
    214             i++;
    215             counter++;
    216         }
    217         cout<<res<<endl;
    218         
    219     }
    220     return 0;
    221 }
    View Code


    作者:YukiRinLL

    出处:YukiRinLL的博客--https://www.cnblogs.com/SutsuharaYuki/

    您的支持是对博主最大的鼓励,感谢您的认真阅读。

    本文版权归作者所有,欢迎转载,但请保留该声明。

  • 相关阅读:
    vue
    mongodb
    ejs模板引擎
    ajax
    node.js2
    node.js1
    bootstrap,ECMA
    商城
    面试:----Struts和springmvc的区别--区别上
    OpenStack
  • 原文地址:https://www.cnblogs.com/SutsuharaYuki/p/11301676.html
Copyright © 2020-2023  润新知