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代码:
 #include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
#include <string>
#include <math.h>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue> using namespace std; const int INF = 0x3f3f3f3f;
const int MAX = + ;
const double eps = 1e-;
const double PI = acos(-1.0); char str[MAX];
int len; int judge(int n)
{
int temp = n , i;
if(n == )
{
char min = str[n];
for(i = ;i < len;i ++)
{
if(str[i] != '' && str[i] <= min)
{
min = str[i];
temp = i;
}
}
}
else
{
char min = str[n];
for(i = n + ;i < len ;i ++)
{
if(str[i] <= min)
{
min = str[i];
temp = i;
}
}
}
return temp;
} int main()
{
int T , n;
scanf("%d",&T);
while(T --)
{
scanf("%s %d",str , &n);
len = strlen(str);
int i = ;
while(n --)
{
int ji = judge(i);
if(ji == i)
{
n ++;
i ++;
}
else
{
str[i] = (str[ji] ^ str[i] ^ (str[ji] = str[i]));
i ++;
}
if(i == len)
break;
}
for(i = ;i < len ;i ++)
printf("%c",str[i]);
puts("");
}
return ;
}

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. SQLite(快速上手版)笔记

    原文 1. SQL语法关键字 关键字 描述 Create Table 创建数据表 Alter Table 修改数据表 Drop Table 删除数据表 Create Index 创建索引 Drop I ...

  2. find grep 组合使用

    1. 查找所有".h"文件 find /PATH -name "*.h" 2. 查找所有".h"文件中的含有"helloworld ...

  3. Myeclipse配置 项目编码格式

    修改MyEclipse 工作空间的编码 修改之后,在该工作空间下创建的任何项目,编码都是UTF-8,既,该项目下所有文件都是utf-8 格式,工作空间影响项目,项目影响文件 General --> ...

  4. 出现upstream sent too big header while reading response header from upstream错误

    一个POS系统,出现upstream sent too big header while reading response header from upstream错误. 1.反向代理端,可以放到se ...

  5. WGZX:javaScript 学习心得--1

    标签: javascriptiframedreamweaver浏览器htmltable 2008-09-11 10:50 1071人阅读 评论(0) 收藏 举报  分类: UI(21)  1,docu ...

  6. java多线程编程(二创建线程)

    1.概念           因为java是完全面向对象的,所以在java中,我们说的线程,就是Thread类的一个实例对象.所以,一个线程就是一个对象,它有自己字段和方法. 2.创建线程 创建线程有 ...

  7. QT多线程及通过事件进行通信(通过自定义事件,然后QApplication::postEvent给主界面,我之前用的是信号槽)

    可以通过QThread实现跨平台的多线程开发,Qt库负责在特定平台上的特定多线程实现.要采用QThread进行多线程开发,首先需要包含头文件: #include <QThread> 然后需 ...

  8. LINQ之select方法选择多个字段

    单个字段: var list1 = list.Select(field1 => field1.CouponID).ToList(); 多个字段: var list1 = list.Select( ...

  9. Magento的迁移方法

    Magento有很多配置内容,比如说CMS配置页.Static Stock.多语言配置等等,所以做数据迁移很有必要性,下面就说说如何做迁移 这个技术文章是从网上整理的,不过一个很重要的点被疏忽了,我在 ...

  10. (leetcode)Summary Ranges

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...