http://topic.csdn.net/u/20090603/13/05ea579e-ba1d-416f-9305-f0bf9aa56e53.html
http://kinghuimail.blog.163.com/blog/static/95820408201011139931364/
http://fishlife.iteye.com/blog/641330
1.用C++寫個程序,如何判斷一個操作系統是16位還是32位的?不能用sizeof()函數
A1:
16位的系統下,
int i = 65536;
cout << i; // 輸出0;
int i = 65535;
cout << i; // 輸出-1;
32位的系統下,
int i = 65536;
cout << i; // 輸出65536;
int i = 65535;
cout << i; // 輸出65535;
A2:
int a = ~0;
if( a>65536 )
{
cout<<"32 bit"<<endl;
}
else
{
cout<<"16 bit"<<endl;
}
2.在不用第三方參數的情況下,交換兩個參數的值
#include <stdio.h>
void main()
{
int i=60;
int j=50;
i=i+j;
j=i-j;
i=i-j;
printf("i=%d\n",i);
printf("j=%d\n",j);
}
方法二:
i^=j;
j^=i;
i^=j;
方法三:
// 用加減實現,而且不會溢出
a = a+b-(b=a)
3.struct A
{
char t:4;
char k:4;
unsigned short i:8;
unsigned long m;
}
sizeof(A)=?(不考慮邊界對齊)
這題各種答案都有,有的說是6,有的說是7,我自己在VC++6.0上跑了一下是8;
4.
union a {
int a_int1;
double a_double;
int a_int2;
};
typedef struct
{
a a1;
char y;
} b;
class c
{
double c_double;
b b1;
a a2;
};
輸出cout<<sizeof(c)<<endl;的結果?
注意結構體的邊界對齊。
答:
VC6環境下得出的結果是32
5
.程序改錯
class mml
{
private:
static unsigned int x;
public:
mml(){ x++; }
mml(static unsigned int &) {x++;}
~mml{x--;}
pulic:
virtual mon() {} = 0;
static unsigned int mmc(){return x;}
......
};
class nnl:public mml
{
private:
static unsigned int y;
public:
nnl(){ x++; }
nnl(static unsigned int &) {x++;}
~nnl{x--;}
public:
virtual mon() {};
static unsigned int nnc(){return y;}
......
};
代碼片斷:
mml* pp = new nnl;
..........
delete pp;
A:
基類的析構函數應該爲虛函數
virtual ~mml{x--;}
6
strncpy()和memcpy()的區別:
strncpy()字符串,memcpy()拷貝內存內容.
strncpy
原型:extern char *strncpy(char *dest, char *src, int n);
用法:#include <string.h>
功能:把src所指由NULL結束的字符串的前n個字節複製到dest所指的數組中。
說明:
如果src的前n個字節不含NULL字符,則結果不會以NULL字符結束。
如果src的長度小於n個字節,則以NULL填充dest直到複製完n個字節。
src和dest所指內存區域不可以重疊且dest必須有足夠的空間來容納src的字符串。
返回指向dest的指針。
memcpy
原型:extern void *memcpy(void *dest, void *src, unsigned int count);
用法:#include <string.h>
功能:由src所指內存區域複製count個字節到dest所指內存區域。
說明:src和dest所指內存區域不能重疊,函數返回指向dest的指針。
7
單鏈表逆序的遞歸與非遞歸算法 (轉)
#include <iostream>
using namespace std;
struct Node
{
int data ;
Node *next ;
};
typedef struct Node Node ;
Node* ReverseList(Node* head)
{
if (!head || !head->next)
{
return head;
}
Node* p1 = head;
Node* p2 = p1->next;
head->next = NULL;
while (p2)
{
p1 = p2;
p2 = p2->next;
p1->next = head;
head = p1;
}
return head;
}
Node* RecReverseList(Node* head) //遞歸方法
{
if (!head || !head->next)
{
return head;
}
Node *newhead = RecReverseList(head->next);
head->next->next = head;
head->next = NULL;
return newhead;
}
void main()
{
Node a, b, c;
a.data = 1, a.next = &b;
b.data = 2, b.next = &c;
c.data = 3, c.next = NULL;
Node* tmp = &a;
while(tmp)
{
cout<<tmp->data<<" ";
tmp = tmp->next;
}
cout<<endl;
tmp = RecReverseList(&a);
while(tmp)
{
cout<<tmp->data<<" ";
tmp = tmp->next;
}
cout<<endl;
}
一. 鏈表
1. 鏈表中的倒數第m個元素
給定一個單向鏈表,請設計一個既節省時間又節省空間的算法來找出該鏈表中的倒數第m個元素。實現這個算法。
“倒數第m個元素”是這樣規定的:當m=0時,鏈表的最後一個元素(尾元素)將被返回。
前進m步後啓動一個“後指針”
2. 空鏈表和循環鏈表
給定一個鏈表,它可能是一個以"NULL"結尾的非循環鏈表,也可能是一個循環結構結尾的循環鏈表。寫一個函數來判斷該鏈表是一個循環鏈表還是一個非循環 鏈表,該函數不得對鏈表本身做任何修改。
用兩個指針,一快一慢,如果是循環鏈表,快的指針遲早會 超過慢指針,時間複雜度:O(n),最多也是3n .
int DetermineTermination(node *head)
{
node *fast, *slow;
fast = slow = head;
while(1){
if(!fast || !fast->next){
return 0;
}
else if(fast == slow || fast->next == slow){
return 1;
}
else {
slow = slow->next;
fast = fast->next->next;
}
}
}
二、其他
1. 字節的升序存儲和降序存儲方式
請編寫一個函數還判斷某計算機的字節存儲順序是升序(little-endian)還是降序(big-endian)。
“字節的存儲順序”指的是多字節數據的各個字節在計算機裏的存儲順序。比如說用來表示整數的那4個字節的順序,其包括兩種,一是按從最低位字節 (LSB)到最高位字節(MSB)的順序進行存儲;另一種是從MSB到LSB的順序進行存儲。這裏說的“高位字節”指的是字(word)中的高位字節。如 果字節表示的是字的低值部分,我們就說它是這個字的LSB。比如說,數值“5A6C”中的LSB就是"6C"。反過來說,“5A6C”中的MSB是
“5A”。
在一臺採用字節降序存儲方案的計算機裏,MSB將被保存在最低位的地址裏;在一臺採用字節升序存儲方案的計算機裏,LSB將被保存在最低位的地址裏。比如 說,採用字節降序存儲方案的計算機把十六進制值“A45C”中的"A4"保存在第一個字節裏,把"5C"保存在第二個字節裏;而一臺採用字節升序存儲方案 的計算機將把“5C”保存在第一個字節裏,把"A4"保存在第二個字節裏。
具體到本題中,可以使用字符指針來獲取某個字節中的內容。
/* Return 1 if the machine is little-endian, 0 if the
* machine is big-endian
*/
int Endianness(void)
{
int testNum;
char *ptr;
testNum = 1;
ptr = (char *)&testNum;
return (*ptr);
}
有一種更精妙的解法,利用了“union”類型:這種類型與 “struct”類似,
只是它的所有數據成員都是從內存中的同一個位置開始存儲的。
這樣使你能夠把同樣的數據當做不同的變量類型來使用。
/* Return 1 if the machine is little-endian, 0 if the
* machine is big-endian
*/
int Endianness(void)
{
union{
int theInteger;
char singleByte;
} endianTest;
endianTest.theInteger = 1;
return endianTest.singleByte;
}