华为机考
031 【靠谱的车】
public class ZT31 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int input = sc.nextInt();
int reduce = 0;
int idx = 0;
while (idx < input){
int next = idx +1 ;
String temp = String.valueOf(next);
String newTemp = "";
if (temp.contains("4")) {
int fourIdx = temp.indexOf("4");
if (fourIdx == temp.length()-1){
newTemp = temp.substring(0,fourIdx) + 5;
}else {
newTemp = temp.substring(0,fourIdx) + 5 + temp.substring(fourIdx+1);
}
reduce += Integer.parseInt(newTemp) - idx -1;
idx = Integer.parseInt(newTemp);
}else {
idx++;
}
}
System.out.println(input - reduce);
}
上述算法遇到大位数据会超时,优化如下
public class ZT3102 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int input = in.nextInt();
//求司机多算了多少[0,10][10,100][100,1000]
int temp = 0;
int total = input;
int k = 0;//记录当前位
int j = 1;//记录个位?
//13
while (total > 0){
if (total % 10 >4) {//当前位大于4
temp += (total % 10 - 1) * k + j ;
}else {//当前位小于4
temp += (total % 10) * k;
}
k = k * 9 + j;
j *= 10;
total = total/10;
}
System.out.println(input - temp);
}
}
032 【快递运输】
public class ZT32 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] input = sc.nextLine().split(",");//2 5 10 11 2510
int max = Integer.parseInt(sc.nextLine());//20
int[] arr = new int[input.length];
for (int i = 0; i < input.length; i++) {
arr[i] = Integer.parseInt(input[i]);
}
Arrays.sort(arr);
System.out.println(calc(arr,0,0,0,max));
}
private static int calc(int[] arr,int idx,int count,int total,int max){
if (total > max || idx>= arr.length){
return count-1;
}
int cl1 = calc(arr, idx+1, count+1,total + arr[idx],max);
int cl2 = calc(arr, idx+1, count, total,max);
return Math.max(cl1,cl2);
}
}
033 【连续字母长度】
public class ZT33 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
int k1 = Integer.parseInt(sc.nextLine());
int[] arr = new int[26];
int temp = 0;
char pre = ' ';
for (int i = 0; i < input.length(); i++) {
if (i == 0){
temp++;
pre = input.charAt(i);
}else if (input.charAt(i) == pre){
temp++;
int count = arr[pre - 'A'];
arr[pre - 'A'] = Math.max(temp, count);
}else {//换代
temp = 1;
pre = input.charAt(i);
arr[input.charAt(i) - 'A'] = Math.max(temp, arr[input.charAt(i) - 'A'] );;
}
}
List<Integer> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
if (arr[i] != 0){
list.add(arr[i]);
}
}
list.sort((a1,b1) -> b1 - a1);
if (list.size()<k1){
System.out.println(-1);
}else {
System.out.println(list.get(k1-1));
}
}
}
034 【两数之和绝对值最小】
public class ZT34 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] input = sc.nextLine().split(" ");
int[] arr = new int[input.length];
for (int i = 0; i < input.length; i++) {
arr[i] = Integer.parseInt(input[i]);
}
int min = Integer.MAX_VALUE;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
min = Math.min(min,Math.abs(arr[i]+arr[j]));
}
}
System.out.println(min);
}
}
035 【流水线】
public class ZT35 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] input = sc.nextLine().split(" ");
String[] job = sc.nextLine().split(" ");
int med = Integer.parseInt(input[0]);
int[] arr = new int[job.length];
for (int i = 0; i < job.length; i++) {
arr[i] = Integer.parseInt(job[i]);
}
Arrays.sort(arr);
List<Medic> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
if (list.size() <med){
list.add(new Medic(arr[i],arr[i]));
}else {
//找到数组中 total最小的medic 加进去
Collections.sort(list);
Medic medic = list.get(0);
medic.total += arr[i];
}
}
Collections.sort(list);
System.out.println(list.get(list.size()-1).total);
}
static class Medic implements Comparable{
private int end;
private int total;
public Medic(int end, int total) {
this.end = end;
this.total = total;
}
@Override
public int compareTo(Object obj) {
Medic medic= (Medic)obj;
return this.total - medic.total;
}
}
}
036 【内存资源分配】
public class ZT36 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] input = sc.nextLine().split(",");
String[] apply = sc.nextLine().split(",");
List<Integer> exist = new ArrayList<>();
Map<Integer,Integer> map = new HashMap<>();
for (int i = 0; i < input.length; i++) {
String[] typeCount = input[i].split(":");
int type = Integer.parseInt(typeCount[0]);
exist.add(type);
map.put(type,Integer.parseInt(typeCount[1]));
}
Collections.sort(exist);
for (int i = 0; i < apply.length; i++) {
boolean flag = false;
int need = Integer.parseInt(apply[i]);
for (int j = 0; j < exist.size(); j++) {
if (need<= exist.get(j)){
//拿出来一个
int pool = map.get(exist.get(j));
flag = true;
if (--pool == 0){
map.remove(exist.get(j));
exist.remove(j);
}else {
map.put(exist.get(j),pool);
}
break;
}
}
System.out.print(flag + " ");
}
System.out.println();
}
}
037 【判断一组不等式是否满足约束并输出最大差】
038 【判断字符串子序列】
public class ZT38 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String target = sc.nextLine();//[0,100]
String source = sc.nextLine();//[0,50w]
int idx = source.length();
for (int i = target.length()-1; i >= 0; i--) {
char ta = target.charAt(i);
if (source.contains(String.valueOf(ta))) {
int idxLast = source.lastIndexOf(String.valueOf(ta));
if (idxLast < idx){
idx = idxLast;
}else {
System.out.println(-1);
return;
}
}else {
System.out.println(-1);
return;
}
}
System.out.println(idx);
}
}
039 【拼接URL】
040 【求符合要求的结对方式】
public class ZT40 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int people = Integer.parseInt(sc.nextLine());
String[] input = sc.nextLine().split(" ");
int target = Integer.parseInt(sc.nextLine());
int[] arr = new int[people];
for (int i = 0; i < people; i++) {
arr[i] = Integer.parseInt(input[i]);
}
int count = 0;
for (int i = 0; i < people; i++) {
for (int j = i + 1; j < people; j++) {
if (target == arr[i] + arr[j]){
count++;
}
}
}
System.out.println(count);
}
}