华为机考
051 【数组组成的最小数字】
//思路:
//1、大于3各数的数组,从其中选择3各最小的数,组合排序可以得到最小的数
//2、小于3各数,组合排序即可
//3、可以根据首位进行排序
052 【水仙花数】
public class ZT02 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = Integer.parseInt(sc.nextLine());//[3,7]
int num = Integer.parseInt(sc.nextLine());
if (count< 3 || count> 7){
System.out.println(-1);
return;
}//3 100 4 1000
int start = 1;
for (int i = 1; i < count; i++) {
start *= 10;
}
int end = start *10 -1;
int times = 0;
for (int i = start; i < end; i++) {
if (checkFlower(i)){
if (times++ == num) {
System.out.println(i);
return;
}
}
}
System.out.println(-1);
}
private static boolean checkFlower(int num){
int temp = num;
int total = 0;
while (temp > 0){
int wei = temp%10;
total += wei *wei * wei;
temp /=10;
}
return total == num;
}
}
053 【素数之积】
public class ZT53 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int target = sc.nextInt();//[0,2147483647]
int max = target;
for (int i = 3; i <= max; i++) {
//先判断能不能被target整除
if (target % i == 0){
max = target / i;
if (checkSu(i) && checkSu(target/i)){
System.out.println(i + " " + target/i);
return;
}
}
}
System.out.println(-1 + " " + -1);
}
//素数是指除了1和本身不能被其他所有数整除
private static boolean checkSu(int num){
for (int i = 2; i < num; i++) {
if (num % i == 0){
return false;
}
}
return true;
}
}
054 【太阳能板最大面积】
055 【停车场车辆统计】
public class ZT55 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] split = sc.nextLine().split(",");
//连续的2个1可以是1个 连续三个1可以是1个
int total = 0;
int tem = 0;
for (int i = 0; i < split.length; i++) {
if (Integer.parseInt(split[i]) == 1){
tem++;
}else {
if (tem != 0){
total += calcMin(tem);
}
tem = 0;
}
}
total += calcMin(tem);
System.out.println(total);
}
private static int calcMin(int num){
//6 -> 2
int total = 0;
while (num >= 3){//有多少3 除掉多少3
num -= 3;
total++;
}
while (num >= 2){//有多少3 除掉多少3
num -= 2;
total++;
}
total += num;
return total;
}
}
056 【统计射击比赛成绩】
public class ZT56 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int count = Integer.parseInt(sc.nextLine());
String[] turnList = sc.nextLine().split(",");
String[] scores = sc.nextLine().split(",");
List<Player> plays = new ArrayList<>();
Map<Integer,Integer> map= new HashMap<>();
int tempCount = 0;
//获取成绩
for (int i = 0; i < count; i++) {
int idx = Integer.parseInt(turnList[i]);
List<Integer> li = new ArrayList<>();
li.add(Integer.parseInt(scores[i]));
Player pls = new Player(idx,li);
if (plays.contains(pls)) {
plays.get(map.get(idx)).list.add(Integer.parseInt(scores[i]));
}else{
map.put(idx,tempCount++);
plays.add(pls);
}
}
//整理成绩
for (int i = 0; i < plays.size(); i++) {
Player player = plays.get(i);
List<Integer> list = player.list;
list.sort((a0,b0) -> b0 -a0);//逆序
int total = 0;
for (int j = 0; j < 3; j++) {
total += list.get(j);
}
player.setScore(total);
}
plays.sort(null);
for (int i = 0; i < plays.size(); i++) {
if (i == plays.size() -1){
System.out.print(plays.get(i).idx);
}else {
System.out.print(plays.get(i).idx + ",");
}
}
}
static class Player implements Comparable<Player>{
private int idx;
private List<Integer> list;
private int score;
public void setScore(int score) {
this.score = score;
}
public Player(int idx, List<Integer> list) {
this.idx = idx;
this.list = list;
}
@Override
public boolean equals(Object obj) {
Player ply = (Player)obj;
return ply.idx == this.idx;
}
@Override
public int compareTo(Player ply) {
if (ply.score != this.score){
return ply.score - this.score;
}else {
return ply.idx - this.idx;
}
}
}
}
057 【完全二叉树非叶子部分后序遍历】
public class ZT57Tree {
private static List<TreeNode> nodes = new ArrayList<>();
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] input = sc.nextLine().split(" ");
int[] arr = new int[input.length+1];
for (int i = 1; i <= input.length; i++) {
arr[i] = Integer.parseInt(input[i-1]);
}
buildTree(arr);
//检查叶子节点
for (int i = 0; i < nodes.size(); i++) {
int degree = 0;
if (nodes.get(i).left != null){
degree++;
}
if (nodes.get(i).right != null){
degree++;
}
nodes.get(i).degree = degree;
}
afterPrint(nodes.get(1));
}
private static void buildTree(int[] arr){
nodes.add(new TreeNode(0,null,null));
//先将所有的节点放到数组中
for (int i = 1; i < arr.length; i++) {
nodes.add(new TreeNode(arr[i],null,null));
}
for (int i = 1; i < nodes.size(); i++) {
if (2 * i < nodes.size()){
nodes.get(i).left = nodes.get(2 * i);
}
if (2 * i +1 < nodes.size()){
nodes.get(i).right = nodes.get(2 * i +1);
}
}
}
private static void print(TreeNode node){
//只输出非叶子节点
if (node.degree != 0){
System.out.println(node.val);
}
}
//前序遍历 根[根输出] 左 右
private static void prePrint(TreeNode node){
print(node);
if (node.left != null){
prePrint(node.left);
}
if (node.right != null){
prePrint(node.right);
}
}
//中序遍历 左 根[根输出] 右
private static void middlePrint(TreeNode node){
if (node.left != null){
middlePrint(node.left);
}
print(node);
if (node.right != null){
middlePrint(node.right);
}
}
//后续遍历 左-右-根[根输出]
private static void afterPrint(TreeNode node){
if (node.left != null){
afterPrint(node.left);
}
if (node.right != null){
afterPrint(node.right);
}
print(node);
}
private static class TreeNode{
private int degree;
private int val;
private TreeNode left;
private TreeNode right;
public TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
}
058 【玩牌高手】
059 【相对开音节】
public class ZT59 {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
String[] input = sc.nextLine().split(" ");
int total = 0;
for (int i = 0; i < input.length; i++) {
String content = input[i];
boolean flag = true;
for (int j = 0; j < content.length(); j++) {
if (content.charAt(j) < 'a' || content.charAt(j)> 'z'){
flag = false;
break;
}
}
if (flag){
total += checkKai(content);
}
}
System.out.println(total);
}
private static int checkKai(String str){
String strTemp = "";
for (int i = str.length()-1; i >=0 ; i--) {
strTemp += str.charAt(i);
}
int left = 0;
int right = 0;
int total = 0;
String tem = "";
while (right<=str.length()){
tem = strTemp.substring(left,right);
if (tem.length() < 4 ){
right++;
continue;
}
if (checkTrue(tem)) {
total++;
}
left++;
right++;
}
return total;
}
private static boolean checkTrue(String str){
//辅音+元音(aeiou)+辅音(r除外)+e blame
if (!yuanyin.contains(str.charAt(0)) && 'e' == str.charAt(str.length()-1)){//首位是辅音 末尾是元音
int temYuan = 0;
for (int i = 1; i < str.length()-1; i++) {//中间2位或3位
if (yuanyin.contains(str.charAt(i))){
temYuan = i;
}
if (temYuan!= 0 && !yuanyin.contains(str.charAt(i)) && str.charAt(i) != 'r' && i> temYuan){
return true;
}
}
}
return false;
}
private static List<Character> yuanyin = new ArrayList<>();
static {
char[] f1 = {'a','e','i','o','u'};
for (int i = 0; i < f1.length; i++) {
yuanyin.add(f1[i]);
}
}
}
060 【消消乐游戏】
public class ZT60 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String input = sc.nextLine();
while (true){
char pre = input.charAt(0);
boolean flag = false;
for (int i = 1; i < input.length(); i++) {
if (input.charAt(i) == pre){
input = input.substring(0,i-1) + input.substring(i+1);
flag = true;
break;
}else {
pre = input.charAt(i);
}
}
if (!flag || input.length() == 0){
System.out.println(input.length());
return;
}
}
}
}