C++笔试题库之编程、问答题 100~150道(三)

2014-11-24 00:54:37 · 作者: · 浏览: 54
”?


流操作符(<<、>>)和赋值操作符(=)的返回值、拷贝构造函数的参数、赋值操作符的参数、其它情况都推荐使用引用。



135.面向对象的三个基本特征,并简单叙述之?



1. 封装:将客观事物抽象成类,每个类对自身的数据和方法实行protection(private, protected,public)



2. 继承:广义的继承有三种实现形式:实现继承(指使用基类的属性和方法而无需额外编码的能力)、可视继承(子窗体使用父窗体的外观和实现代码)、接口继承(仅使用属性和方法,实现滞后到子类实现)。前两种(类继承)和后一种(对象组合=>接口继承以及纯虚函数)构成了功能复用的两种方式。



3. 多态:是将父对象设置成为和一个或更多的他的子对象相等的技术,赋值之后,父对象就可以根据当前赋值给它的子对象的特性以不同的方式运作。简单的说,就是一句话:允许将子类类型的指针赋值给父类类型的指针。




136.求下面函数的返回值(微软)



int func(x)


{


int countx = 0;


while(x)


{


countx ++;


x = x&(x-1);


}


return countx;


}



假定x = 9999。 答案:8


思路:将x转化为2进制,看含有的1的个数。



137、写出下列代码的输出内容


#i nclude


int inc(int a)


{


return(++a);


}


int multi(int*a,int*b,int*c)


{


return(*c=*a**b);


}


typedef int(FUNC1)(int in);


typedef int(FUNC2) (int*,int*,int*);


void show(FUNC2 fun,int arg1, int*arg2)


{


INCp=&inc;


int temp =p(arg1);


fun(&temp,&arg1, arg2);


printf(“%d\n”,*arg2);


}


main()


{


int a;


show(multi,10,&a);


return 0;


}


答:110


138。编写一个 C 函数,该函数在一个字符串中找到可能的最长的子字符串,且该字符串是由同一字符组成的。


char * search(char *cpSource, char ch)


{


char *cpTemp=NULL, *cpDest=NULL;


int iTemp, iCount=0;


while(*cpSource)


{


if(*cpSource == ch)


{


iTemp = 0;


cpTemp = cpSource;


while(*cpSource == ch)


++iTemp, ++cpSource;


if(iTemp > iCount)


iCount = iTemp, cpDest = cpTemp;


if(!*cpSource)


break;


}


++cpSource;


}


return cpDest;


}



139。请编写一个 C 函数,该函数在给定的内存区域搜索给定的字符,并返回该字符所在位置索引值。


int search(char *cpSource, int n, char ch)


{


int i;


for(i=0; i

return i;


}


140.一个单向链表,不知道头节点,一个指针指向其中的一个节点,问如何删除这个指针指向的节点?


将这个指针指向的next节点值copy到本节点,将next指向next->next,并随后删除原next指向的节点。


141、用指针的方法,将字符串“ABCD1234efgh”前后对调显示


#i nclude


#i nclude


#i nclude


int main()


{


char str[] = “ABCD1234efgh”;


int length = strlen(str);


char * p1 = str;


char * p2 = str + length – 1;


while(p1 < p2)


{


char c = *p1;


*p1 = *p2;


*p2 = c;


++p1;


–p2;


}


printf(“str now is %s\n”,str);


system(“pause”);


return 0;


}



142、有一分数序列:1/2,1/4,1/6,1/8……,用函数调用的方法,求此数列前20项的和


#i nclude


double getValue()


{


double result = 0;


int i = 2;


while(i < 42)


{


result += 1.0 / i;//一定要使用1.0做除数,不能用1,否则结果将自动转化成整数,即0.000000


i += 2;


}


return result;


}


int main()


{


printf(“result is %f\n”, getValue());


system(“pause”);


return 0;


}


143、有一个数组a[1000]存放0–1000;要求每隔二个数删掉一个数,到末尾时循环至开头继续进行,求最后一个被删掉的数的原始下标位置。


以7个数为例:


{0,1,2,3,4,5,6,7} 0–>1–>2(删除)–>3–>4–>5(删除)–>6–>7–>0(删除),如此循环直到最后一个数被删除。


方法1:数组


#include


using namespace std;


#define null 1000


int main()


{


int arr[1000];


for (int i=0;i<1000;++i)


arr[i]=i;


int j=0;


int count=0;


while(count<999)


{


while(arr[j%1000]==null)


j=(++j)%1000;


j=(++j)%1000;


while(arr[j%1000]==null)


j=(++j)%1000;


j=(++j)%1000;


while(arr[j%1000]==null)


j=(++j)%1000;


arr[j]=null;


++count;


}


while(arr[j]==null)


j=(++j)%1000;


cout<

return 0;


}


方法2:链表


#i nclude


using namespace std;


#define null 0


struct node


{


int data;


node* next;


};


int main()


{


node* head=new node;


head->data=0;


head->next=null;


node* p=head;


for(int i=1;i<1000;i++)


{


node* tmp=new node;


tmp->data=i;


tmp->next=null;


head->next=tmp;


head=head->next;


}


head->next=p;


while(p!=p->next)


{


p->next->next=p->next->next->next;


p=p->next->next;


}


cout<data;


return 0;


}



方法3:通用算法


#i nclude


#define MAXLINE 1000 //元素个数


/*


MAXLINE 元素个数


a[] 元素数组


R[] 指针场


suffix 下标


index 返回最后的下标序号


values 返回最后的下标对应的值


start 从第几个开始


K 间隔


*/


int find_n(int a[],int R[],int K,int& index,int& values,int s=0) {


int suffix;


int front_node,current_node;


suffix=0;


if(s==0) {


current_node=0;


front_node=MAXLINE-1;


}


else {


current_node=s;


front_node=s-1;


}


while(R[front_node