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");
}
}
}