对数组,指针,数据结构,算法,字符串,文件*作等问题都有覆盖.主要以c语言的实现为主,也有c++的题.大家可以先做做这10道题,测试一下自己的水平.
1.下面这段代码的输出是多少(在32位机上).
char*p;
char*q[20];
char*m[20][20];
int(*n)[10];
structmystruct
{
chardda;
doubledda1;
inttype;
};
mystructk;
printf("%d%d%d%d",sizeof(p),sizeof(q),sizeof(m),sizeof(n),sizeof(k));
*:4,80,1600,4,24
n是指向一维数组的指针变量;k中不要忘了考虑对齐问题,这里dda为4个字节。
2.
(1)
chara[2][2][3]={{{1,6,3},{5,4,15}},{{3,5,33},{23,12,7}}};
for(inti=0;i
printf("%d",_______);
在空格处填上合适的语句,顺序打印出a中的数字
*:a[i/6][(i/3)%2][i%3];这道题目是多维数组的输出问题,这里要考虑的是每维数字的取值顺序问题:第一维,前六次循环都取0,后六次取1,于是i/6可以满足要求;第二维,前3次为0,再3次为1,再3次为0,再3次为1,用量化的思想,i/3把12个数字分为4组每组3个,量化为0、1、2、3,为要得到0、1、0、1我们这里就需要对(0、1、2、3)%2=(0、1、0、1),于是(i/3)%2;最后一维我们需要的是(0、1、2;0、1、2;0、1、2;0、1、2;)我们就i%3。
(2)
char**p,a[16][8];
问:p=a是否会导致程序在以后出现问题?为什么?
*:这个不会导致出现问题,但是要注意p的使用,如a[1][2]等价的为*(*(p+1)+2)而不是*(p+11),
3.用递归方式,非递归方式写函数将一个字符串反转.
函数原型如下:char*reverse(char*str);
*:
非递归方式:
char*reverse(char*str)
{
intlen=strlen(str);
chartemp;
for(inti=0;i
{
temp=*(str+i);
*(str+i)=*(str+len-1-i);
*(str+len-1-i)=temp;
}
returnstr;
}
递归方式:???
4.strcpy函数和memcpy函数有什么区别?它们各自使用时应该注意什么问题?
*:strcpy是字符串拷贝,遇\0则停。
memcpy是内存拷贝,要指定拷贝的长度。
当要拷贝二进制数据(比如说一个结构),只能用memcpy
5.写一个函数将一个链表逆序.
一个单链表,不知道长度,写一个函数快速找到中间节点的位置.
写一个函数找出一个单向链表的倒数第n个节点的指针.(把能想到的最好算法写出).
*:
把一个链表中的接点顺序倒排
typedefstructlinknode
{
intdata;
structlinknode*next;
}node;
//将一个链表逆置
node*reverse(node*head)
{
node*p,*q,*r;
p=head;
q=p->next;
while(q!=null)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
head->next=null;
head=p;
returnhead;
}
6.用递归算法判断数组a[n]是否为一个递增数组。
7.
有一个文件(名为a.txt)如下,每行有4项,第一项是他们的名次,写一个c程序,将五个人的名字打印出来.并按名次排序后将5行数据仍然保存到a.txt中.使文件按名次排列每行.
2,07010188,0711,李镇豪,
1,07010154,0421,陈亦良,
3,07010194,0312,凌瑞松,
4,07010209,0351,罗安祥,
5,07010237,0961,黄世传,
8.写一个函数,判断一个unsignedchar字符有几位是1.
写一个函数判断计算机的字节存储顺序是升序(little-endian)还是降序(big-endian).
9.微软的笔试题.
implementastringclassinc++withbasicfunctionalitylikeparison,concatenation,inputandoutput.pleasealsoprovidesometestcasesandusingscenarios(samplecodeofusingthisclass).
pleasedonotusemfc,stlandotherlibrariesinyourimplementation.
10.有个数组a[100]存放了100个数,这100个数取自1-99,且只有两个相同的数,剩下的98个数不同,写一个搜索算法找出相同的那个数的值.(注意空间效率时间效率尽可能要低).
这十道题还是能够看出自己的水平如何的.如果你能不假思索地做出这10道题,估计去国外大公司是没有问题了,呵呵.
*我在整理中,以后陆续发布.................
下面有些题也不错,可以参考.
1.下面的代码输出是什么,为什么?
voidfoo(void)
{
unsignedinta=6;
intb=-20;
(a+b>6)?puts(">6"):puts("
}
输出>6.
就是考察隐式转换.int型变量转化成unsignedint,b成了正数.
2.b)运行下面的函数会有什么结果?为什么?
voidfoo(void)
{
charstring[10],str1[10];
inti;
for(i=0;i
{
str1=a;
}
strcpy(string,str1);
printf("%s",string);
}
首先搞清strcpy函数的实现方法,
char*strcpy(char*strdest,constchar*strsrc)
{
if((strdest==null)||(strsrc==null))
throw"invalidargument(s)";
char*strdestcopy=strdest;
while((*strdest++=*strsrc++)!=\0);
returnstrdestcopy;
}
由于str1末尾没有‘\0’结束标志,所以strcpy不知道拷贝到何时结束.
printf函数,对于输出char*类型,顺序打印字符串中的字符直到遇到空字符('\0')或已打印了由精度指定的字符数为止.
下面是微软的两道笔试题....
3.implementastringclassinc++withbasicfunctionalitylikeparison,concatenation,inputandoutput.pleasealsoprovidesometestcasesandusingscenarios(samplecodeofusingthisclass).
pleasedonotusemfc,stlandotherlibrariesinyourimplementation.
我的实现方案如下,这道题真地对c++的主要特*都进行了较好地考察.
string.h:
#ifndefstring_h
#definestring_h
#include
usingnamespacestd;
classstring{
public:
string();
string(intn,charc);
string(constchar*source);
string(conststring&s);
//string&operator=(char*s);
string&operator=(conststring&s);
~string();
char&operator[](inti){returna;}
constchar&operator[](inti)const{returna;}//对常量的索引.
string&operator+=(conststring&s);
intlength();
friendistream&operator>>(istream&is,string&s);//搞清为什么将>>设置为友元函数的原因.
//friendbooloperator
friendbooloperator>(conststring&left,conststring&right);//下面三个运算符都没必要设成友元函数,这里是为了简单.
friendbooloperator==(conststring&left,conststring&right);
friendbooloperator!=(conststring&left,conststring&right);
private:
char*a;
intsize;
};
#endif
string.cpp:
#include"string.h"
#include
#include
string::string(){
a=newchar[1];
a[0]=\0;
size=0;
}
string::string(intn,charc){
a=newchar[n+1];
memset(a,c,n);
a[n]=\0;
size=n;
}
string::string(constchar*source){
if(source==null){
a=newchar[1];
a[0]=\0;
size=0;
}
else
{size=strlen(source);
a=newchar[size+1];
strcpy(a,source);
}
}
string::string(conststring&s){
size=strlen(s.a);//可以访问私有变量.
a=newchar[size+1];
//if(a==null)
strcpy(a,s.a);
}
string&string::operator=(conststring&s){
if(this==&s)
return*this;
else
{
[]a;
size=strlen(s.a);
a=newchar[size+1];
strcpy(a,s.a);
return*this;
}
}
string::~string(){
[]a;//
}
string&string::operator+=(conststring&s){
intj=strlen(a);
intsize=j+strlen(s.a);
char*tmp=newchar[size+1];
strcpy(tmp,a);
strcpy(tmp+j,s.a);
[]a;
a=tmp;
return*this;
}
intstring::length(){
returnstrlen(a);
}
main.cpp:
#include
#include"string.h"
usingnamespacestd;
booloperator==(conststring&left,conststring&right)
{
inta=strcmp(left.a,right.a);
if(a==0)
returntrue;
else
returnfalse;
}
booloperator!=(conststring&left,conststring&right)
{
return!(left==right);
}
ostream&operator
intlength=s.length();
for(inti=0;i
//os
os
returnos;
}
stringoperator+(conststring&a,conststring&b){
stringtemp;
temp=a;
temp+=b;
returntemp;
}
booloperator
intj=0;
while((left[j]!=\0)&&(right[j]!=\0)){
if(left[j]
returntrue;
else
{
if(left[j]==right[j]){
j++;
continue;
}
else
returnfalse;
}
}
if((left[j]==\0)&&(right[j]!=\0))
returntrue;
else
returnfalse;
}
booloperator>(conststring&left,conststring&right)
{inta=strcmp(left.a,right.a);
if(a>0)
returntrue;
else
returnfalse;
}
istream&operator>>(istream&is,string&s){
[]s.a;
s.a=newchar[20];
intm=20;
charc;
inti=0;
while(is.get(c)&&isspace(c));
if(is){
do{s.a=c;
i++;
if(i==m-1){
s.a=\0;
char*b=newchar[m];
strcpy(b,s.a);
m=m*2;
s.a=newchar[m];
strcpy(s.a,b);
[]b;
}
}
while(is.get(c)&&!isspace(c));
//如果读到空白,将其放回.
if(is)
is.unget();
}
s.size=i;
s.a=\0;
returnis;
}
intmain(){
stringa="abcd";
stringb="";
//stringc(6,b);这么写不对.
stringc(6,l);
stringd;
stringe=a;//abcd
stringf;
cin>>f;//需要输入...
stringg;
g=a+b;//abcd
if(a
cout
else
cout=b"
if(e==a)
cout
else
cout
b+=a;
cout
cout
cout
cout
cout
cout
cout
cout
return0;
}
4.implementasingle-directionlinkedlistsortingalgorithm.pleasefirstdefinethedatastructureoflinkedlistandthenimplementthesortingalgorithm.
5.编写一个函数,返回两个字符串的最大公串!例如,“adbccadebbca”和“edabccadece”,返回“ccade”
10.有个数组a[100]存放了100个数,这100个数取自1-99,且只有两个相同的数,剩下的98个数不同,写一个搜索算法找出相同的那个数的值.(注意空间效率时间效率尽可能要低).*:先把a[100]中所有数加起来得到sum1,然后计算1-99之和sum2=(1+99)*99/2,那么相同的那个数是b=sum1-sum2
10.有个数组a[100]存放了100个数,这100个数取自1-99,且只有两个相同的数,剩下的98个数不同,写一个搜索算法找出相同的那个数的值.(注意空间效率时间效率尽可能要低).*:先把a[100]中所有数加起来得到sum1,然后计算1-99之和sum2=(1+99)*99/2,那么相同的那个数是b=sum1-sum2