- 读入一个字符串str,输出字符串str中的连续最长的数字串
输入描述:
个测试输入包含1个测试用例,一个字符串str,长度不超过255。
输出描述:
在一行内输出str中里连续最长的数字串。
示例1
输入
abcd12345ed125ss123456789
输出
123456789
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
String str = scanner.nextLine();
int max = 0;
int count = 0;
int end = 0;
for(int i = 0;i<str.length();i++){
if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){
count ++;
if(max < count ){
max = count;
end = i;
}
}else{
count = 0;
}
}
System.out.println(str.substring(end-max+1,end+1));
}
}
- 输入n个整数,找出其中最小的K个数。例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4。
import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Comparator;
public class Solution {
public ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k) {
ArrayList<Integer> result = new ArrayList<Integer>();
int length = input.length;
if(k > length || k == 0){
return result;
}
PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(k, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2.compareTo(o1);
}
});
for (int i = 0; i < length; i++) {
if (maxHeap.size() != k) {
maxHeap.offer(input[i]);
} else if (maxHeap.peek() > input[i]) {
Integer temp = maxHeap.poll();
temp = null;
maxHeap.offer(input[i]);
}
}
for (Integer integer : maxHeap) {
result.add(integer);
}
return result;
}
}
- 某餐馆有n张桌子,每张桌子有一个参数:a 可容纳的最大人数; 有m批客人,每批客人有两个参数:b人数,c预计消费金额。 在不允许拼桌的情况下,请实现一个算法选择其中一部分客人,使得总预计消费金额最大
输入描述:
输入包括m+2行。 第一行两个整数n(1 <= n <= 50000),m(1 <= m <= 50000) 第二行为n个参数a,即每个桌子可容纳的最大人数,以空格分隔,范围均在32位int范围内。 接下来m行,每行两个参数b,c。分别表示第i批客人的人数和预计消费金额,以空格分隔,范围均在32位int范围内。
输出描述:
输出一个整数,表示最大的总预计消费金额
示例1
输入
3 5 2 4 2 1 3 3 5 3 7 5 9 1 10
输出
20
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
int m = sc.nextInt();
int[] disk = new int[n]; //桌子数组
for (int i = 0; i < n; i ++) {
disk[i] = sc.nextInt();
}
Arrays.sort(disk); // 桌子容纳量从小到大排序
PriorityQueue<Customer> queue = new PriorityQueue<>(); // 将客人按消费额降序加入优先级队列
for (int i = 0; i < m; i ++) {
int b = sc.nextInt();
int c = sc.nextInt();
if(b <= disk[n - 1]) queue.add(new Customer(b, c)); // 如果人数小于桌子最大容纳量,加入队列
}
boolean[] visited = new boolean[n]; // 记录桌子是否被占用
long sum = 0; // 记录总盈利
int count = 0; // 记录已使用的桌子数
while ( ! queue.isEmpty()) {
Customer customer = queue.poll();
for (int i = 0; i < n; i ++) { // 为客人分配桌子
if(customer.peopleCount <= disk[i] && ! visited[i]) {
sum += customer.moneyCount;
visited[i] = true;
count ++;
break;
}
}
if(count == n) break;
}
System.out.println(sum);
}
}
static class Customer implements Comparable<Customer> {
private int peopleCount;
private int moneyCount;
public Customer(int peopleCount, int moneyCount) {
this.peopleCount = peopleCount;
this.moneyCount = moneyCount;
}
@Override
public int compareTo(Customer o) {
if(o.moneyCount > this.moneyCount) return 1;
else if(o.moneyCount < this.moneyCount) return - 1;
return 0;
}
}
}