?答案為自己整理的,歡迎批評指正。
公共題
選擇題(每題5分)
1. 若一棵二叉樹具有10個度為2的結點,則該二叉樹的度為0的結點個數是(???? ?)
A:9????B:11? ???C:12???? D:不確定?
?
2.下列排序算法中,其時間復雜度和記錄的初始排列無關的是(????? )
A:插入排序?(預先排序,運行時間為O(N)) ???B:堆排序? ???C:快速排序? (最壞情形O(N2))? D:冒泡排序?(最壞情形O(N2), 最優O(N))
3.已知中序遍歷的序列為abcdef,高度最小的可能的二叉樹的葉子是(???? )
A:ace??????B:acf????????C:adf? ??????D:cdf?
4.參加百年阿里培訓的n位同學結伴去西湖旁邊為游人指路,兩人一組,他們打算先讓體重之和恰好為102公斤的同學一組,請給出一個算法找到這樣的組合,或者確定他們中不存在這樣的組合,其中最優的算法時間復雜度為?(假設體重均為整數) (???? )
A:O(log(n))????B:O(n)? ????C:O(n log(n))? ??D:O(n^2)
?
5.眾所周知數據結構中非?;镜臉浣Y構包括二叉查找樹(BST)。當我們把如下序列:10,5,19,4,13,7,6,3,1按順序建立一棵BST時,樹的最大深度是?(令根節點深度為0,執行不進行平衡的基本插入) (???? )
A:5????B:4????C:3? ???D:2?
?
6.阿里巴巴啟用了新的辦公大廈,這里的一切都充滿了現代感;工程師們打算在娛樂區用大小相等的圓形材料分割出一些空間,使用A,B,C三個圓形材料,最多可以將空間分為八個區域(包括圓形以外的區域),如果給你五個圓形材料,你最多可以幫助工程師們分出多少個空間? (???? )
A:20????B:22? ????C:26? ???D:32?
?
綜合題(每題15分)
1)?分析MergeSort的原理以及算法復雜度,并用最擅長的編程語言實現Merge Sort。
MergeSort利用分治法的原理,依次減小問題的規模。時間復雜度為O(nlog(n)), 空間復雜度為O(N);
[cpp]?view plaincopy
void?Mergesort(int?*p,?int?n)?? {?? ????void?Msort(int?*p,?int?*temp,?int?left,?int?right);?? ????int?*temp;?? ????if(n?<=?0?||?p?==?NULL)?? ????????return;?? ????temp?=?(int?*)malloc(sizeof(int)?*?n);?? ????if(temp?==?NULL)?? ????????return;?? ????Msort(p,?temp,?0,?n-1);??? ????free(temp);?? }?? ?? void?Msort(int?*p,?int?*temp,?int?left,?int?right)?? {?? ????void?Merge(int?*p,?int?*temp,?int?left,?int?rightbegin,?int?right);?? ????int?leftend?=?(right?+?left)/2;?? ????int?rightbegin?=?leftend+1;?? ????if(left?<?right)?? ????{?? ????????Msort(p,?temp,?left,?leftend);?? ????????Msort(p,?temp,?rightbegin,?right);?? ????????Merge(p,?temp,?left,?rightbegin,?right);?? ????}?? }?? ?? void?Merge(int?*p,?int?*temp,?int?left,?int?rightbegin,?int?right)?? {?? ????int?TempArray?=?rightbegin;?? ????int?pos?=?left;?? ????int?begin?=?left;?? ????while(left?<?TempArray?&&?rightbegin?<=?right)?? ????{?? ????????if(p[left]?<=?p[rightbegin])?? ????????{?? ????????????temp[pos++]?=?p[left++];?? ????????}?? ????????else?if(p[left]?>?p[rightbegin])?? ????????{?? ????????????temp[pos++]?=?p[rightbegin++];?? ????????}?? ????}?? ????while(left?<?TempArray)?? ????????temp[pos++]?=?p[left++];?? ????while(rightbegin?<=?right)?? ????????temp[pos++]?=?p[rightbegin++];?? ????while(pos--?>=?begin)?? ????{?? ????????p[pos]?=?temp[pos];?? ????}?? }??
給定一個數t,以及n個整數,在這n個數中找到加和為t的所有組合,例如t = 4, n = 6,這6個數為?[4, 3, 2, 2,?1, 1],這樣輸出就有4個不同的組合它們的加和為4: 4, 3+1, 2+2, and 2+1+1.??請設計一個高效算法實現這個需求。
[cpp]?view plaincopy
#include<iostream>?? #include<vector>?? ?? using?namespace?std;?? ?? void?Find(int?*p,?int?n,?int?sum);?? void?Qsort(int?*p,?int?n);?? ?? int?main()?? {?? ????int?*p;?? ????int?n;?? ????int?sum;?? ????cin>>n;?? ????p?=?new?int[n];?? ????for(int?i?=?0;?i?<?n;?++i)?? ????????cin>>p[i];?? ????cin>>sum;?? ????Qsort(p,?n);??? ????for(int?i?=?0;?i?<?n;?++i)?? ????????cout<<p[i]<<"?";?? ????cout<<endl;?? ????Find(p,?n,?sum);?? }?? ?? void?Find(int?*p,?int?n,?int?sum)?? {?? ????void?FindSum(int?*p,?int?n,?int?sum,?vector<int>?&vec);?? ????vector<int>?vec;?? ????if(p?==?NULL?||?n?<?0)?? ????????return;?? ????if(sum?<?p[0])?? ????????return;?? ????else?? ????????FindSum(p,?n,?sum,vec);?? }?? ?? void?FindSum(int?*p,?int?n,?int?sum,?vector<int>?&vec)?? {?? ????if(sum?==?0)?? ????{?? ????????for(vector<int>::iterator?iter?=?vec.begin();?iter?!=?vec.end();?++iter)?? ????????????cout<<*iter<<"?";?? ????????cout<<endl;?? ????????return?;?? ????}?? ????if(sum?<?*p?||?n?<?0)?? ????{?? ????????return;?? ????}?? ????vec.push_back(*p);?? ????sum?-=?*p;?? ????FindSum(p+1,?n-1,?sum,?vec);?? ????sum?+=?*p;?? ????vec.pop_back();?? ????while(*p?==?*(p+1)?&&?p?<?p+n)??? ????????p++;?? ????FindSum(p+1,?n-1,?sum,?vec);?? }?? ?? void?Qsort(int?*p,?int?n)?? {?? ????void?swap(int?*,?int?*);?? ????int?pivot;?? ????int?j?=?-1;?? ????if(n?<=?1)?? ????????return;?? ????pivot?=?p[n/2];?? ????swap(p+n/2,?p+n-1);?? ????for(int?i?=?0;?i?<?n-1;?++i)?? ????{?? ????????if(p[i]?<?pivot)?? ????????{?? ????????????j++;?? ????????????if(j?!=?i)?? ????????????{?? ????????????????swap(p+i,?p+j);?? ????????????}?? ????????}?? ????}?? ????swap(p+j+1,?p+n-1);?? ????Qsort(p,?j+1);?? ????Qsort(p+j+2,?n-j-2);?? }?? ?? void?swap(int?*a,?int?*b)?? {?? ????int?temp;?? ????temp?=?*a;?? ????*a?=?*b;?? ????*b?=?temp;?? }??
熱點題?聊聊近期最吸引你的互聯網事件,談談你對此事件的看法。
C&C++部分
選擇題(每題5分)
1、int main(void)
{
? int count=0; int m=779;
? while(m)
? {count++;? m=m&(m-1);}
? printf("%d\n",count); return0;
}
請問最終輸出的count值為(????)?????????A:?3???? ?B:4?? ???C:5???? ?D:8
?
2、在32位操作系統中,我們定義如下變量
int (*n)[10];
請問調用函數sizeof(n),返回值為(???? )??A:4?????B:40? ???C:8 ????D:80
?
3、int main(void)
{
? int i=1;? int j=i++;
? if((i++>++j) && (++i == j))i+=j;
? printf("%d\n",i);?return 0;
}
請問最終輸出的i值為(????)?????????????A:?2?? ??B:3??????C:4???? ?D:5
?
4、以下敘述中正確的是(???? )
A:可以在一個函數中定義另一個函數?????B:main()函數必須放在其他函數之前
C:構成C++語言程序的基本單位是類?????D:所有被調用的函數一定要在調用之前進行定義
?
綜合題(每題15分)
有10億個數,這些數的值都在0~1000萬之內。請使用定義一個數據結構實現這些數字的存儲,并實現函數get_bigger_count( unsigned value ),輸入一個值value,返回這10億個數中比value值大的數的數目。
要求:不能使用STL,請盡量考慮性能與資源的占用。??
思路:創建一個包含1000萬個元素的數組,然后遍歷10億個數字,數組用來統計對應數字出現的次數。
如果10億個數字中0~1000萬是隨機出現的,可以滿足需求。如果有一個數字出現的次數非常的,則數組可能溢出。 ?
總結
以上是生活随笔為你收集整理的2011阿里巴巴集团实习生招聘笔试题 CC++的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。