《各大公司招聘考试算法笔试题附答案》由会员分享,可在线阅读,更多相关《各大公司招聘考试算法笔试题附答案(17页珍藏版)》请在金锄头文库上搜索。
1、各大公司招聘考试算法笔试题含答案1、将一整数逆序后放入一数组中(要求递归实现)void convert(int *result, int n) if(n=10)convert(result+1, n/10);*result = n%10;int main(int argc, char* argv) int n = 123456789, result20=;convert(result, n);printf(%d:, n);for(int i=0; i= average)printf(%d:%dn, number, score);return average; else printf(Avera
2、ge=%dn, total/n);return total/n;int main(int argc, char* argv) find(0, 0);3、递归实现回文判断(如:abcdedbca就是回文,判断一个面试者对递归理解的简单程序)int find(char *str, int n) if(n=n; i-) resultj = *source+;resultj+1 = 0;find(source, result, n-1);int main(int argc, char* argv) int const n = 3;char *source = ABCDE, resultn+1 = 0;
3、if(n0 & strlen(source)0 & nn) while(m%n != 0) n+;m /= n;prim(m, n);printf(%d*, n);int main(int argc, char* argv) int n = 435234;printf(%d=, n);prim(n, 2);6、寻找迷宫的一条出路,o:通路; X:障碍。(大家经常谈到的一个小算法题)#define MAX_SIZE 8int H4 = 0, 1, 0, -1; int V4 = -1, 0, 1, 0; char MazeMAX_SIZEMAX_SIZE = X,X,X,X,X,X,X,X, o
4、,o,o,o,o,X,X,X, X,o,X,X,o,o,o,X, X,o,X,X,o,X,X,o, X,o,X,X,X,X,X,X,X,o,X,X,o,o,o,X, X,o,o,o,o,X,o,o, X,X,X,X,X,X,X,X;void FindPath(int X, int Y) if(X = MAX_SIZE | Y = MAX_SIZE) for(int i = 0; i MAX_SIZE; i+)for(int j = 0; j MAX_SIZE; j+) printf(%c%c, Mazeij, j MAX_SIZE-1 ? : n);else for(int k = 0; k
5、= 0 & Y = 0 & Y MAX_SIZE & X MAX_SIZE & o = MazeXY) MazeXY = ; FindPath(X+Vk, Y+Hk); MazeXY =o; int main(int argc, char* argv) FindPath(1,0);7、随机分配座位,共50个学生,使学号相邻的同学座位不能相邻(早些时候用C#写的,没有用C改写)。static void Main(string args)int Tmp = 0, Count = 50;int Seats = new intCount;bool Students = new boolCount;Sy
6、stem.Random RandStudent=new System.Random();StudentsSeats0=RandStudent.Next(0,Count)=true;for(int i = 1; i Count; ) Tmp=(int)RandStudent.Next(0,Count); if(!StudentsTmp)&(Seatsi-1-Tmp!=1) & (Seatsi-1 - Tmp) != -1) Seatsi+ = Tmp;StudentsTmp = true;foreach(int Student in Seats) System.Console.Write(Stu
7、dent + );System.Console.Read();8、求网格中的黑点分布。现有6*7的网格,在某些格子中有黑点,已知各行与各列中有黑点的点数之和,请在这张网格中画出黑点的位置。(这是一网友提出的题目,说是他笔试时遇到算法题)#define ROWS 6#define COLS 7int iPointsRROWS = 2, 0, 4, 3, 4, 0; / 各行黑点数和的情况int iPointsCCOLS = 4, 1, 2, 2, 1, 2, 1; / 各列黑点数和的情况int iCount, iFound;int iSumRROWS, iSumCCOLS, GridROWSCO
8、LS;int Set(int iRowNo) if(iRowNo = ROWS) for(int iColNo=0; iColNo COLS & iSumCiColNo=iPointsCiColNo; iColNo+) if(iColNo = COLS-1) printf(nNo.%d:n, +iCount); for(int i=0; i ROWS; i+) for(int j=0; j COLS; j+) printf(%d%c, Gridij, (j+1) % COLS ? : n); iFound = 1;/ iFound = 1,有解 else for(int iColNo=0; i
9、ColNo COLS; iColNo+) if(iPointsRiRowNo = 0) Set(iRowNo + 1); else if(GridiRowNoiColNo=0) GridiRowNoiColNo = 1; iSumRiRowNo+; iSumCiColNo+; if(iSumRiRowNoiPointsRiRowNo & iSumCiColNo=iPointsCiColNo) Set(iRowNo);else if(iSumRiRowNo=iPointsRiRowNo & iRowNo ROWS) Set(iRowNo + 1); GridiRowNoiColNo = 0; iSumRiRowNo-; iSumCiColNo-; return iFound; / 用于判断是否有解int main(int argc,