// HJ21 简单密码
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String line = in.nextLine();
HashMap<Character, Character> map = new HashMap<>();
map.put('1', '1');
map.put('a', '2');
map.put('b', '2');
map.put('c', '2');
map.put('d', '3');
map.put('e', '3');
map.put('f', '3');
map.put('g', '4');
map.put('h', '4');
map.put('i', '4');
map.put('j', '5');
map.put('k', '5');
map.put('l', '5');
map.put('m', '6');
map.put('n', '6');
map.put('o', '6');
map.put('p', '7');
map.put('q', '7');
map.put('r', '7');
map.put('s', '7');
map.put('t', '8');
map.put('u', '8');
map.put('v', '8');
map.put('w', '9');
map.put('x', '9');
map.put('y', '9');
map.put('z', '9');
map.put('0', '0');
StringBuffer sb = new StringBuffer();
for (char c : line.toCharArray()) {
if ('A' <= c && c < 'Z') {
sb.append((char) (c + 33));
} else if (c == 'Z') {
sb.append('a');
} else if (map.get(c) != null) {
sb.append(map.get(c));
} else {
sb.append(c);
}
}
System.out.println(sb.toString());
}
}
// HJ22 汽水瓶
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNextInt()) {
int n = in.nextInt();
if (n == 0) break;
int result = 0;
int k = n / 3;
int m = n % 3;
result += k;
if ((k + m) == 2) {
result += 1;
}
while ( (k + m) >= 3 ) {
int sum = k + m;
k = sum / 3;
m = sum % 3;
result += k;
if ((k + m) == 2) {
result += 1;
}
}
System.out.println(result);
}
}
}
// HJ23 删除字符串中出现次数最少的字符
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String line = in.nextLine();
if (line.length() == 1) {
System.out.println(line);
}
HashMap<Character, Integer> map = new HashMap<>();
for (char c : line.toCharArray()) {
if (!map.containsKey(c)) {
map.put(c, 1);
} else {
map.put(c, map.get(c) + 1);
}
}
ArrayList<te> tes = new ArrayList<>();
for (Map.Entry<Character, Integer> entry : map.entrySet()) {
te te = new te(entry.getKey(), entry.getValue());
tes.add(te);
}
tes.sort((o1, o2) -> {
if (o1.getCount() > o2.getCount()) {
return 1;
}
if (o1.getCount() == o2.getCount()) {
return 0;
}
return -1;
});
ArrayList<Character> delChars = new ArrayList<>();
Integer minCount = tes.get(0).getCount();
for (te te : tes) {
if (Objects.equals(te.getCount(), minCount)) {
delChars.add(te.getC());
}
}
StringBuffer sb = new StringBuffer();
for (char c : line.toCharArray()) {
if (!delChars.contains(c)) {
sb.append(c);
}
}
System.out.println(sb.toString());
}
static class te {
private Character c;
private Integer count;
public te(Character c, Integer count) {
this.c = c;
this.count = count;
}
public Character getC() {
return c;
}
public void setC(Character c) {
this.c = c;
}
public Integer getCount() {
return count;
}
public void setCount(Integer count) {
this.count = count;
}
}
}
// HJ26 字符串排序
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String line = in.nextLine();
ArrayList<Character> characters = new ArrayList<>();
for (char aChar : line.toCharArray()) {
if (('a' <= aChar && aChar <= 'z') || ('A' <= aChar && aChar <= 'Z')) {
characters.add(aChar);
}
}
characters.sort((o1, o2) -> {
if ('a' <= o1 && o1 <= 'z') {
o1 = (char) (o1 - 32);
}
if ('a' <= o2 && o2 <= 'z') {
o2 = (char) (o2 - 32);
}
if (o1 > o2) {
return 1;
} else if (o1 == o2) {
return 0;
} else {
return -1;
}
});
int index = 0;
StringBuffer sb = new StringBuffer();
for (char aChar : line.toCharArray()) {
if (!(('a' <= aChar && aChar <= 'z') || ('A' <= aChar && aChar <= 'Z'))) {
sb.append(aChar);
} else {
sb.append(characters.get(index++));
}
}
System.out.println(sb.toString());
}
}
华为机试-HJ21~HJ30
最新推荐文章于 2024-07-11 17:24:34 发布