zoukankan      html  css  js  c++  java
  • fzu 2111 Min Number

     
    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
  • 相关阅读:
    FastApi 进阶
    flask为多个接口添加同一个拦截器的方法
    记一次flask上传文件返回200前端却504的问题
    Python在项目外更改项目内引用
    go mod 拉取私有仓库
    go跳出多层循环的几种方式
    Zap简单使用
    记一次Goroutine与wg导致的问题
    go判断字符串是否是IP地址
    SpringBoot的启动流程
  • 原文地址:https://www.cnblogs.com/jeff-wgc/p/4449377.html
Copyright © 2011-2022 走看看