2015 百度筆試的一道經典題目,2015筆試經典題目
請編碼實現memcpy函數:void *memcpy(void *dst,const void *src,unsigned int count) 顯然是記憶體複製函數
下面是本人結合memcpy的原始碼實現的一個測試案例,請大家指點
#include <stdio.h>void *memcpy(void *dst,const void *src,unsigned int count){char *p_dst = (char *)(dst);char *p_src = (char *)(src);unsigned int i;if(p_dst>p_src && p_dst<=(p_src+count-1)){while(count){*(p_dst+count-1) = *(p_src+count-1);count--;}}else{for(i=0;i<count;i++){*(p_dst+i) = *(p_src+i);}}return dst;}int main(){ int arr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; memcpy(arr+4,arr,sizeof(int)*6);// 這樣的拷貝 src<dst && src+count-1>dst,也就是說已經產生了覆蓋, //所以用從後往前的複製,進入if for(int i=0;i<10;i++) printf("%d\t",arr[i]);// memcpy(arr,arr+4,sizeof(int)*6);// 這樣的拷貝 src>dst && src<dst+count-1,// 這和正常的拷貝一樣,進入else// for(int i=0;i<10;i++)// printf("%d\t",arr[i]); printf("\n"); return 0;}
1 有人可能會問:
你的演算法保證了任何情況下都能夠把資料正確的複製到目的地址,但是你有沒有考慮過在複製的過程中,你有可能修改了未經處理資料呢?
修改了原始串這樣的功能明確嗎?
回答:對,老兄你說的很對,的確會存在這個問題,舉個例子吧,比如hello world,我們想把中間的空格去掉,濾掉空格就類似於作業系統中有一種記憶體配置方法,叫做可重地位分區分配,就是把小作業移動,去除中間的空隙,然後空出後面的一塊大的記憶體位址,供其他程式使用。這樣對未經處理資料不需要儲存的情況下,可以應用這個函數。
2 可能你還有疑問:最後的dst+count = '\0' 假如傳入的是char 型的數組,怎麼辦呢?你return dst如何判斷空不空呢? —— 好吧,這也是我的問題,請大神們協助解答????
3memcpy() 和 普通 strncpy() 複製函數的區別,看代碼吧,不多說了:
#include <iostream>#include <cstring> // for c style string manipulationusing namespace std;int main (){ char str1[]= "To be or not to be";// 18 個字元(含空格)+ '\0' = 19 個字元,因為預設結尾有 '\0' cout << sizeof(str1) << endl; char str2[40]; cout << str1 << endl; // copy to sized buffer (overflow safe): strncpy ( str2, str1, sizeof(str2) ); cout << str2 << endl; // partial copy (only 5 chars): strncpy ( str2, str1, 5 ); cout << str2 << endl; str2[5] = '\0'; // null character manually added cout << sizeof(str2) << " " << str2 << endl; strncpy(str1 + 3, str1, 6); // 出現記憶體覆蓋問題 cout << str1 ; return 0;}
總結:strcpy 源字串全部拷貝到目標字串中,包括'\0',但是程式員必須保證目標串長度足夠,且不與源串重疊。 strncpy 如果目標長>=指定長>源長,則將源串全部拷貝到目標串,連同'\0' 如果指定長<源長,則將截取源串中按指定長度拷貝到目標字串,不包括'\0',手動添加‘\0’,建議對於前兩種都手動添加就可以了 如果指定長>目標長,錯誤!
#include <iostream>#include <cstring> // for c style string manipulationusing namespace std;const int maxn = 8;int main (){ int i; char dest[]="Hell99iam!";// 10個字元,佔了11個空間,最後一個'\0' char src[]="abc\0def"; strncpy(dest,src,maxn); dest[maxn] = '\0'; for(i=0;i<sizeof(dest);i++) { if(*(dest+i) == '\0') cout << "* "; else cout << *(dest+i) << " "; } cout << endl << dest << endl; char str[maxn]; strncpy(str,src,1); for(i=0;i<sizeof(str);i++) { if(*(str+i) == '\0') cout << "* "; else cout << *(str+i) << " "; } //str[1] = '\0'; 如果沒有這句話,就是的結果了 cout << endl << str << endl; return 0;}/* (c/c++)複製字串src中的內容(字元,數字、漢字....)到字串dest中,複製多少由size_t的 值決定,返回指向dest的指標。如果遇到Null 字元('\0')[1] ,則Null 字元後面全部為空白(字元),maxn 決定有多少個空格*/
3 中第一個函數的結果:
3 中第二個函數的結果:
4 void * memset ( void * ptr, int ch, size_t num );
函數的作用是: fill the first num bytes(注意不是element) of block of memory(由null 指標ptr指向) with value. 最後我們會解釋這句話的含義。在C++中,設計 size_t 就是為了適應多個平台的 。size_t的引入增強了程式在不同平台上的可移植性。size_t是針對系統定製的一種資料類型,一般是整型(int),因為C/C++標準只定義一最低的位元,而不是必需的固定位元。
/* memset example */#include <iostream>#include <cstring>using namespace std;const int maxn = 11;int main (){ int arr[maxn], i; char str[] = "almost every programmer should know memset!"; memset(str, '-', 6); cout << str << endl; memset(arr,10,maxn*sizeof(int)); for(i=0; i<maxn; i++) { cout << arr[i] << " "; } cout << endl; memset(arr,-1,maxn*sizeof(int)); for(i=0; i<maxn; i++) { cout << arr[i] << " "; } cout << endl; return 0;}
問題來了: 對於上例子中, 對char 型數組的初始化, 輸出符合預期。 但是對於整型數組卻不符合預期, 為什麼呢。 答案就是memset 是對於memory block 是一個位元組一個位元組的賦值。 由於int 型數組一個int元素就有4個bytes, 當然不符合預期了。 相信改為0後就可以了, 另外改為-1 也可以:
結果如下:
百度筆試的一道智力推斷題
的確是老題,不過可惜你把題抄錯了。你這道題的答案是:他倆誰也不可能知道。
但原題是:
P先生、Q先生都具有足夠的推理能力。
這天,他們正在接受推理面試。
他們知道桌子的抽屜裡有如下16張撲克牌:
紅桃 A、Q、4
黑桃 J、8、4、2、7、3
草花 K、Q、5、4、6
方塊 A、5
約翰教授從這16張牌中挑出一張牌來,並把這張牌的點數告訴P先生,把這張牌的花色告訴Q先生。
這時,約翰教授問P先生和Q先生:你們能從已知的點數或花色中推知這張牌是什麼牌嗎?
P先生:"我不知道這張牌。"——P1
Q先生:"我知道你不知道這張牌。"——Q1
P先生:"現在我知道這張牌了。" ——P2
Q先生:"我也知道了。" ——Q2
請問:這張牌是什麼牌?
1、設P先生為“行”所說的話為P1,P2;Q先生為“列”所說的話為Q1,Q2。牌的點數為N。
2、由P1——N在行中不唯一,是一個重複的數字。即藍線所畫的幾個(A,4,5,Q)
3、由Q1——N不在行能直接確定的列中,即在是(方塊,紅桃)
4、由P2——N≠A,N=(4,5,Q)
5、由Q2——N在列內不重複。N=5(方塊)。
所以“行”“列”都知道了,大家也都知道了。
一道JAVA面試筆試題,很經典的
public A(int radio){
this.radio = radio;
System.out.println("A construt");
}
本質上是:
public A(int radio){
super();//必須在這個位置
this.radio = radio;
System.out.println("A construt");
}
看出了了吧 如果你這樣寫:
public A(int radio){
this.radio = radio;
super();
System.out.println("A construt");
}
如果可以執行的話 A.draw(),radio=1
當然 這個假設根本不成立,因為super()必須處於首句。
歡迎加入討論群: 75549598