2022.06.26 华为od机试真题

1.最长连续子串

 有N个正整数组成的一个序列
 给定一个整数sum
 求长度最长的的连续子序列使他们的和等于sum
 返回次子序列的长度
 如果没有满足要求的序列 返回-1
 案例1:
 输入
 1,2,3,4,2
 6
 输出
 3
 解析:1,2,3和4,2两个序列均能满足要求
 所以最长的连续序列为1,2,3 因此结果为3

 示例2:
 输入
 1,2,3,4,2
 20
 输出
 -1
 解释:没有满足要求的子序列,返回-1

 备注: 输入序列仅由数字和英文逗号构成
 数字之间采用英文逗号分割
 序列长度   1<=N<=200
 输入序列不考虑异常情况
 由题目保证输入序列满足要求
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author CZM
 * @date 2022 06 26 20:12
 */
// 最长连续子串
public class test02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String line = sc.nextLine();
        int num = Integer.parseInt(sc.nextLine());
        String[] split = line.split(",");
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < split.length; i++){
            list.add(Integer.parseInt(split[i]));
        }
        int count = -1;
        for (int i = 0; i < list.size(); i++){
            if (list.get(i) == num) {
                count = Math.max(1,count);
            } else if (list.get(i) < num) {
                int sum = 0;
                int start = i;
                while (start < list.size()){
                    sum += list.get(start);
                    if (sum == num) {
                        count = Math.max(count, start+1-i);
                        break;
                    } else if (sum > num){
                        break;
                    } else {
                        start++;
                    }
                }
            }
        }
        System.out.println(count);
    }
}
 题目描述
 输入N个互不相同的二维整数坐标,求这N个坐标可以构成的正方形数量。[内积为零的的两个向量垂直]
 输入描述
 第一行输入为N,N代表坐标数量,N为正整数。N <= 100
 之后的 K 行输入为坐标x y以空格分隔,x,y为整数,-10<=x, y<=10
 输出描述
 输出可以构成的正方形数量。

 示例 1
 输入
 3
 1 3
 2 4
 3 1
 输出
 0 (3个点不足以构成正方形)
 
 示例 2 
 输入
 4
 0 0
 1 2
 3 1
 2 -1
 输出
 1

2.正方形数量


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
 * @author CZM
 * @date 2022 06 26 20:12
 */
// 正方形数量
public class test03 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = Integer.parseInt(sc.nextLine());
        List<String> list = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            list.add(sc.nextLine());
        }
        int count = 0;
        if (num <= 3) {
            System.out.println(count);
            return;
        }
        for (int i = 0; i<list.size(); i++) {
            String str = list.get(i);
            String[] split = str.split(" ");
            int x1 = Integer.parseInt(split[0]);
            int y1 = Integer.parseInt(split[1]);
            for (int j = i + 1; j<list.size(); j++) {
                String str1 = list.get(j);
                String[] split1 = str1.split(" ");
                int x2 = Integer.parseInt(split1[0]);
                int y2 = Integer.parseInt(split1[1]);

                int x31 = x1 - (y1-y2);
                int y31 = y1 + (x1-x2);
                int x41 = x2 - (y1-y2);
                int y41 = y2 + (x1-x2);

                int x32 = x1 + (y1-y2);
                int y32 = y1 - (x1-x2);
                int x42 = x2 + (y1-y2);
                int y42 = y2 - (x1-x2);

                if (list.contains(x31 + " " + y31) && list.contains(x41 + " " + y41)) {
                    count++;
                }
                if (list.contains(x32 + " " + y32) && list.contains(x42 + " " + y42)) {
                    count++;
                }
            }
        }
        System.out.println(count / 4);
    }
}

3.二叉树层次遍历(不会做)

 题目描述
 有一棵二叉树,每个节点由一个大写字母标识(最多26个节点)。
 现有两组字母,分别表示后序遍历(父节点->左孩子->右孩子)
 和中序遍历(左孩子->父节点->右孩子)的结果,
 请你输出层次遍历(左孩子->右孩子->父节点)的结果
 
 输入描述
 输入1行,分别表示后序遍历(父节点->左孩子->右孩子)和中序遍历(左孩子->父节点->右孩子)的结果。
 
 输出描述
 输出层次遍历(左孩子->右孩子->父节点)的结果。

 示例 
 输入
 CBEFDA CBAEDF

 输出
 ABDCEF
 说明:(如下图)

在这里插入图片描述

  • 8
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 19
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chenzm666666

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值