1、假定int整形指針p所指對象的值爲25,p+1所指對象的值爲46,執行(*p)++後,p所指對象的值爲?
答:26
int *p;
int a[2] = {25,46};
p = a;
cout<<*p<<endl;
return 0;
2、某人上樓梯,1步可以跨一個臺階或2個臺階,這個樓梯共有10個臺階,從地面到最上層共有多少種不同跨法?
答案:共89種
只一次兩個臺階 有c(1/9)=9種
2次兩個臺階 有c(2/8)=28種
3次兩個臺階 有C(3/7)=35種
4次兩個臺階 C(4/6)=15種
5次兩個臺階 1種
0次兩個臺階 1種
3、下列說明中 const char *ptr;ptr應該是()
A 指向字符常量的指針;
B 指向字符的常量指針;
C 指向字符串常量的指針;
D 指向字符串的常量指針;
答案:A C
4、對使用關鍵字new所開闢的動態存儲空間,釋放時必須使用 delete 。
答:C++程序中由new分配的動態內存空間必須通過delete釋放。
5、單鏈表逆序(三種方法)
public class LinkedList {
class Node {
private Node next;
private Object data;
public Node() {
super();
}
public Node(Object data, Node next) {
this.data = data;
this.next = next;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
private Node head;
public Node first() {
return head.next;
}
public Node tail() {
Node temp = head;
while (temp.next != null)
temp = temp.next;
return temp;
}
public Node head() {
return head;
}
public LinkedList() {
head = new Node(null, null);
}
public boolean addStack(Object data, Node head) {
boolean flag = false;
Node node = new Node(data, null);
if (!flag) {
node.next = head.next;
head.next = node;
flag = true;
}
return flag;
}
public boolean addQueue(Object data, Node head) {
Node temp = head;
while (temp.next != null)
temp = temp.next;
boolean flag = false;
Node node = new Node(data, null);
if (!flag) {
temp.next = node;
flag = true;
}
return flag;
}
public void print(Node head) {
Node temp = head;
while (temp.next != null) {
System.out.println("list node data :" + temp.data);
temp = temp.next;
}
System.out.println("list node data :" + temp.data);
}
public void addQueue() {
}
//直接頭結點插入
public LinkedList invertedList(Node head) {
LinkedList newList = new LinkedList();
Node temp = head;
while (temp.next != null) {
addStack(temp.data, newList.head);
temp = temp.next;
}
addStack(temp.data, newList.head);
return newList;
}
public int length() {
Node temp = head;
int i = 0;
while (temp.next != null) {
i++;
temp = temp.next;
}
return i;
}
//交換兩個對應的數據來實現,鏈表逆序
public void invertedListExchange(Node head) {
Node tempStart = head;
Object old = null;
Node tempEnd = null;
int halfLen = this.length() / 2;
for (int i = 0; i < halfLen; i++) {
tempEnd = tempStart;
if (i == 0) {
tempEnd = this.tail();
}
while (i != 0 && tempEnd.next.data != old)
tempEnd = tempEnd.next;
System.out.println(tempStart.data);
Object tt = tempStart.data;
tempStart.data = tempEnd.data;
tempEnd.data = tt;
old = tempEnd.data;
tempStart = tempStart.next;
}
}
//利用棧的性質來實現
public LinkedList invertedListByStack(Node head) {
Deque<Object> stack = new java.util.LinkedList<Object>();
Node temp = head;
while (temp.next != null) {
stack.push(temp.data);
temp = temp.next;
}
stack.push(temp.data);
LinkedList newList = new LinkedList();
Node node = newList.head;
while (!stack.isEmpty()) {
addQueue(stack.pop(), node);
}
return newList;
}
}
6、TCP與UDP
TCP:(Transmission Control Protocol 傳輸控制協議)是一種面向連接的、可靠的、基於字節流的傳輸層通信協議。
UDP:(User Datagram Protocol用戶數據報協議)是OSI(Open System Interconnection,開放式系統互聯) 參考模型中一種無連接的傳輸層協議,提供面向事務的簡單不可靠信息傳送服務。
區別: 1.基於連接與無連接
2.TCP要求系統資源較多,UDP較少;
3.UDP程序結構較簡單
4.流模式(TCP)與數據報模式(UDP);
5.TCP保證數據正確性,UDP可能丟包
6.TCP保證數據順序,UDP不保證
差別 |
TCP |
UDP |
是否連接 |
面向連接 |
面向非連接 |
傳輸可靠性 |
可靠 |
不可靠 |
應用場合 |
傳輸大量數據 |
少量數據 |
速度 |
慢 |
快 |
7、分別給出 BOOL,int,float,指針變量
與“零值”,比較的 if 語句(假設變量名爲
var)
答:
BOOL 型變量:if(!var)
int 型變量: if(var==0)
float 型變量:
const float EPSINON = 0.00001;
if ((x >= - EPSINON) && (x <= EPSINON)
指針變量: if(var==NULL)