第一题
本人实现代码:
package com.wl.test;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
String data = cin.nextLine();
String[] parts = data.split(",");
StringBuffer buffer = new StringBuffer();
int n = Integer.parseInt(parts[0]);
for(int i=0; i<parts[1].length(); i++) {
char c = parts[1].charAt(i);
if(c >= 'A' && c <= 'Z') {
buffer.append(c);
}
}
System.out.println(buffer.toString());
}
}
第二题
本人实现代码:
package com.wl.test;
import java.util.Scanner;
public class Mainn {
public static void main(String[] args) {
int[] result;
Scanner cin = new Scanner(System.in);
String d = cin.nextLine();
String[] parts = d.split(" ");
StringBuffer buffer = new StringBuffer();
result = new int[Integer.parseInt(parts[0])];
for (int i = 0; i < result.length; i++) {
result[i] = Integer.parseInt(parts[i + 1]);
}
quickSort(result, 0, result.length - 1);
int var = result[0];
buffer.append(String.valueOf(var) + " ");
for (int i = 1; i < result.length; i++) {
if (result[i] != var) {
var = result[i];
buffer.append(String.valueOf(result[i]) + " ");
}
}
System.out.println(buffer.toString().trim());
}
private static void quickSort(int[] array, int low, int high) {
int pivot;
if (low < high) {
pivot = partition(array, low, high);
quickSort(array, low, pivot - 1);
quickSort(array, pivot + 1, high);
}
}
private static int partition(int[] array, int low, int high) {
int pivotkey = array[low];
while (low < high) {
while (low < high && array[high] >= pivotkey)
high--;
swap(array, low, high);
while (low < high && array[low] <= pivotkey)
low++;
swap(array, low, high);
}
return low;
}
private static void swap(int[] a, int m, int n) {
int x = a[m];
a[m] = a[n];
a[n] = x;
}
}
第三题
本人实现代码:
package com.wl.test;
import java.util.Hashtable;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Hashtable<Character, Integer> table = new Hashtable<>();
Scanner cin = new Scanner(System.in);
String tree = cin.nextLine();
String input = cin.nextLine();
StringBuffer buffer = new StringBuffer();
int treeHeight = 0;
char name = '\0';
String value = "";
for(int i=0; i<tree.length(); i++) {
char c = tree.charAt(i);
if((c >= 'a' && c<= 'z') || (c>='A' && c<='Z')) {
if(name != '\0') {
int x = Integer.parseInt(value);
if(x > treeHeight) {
treeHeight = x;
}
table.put(name, x);
name = '\0';
value = "";
}
name = c;
}
if(c >= '0' && c <= '9') {
value += c;
}
}
if(name != '\0') {
int x = Integer.parseInt(value);
if(x > treeHeight) {
treeHeight = x;
}
table.put(name, x);
name = '\0';
value = "";
}
for(int i=0; i<input.length(); i++) {
if(table.get(input.charAt(i)) != null)
buffer.append(String.valueOf(treeHeight - table.get(input.charAt(i)) + 1) + " ");
else
buffer.append("0 ");
}
System.out.println(buffer.toString().trim());
}
}