1.在 Internet 中实现信息浏览查询服务的是(C)
A DNS B FTP C WWW D ADSL
解析:
WWW 是一种建立在 Internet 上的全球性的、交互的、动态的、多平台的、分布式的,超文本超媒体信息查询系统,也是建立在 Internet 上的一种网络服务。
2.在 OSI 分层模型中,把传输的比特流划分为帧,是哪一层的功能(C )
A 物理层 B 网络层 C 数据链路层 D 传输层
3.已知一个线性表(38,25,74,63,52,48),假定采用散列函数h(key) = key%7 计算散列地址,并散列存储在散列表A【0…6】中,若采用线性探测方法解决 冲突,则在该散列表上进行等概率成功查找的平均查找长度为 C
A 1.5 B 1.7 C 2.0 D 2.3
解析:
4.标题:二叉树平衡检查
【二叉树平衡检查】 实现一个函数,检查二叉树是否平衡,平衡的定义如下,对于树中的任意一个结点,其两颗子树的高度差不超过1。 给定指向树根结点的指针TreeNode* root,请返回一个bool,代表这棵树是否平衡。
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
public class Test3 {
public static int deepth(TreeNode root){
if(root==null){
return 0;
}else{
int leftDeepth = deepth(root.left);
int rightDeepth = deepth(root.right);
int depth = max(leftDeepth,rightDeepth)+1;
return depth;
}
}
public static boolean isBalance(TreeNode root) {
// write code here
if(root==null){
return true;
}else {
int leftDeepth = deepth(root.left);
int rightDeepth = deepth(root.right);
if (abs(leftDeepth - rightDeepth) <= 1) {
return true;
} else {
return false;
}
}
}
}
//方法二:
import java.util.*;
public class Balance {
public boolean isBalance(TreeNode root) {
//判断根元素是否为null
if (root == null) {
return true;
}
//获取左边子树高度
int leftHeight = getTreeHeight(root.left);
int rightHeight = getTreeHeight(root.right);
//左右子树的高度大于1表示不是平衡二叉树
if (Math.abs(leftHeight - rightHeight) > 1) {
return false;
}
//isBalance()检查是否平衡
return isBalance(root.left) && isBalance(root.right);
}//计算树的高度
public static int getTreeHeight(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(getTreeHeight(root.left), getTreeHeight(root.right)) + 1;
}
}
5.标题:数字分类 (20)
给定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:
A1 = 能被5整除的数字中所有偶数的和;
A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4…;
A3 = 被5除后余2的数字的个数;
A4 = 被5除后余3的数字的平均数,精确到小数点后1位;
A5 = 被5除后余4的数字中最大数字。
输入:
每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N,随后给出N个不超过1000的待分类的正整数。数字间以空格分隔。
输出:
对给定的N个正整数,按题目要求计算A1~A5并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。 若其中某一类数字不存在,则在相应位置输出“N”。
public class Test3 {
private static String[] Solution(int[] array) {
String[] result = new String[5];
int flag1 = 0;
int num1 = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] % 5 == 0 && array[i] % 2 == 0) {
num1 = num1 + array[i];
flag1 = 1;
}
}
if (flag1 == 0) {
result[0] = "N";
} else {
result[0] = String.valueOf(num1);
}
int a = 0;
int flag2 = 0;
int num2 = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] % 5 == 1) {
flag2 = 1;
a++;
if (a % 2 != 0) {
num2 = num2 + array[i];
} else {
num2 = num2 - array[i];
}
}
}
if (flag2 == 0) {
result[1] = "N";
} else {
result[1] = String.valueOf(num2);
}
int flag3 = 0;
int count = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] % 5 == 2) {
flag3 = 1;
count++;
}
}
if (flag3 == 0) {
result[2] = "N";
} else {
result[2] = String.valueOf(count);
}
int flag4 = 0;
double num4 = 0;
int c = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] % 5 == 3) {
num4 = num4 + array[i];
c++;
flag4 = 1;
}
}
if (flag4 == 0) {
result[3] = "N";
} else {
DecimalFormat df = new DecimalFormat(".0");
result[3] = df.format(num4 / c);
}
int flag5 = 0;
int max = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] % 5 == 4) {
flag5 = 1;
if (array[i] > max) {
max = array[i];
}
}
}
if (flag5 == 0) {
result[4] = "N";
} else {
result[4] = String.valueOf(max);
}
return result;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
String[] s = line.split(" ");
int[] array = new int[s.length];
for (int i = 0; i < s.length; i++) {
int a = Integer.parseInt(s[i]);
array[i] = a;
}
String[] result = Solution(array);
for (int i = 0; i < 4; i++) {
System.out.print(result[i] + " ");
}
System.out.print(result[4]);
}
}
//方法二:
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int num[] = new int[N];
//n个整数
// A1-A5功能
// flag : A2功能中用到的错误+-
// count: A4中计数
int A1 = 0, A2 = 0, A3 = 0, A4 = 0, A5 = 0, flag = 1, count = 0;
for (int i = 0; i < N; i++) {
num[i] = in.nextInt();
//A1
if (num[i] % 5 == 0) {
if (num[i] % 2 == 0) A1 += num[i];
}
//A2
if (num[i] % 5 == 1) {
A2 += flag * num[i];
flag = -flag;
}
//A3
if (num[i] % 5 == 2) {
A3++;
}
//A4
if (num[i] % 5 == 3) {
A4 += num[i];
count++;
}
//A5
if (num[i] % 5 == 4) {
if (num[i] > A5) A5 = num[i];
}
}
if (A1 != 0) {
System.out.print(A1 + " ");
} else {
System.out.print('N' + " ");
}
if (A2 != 0) {
System.out.print(A2 + " ");
} else {
System.out.print('N' + " ");
}
if (A3 != 0) {
System.out.print(A3 + " ");
} else {
System.out.print('N' + " ");
}
if (A4 != 0) {
System.out.print(A4 / count + "." + (int) ((A4 % count * 100 / count + 5) / 10) + " ");
} else {
System.out.print("N" + " ");
}
if (A5 != 0) {
System.out.print(A5);
} else {
System.out.print("N");
}
}
}
来源:CSDN
作者:Yue_1214
链接:https://blog.csdn.net/weixin_43256788/article/details/103479765