第一部分
四十道选择题基础知识,比较BAT的笔试题,相对难度低一点。
第二部分
两道编程题
1,题目:
在一个二维数组中,每一行都按照从左到右递减的顺序排序,每一列都按照从上到下递减的顺序排序。输入这样的一个二维数组和一个整数,判断数组中是否含有该整数,如有输出整数的位置。
2,题目:选镇长,镇子里有N个人,要选出一个镇长,镇长要求:镇长认识所有人,所有人都认识镇长,编程实现选出镇长。
题目一:
基本思想:
首先选取数组中右上角的数字。如果=要找的数字,直接返回。如果小于要找的数字,剔除这个数字所在的列;如果大于要找的数字,剔除这个数字所在的行。
#include <iostream> using namespace std; void find(int a[][4],int rows,int columns,int number) { if(rows > 0 && columns > 0) { int row = 0,column = columns -1; while(row < rows && column >=0) { if(a[row][column] == number) { cout<<row<<" "<<column<<endl; break; } else if(a[row][column] < number) --column; else ++row; } if(row == rows || column ==-1) cout<<"not exist."<<endl; } } void main() { int a[4][4]={ 15,11,8,6, 13,10,7,4, 12,9,4,2, 9,8,2,1}; find(a,4,4,100); //not exist. find(a,4,4,7); }
题目二:
基本思想:
将镇子里的人的认识关系用一个二维数组存储,a认识b,a->b为1,a不认识b,a->b为0。然后找到[a,a]位置,以此位置的行和列上的值都为1,说明a为镇长。
#include <iostream> using namespace std; void find(int a[][3],int persons)//假设镇子上共有3个人,3x3矩阵 { if(persons<=0) cout<<"input error."<<endl; for(int i=0;i<persons;i++) { int rowi=0,columni=0;//以i所在的行和列 int flag=1; for(;rowi<persons,columni<persons;rowi++,columni++) { if(a[rowi][i]==0 || a[i][columni]==0) { flag=0; break; } else continue; } if(flag==1) cout<<"镇长为:"<<i<<endl; } } void main() { int a[3][3]={ 1,1,0, 1,1,1, 0,1,1}; find(a,3); }