# NYOJ 448 寻找最大数

## 寻找最大数

2
92081346718538 10
1008908 5

9888
98

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 110;

int t, m, len, k;
char c[maxn];

int main()
{
scanf("%d", &t);
while (t--){
scanf("%s%d", c, &m);
len = strlen(c);
k = len - m;    //将问题转化为取出k=len-m为数，使其最大
int begin = 0, end = len - k, index = 0, num, max1 = -1;
for (int i = 0; i < k; i++){
for (int j = begin; j <= end; j++){     //遍历数组，注意开始和结束位置的改变
num = c[j] - '0';
if (num > max1){      //不要写>=,因为最大数字有多个，我们去最左边的那个
max1 = num;
index = j;
}
}
printf("%d", max1);
max1 = -1;
begin = index + 1;     //注意是index+1；
end++;
}
printf("\n");
}
return 0;
}


JAVA解决大数问题

import java.util.Arrays;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a = sc.nextInt();
while (a-- > 0){
String s = sc.next();
char[] chs = s.toCharArray();
int n = sc.nextInt();
int num = s.length()-n;
Arrays.sort(chs);
for(int i = s.length() - 1; i > 0; i--){
if(s.length() - s.indexOf(chs[i]) > num - 1){
System.out.print(chs[i]);
s = s.substring(s.indexOf(chs[i]) + 1, s.length());
chs = s.toCharArray();
Arrays.sort(chs);
i = s.length();
num -= 1;
if (num == s.length()) {
System.out.print(s);
break;
}
}
if (num == 0) {
break;
}
}
System.out.println();
}
}
}

03-09 819

07-18 752

01-13 1041

01-22 333

09-20 383

06-11 75

04-09 317

05-11 418

11-28 671

08-20 366