C++笔试题汇总二
UpperCase( str );
cout << str << endl;
4. 以下代码有什么问题?[C难]
void char2Hex( char c ) // 将字符以16进制表示
{
char ch = c/0x10 + '0'; if( ch > '9' ) ch += ('A'-'9'-1);
char cl = c%0x10 + '0'; if( cl > '9' ) cl += ('A'-'9'-1);
cout << ch << cl << ' ';
}
char str[] = "I love 中国";
for( size_t i=0; i<strlen(str); ++i )
char2Hex( str[i] );
cout << endl;
5. 以下代码有什么问题?[C++易]
struct Test
{
Test( int ) {}
Test() {}
void fun() {}
};
void main( void )
{
Test a(1);
a.fun();
Test b();
b.fun();
}
6. 以下代码有什么问题?[C++易]
cout << (true?1:"1") << endl;
7. 以下代码能够编译通过吗,为什么?[C++易]
unsigned int const size1 = 2;
char str1[ size1 ];
unsigned int temp = 0;
cin >> temp;
unsigned int const size2 = temp;
char str2[ size2 ];
8. 以下代码中的输出语句输出0吗,为什么?[C++易]
struct CLS
{
int m_i;
CLS( int i ) : m_i(i) {}
CLS()
{
CLS(0);
}
};
CLS obj;
cout << obj.m_i << endl;
9. C++中的空类,默认产生哪些类成员函数?[C++易]
答:
class Empty
{
public:
Empty(); // 缺省构造函数
Empty( const Empty& ); // 拷贝构造函数
~Empty(); // 析构函数
Empty& operator=( const Empty& ); // 赋值运算符
Empty* operator&(); // 取址运算符
const Empty* operator&() const; // 取址运算符 const
};
10. 以下两条输出语句分别输出什么?[C++难]
float a = 1.0f;
cout << (int)a << endl;
cout << (int&)a << endl;
cout << boolalpha << ( (int)a == (int&)a ) << endl; // 输出什么?
float b = 0.0f;
cout << (int)b << endl;
cout << (int&)b << endl;
cout << boolalpha << ( (int)b == (int&)b ) << endl; // 输出什么?
11. 以下反向遍历array数组的方法有什么错误?[STL易]
vector array;
array.push_back( 1 );
array.push_back( 2 );
array.push_back( 3 );
for( vector::size_type i=array.size()-1; i>=0; --i ) // 反向遍历array数组
{
cout << array[i] << endl;
}
12. 以下代码有什么问题?[STL易]
typedef vector IntArray;
IntArray array;
array.push_back( 1 );
array.push_back( 2 );
array.push_back( 2 );
array.push_back( 3 );
// 删除array数组中所有的2
for( IntArray::iterator itor=array.begin(); itor!=array.end(); ++itor )
{
if( 2 == *itor ) array.erase( itor );
}
13. 写一个函数,完成内存之间的拷贝。[考虑问题是否全面]
答:
void* mymemcpy( void *dest, const void *src, size_t count )
{
char* pdest = static_cast<char*>( dest );
const char* psrc = static_cast<const char*>( src );
if( pdest>psrc && pdest<psrc+cout ) 能考虑到这种情况就行了
{
for( size_t i=count-1; i!=-1; --i )
pdest[i] = psrc[i];
}
else
{
for( size_t i=0; i<count; ++i )
pdest[i] = psrc[i];
}
return dest;
}
int main( void )
{
char str[] = "0123456789";
mymemcpy( str+1, str+0, 9 );
cout << str << endl;
system( "Pause" );
return 0;
}
本试题仅用于考查C++/C程序员的基本编程技能。内容限于C++/C常用语法,不涉及数据结构、算法以及深奥的语法。考试成绩能反映出考生的编程质量以及对C++/C的理解程度,但不能反映考生的智力和软件开发能力。
笔试时间90分钟。请考生认真答题,切勿轻视。
一、请填写BOOL , float, 指针变量 与“零值”比较的 if 语句。(10分)
提示:这里“零值”可以是0, 0.0 , FALSE或者“空指针”。例如 int 变量 n 与“零值”比较的 if 语句为:
if ( n == 0 )
if ( n != 0 )
以此类推。
请写出 BOOL flag 与“零值”比较的 if 语句:
请写出 float x 与“零值”比较的 if 语句:
请写出 char *p 与“零值”比较的 if 语句:
二、以下为Windows NT下的32位C++程序,请计算sizeof的值(10分)
char str[] = “Hello” ;
char *p = str ;
int n = 10;
请计算
sizeof (str ) =
sizeof ( p ) =
sizeof ( n ) =
void Func ( char str[100])
{
请计算
sizeof( str ) =
}
void *p = malloc( 100 );
请计算
sizeof ( p ) =
三、简答题(25分)
1、头文件中的 ifndef/define/endif 干什么用?
2、#include 和 #include “filename.h” 有什么区别?
3、const 有什么用途?(请至少说明两种)
4、在C++ 程序中调用被 C编译器编译后的函数,为什么要加 extern “C”声明?
5、请简述以下两个for循环的优缺点
// 第一个
for (i=0; i++;)
{
if (condition)
DoSomething();
else
DoOtherthing();
}
// 第二个
if (condition)
{
for (i=0; i++;)
DoSomething();
}
else
{
for (i=0; i++;)
DoOtherthing();
}
优点:
缺点:
优点:
缺点:
四、有关内存的思考题(20分)
void GetMemory(char *p)
{
p = (char *)malloc(100);
}
void Test(void)
{
char *str = NULL;
GetMemory(str);
strcpy(str, "hello world");
printf(str);
}
请问运行Test函数会有什么样的结果?
答:
char *GetMemory(void)
{
char p[] = "hello world";
return p;
}
void Test(void)
{
char *str = NULL;
str = GetMemory();
printf(str);
}
请问运行Test函数会有什么样的结果?
答:
Void GetMemory2(char **p, int num)
{
*p = (char *)malloc(num);
}
void Test(void)
{
char *str = NULL;
GetMemory(&str, 100);
strcpy(str, "hello");
printf(str);
}
请问运行Test函数会有什么样的结果?
答:
void Test(void)
{
char *str = (char *) malloc(100);
strcpy(str, “hello”);
free(str);
if(str != NULL)
{
strcpy(str, “world”);
printf(str);
}
}
请问运行Test函数会有什么样的结果?
答:
五、编写strcpy函数(10分)
已知strcpy函数的原型是
char *strcpy(char *strDest, const char *strSrc);
其中strDest是目的字符串,strSrc是源字符串。
(1)不调用C++/C的字符串库函数,请编写函数 strcpy
(2)strcpy能把strSrc的内容复制到strDest,为什么还要char * 类型的返回值?
六、编写类String的构造函数、析构函数和赋值函数(25分)
已知类String的原型为:
class String
{
public:
String(const char *str = NULL); // 普通构造函数
String(const String &other); // 拷贝构造函数
~ String(void); // 析构函数
String & operate =(const String &other); // 赋值函数
private:
char *m_data; // 用于保存字符串
};
请编写String的上述4个函数。
文章出自http://www.netfetch.cn/
文章出自网魂工作室http://www.netfetch.cn/
附录C :C++/C试题的答案与评分标准
一、请填写BOOL , float, 指针变量 与“零值”比较的 if 语句。(10分)
请写出 BOOL flag 与“零值”比较的 if 语句。(3分)
标准答案:
if ( flag )
if ( !flag )
如下写法均属不良风格,不得分。
if (flag == TRUE)
if (flag == 1 )
if (flag == FALSE)
if (flag == 0)
请写出 float x 与“零值”比较的 if 语句。(4分)
标准答案示例:
const float EPSINON = 0.00001;
if ((x >= - EPSINON) && (x <= EPSINON)
不可将浮点变量用“==”或“!=”与数字比较,应该设法转化成“>=”或“<=”此类形式。
如下是错误的写法,不得分。
if (x == 0.0)
if (x != 0.0)
请写出 char *p 与“零值”比较的 if 语句。(3分)
标准答案:
if (p == NULL)
if (p != NULL)
如下写法均属不良风格,不得分。
if (p == 0)
if (p != 0)
if (p)
if (!)
二、以下为Windows NT下的32位C++程序,请计算sizeof的值(10分)
char str[] = “Hello” ;
char *p = str ;
int n = 10;
请计算
sizeof (str ) = 6 (2分)
sizeof ( p ) = 4 (2分)
sizeof ( n ) = 4 (2分)
void Func ( char str[100])
{
请计算
sizeof( str ) = 4 (2分)
}
void *p = malloc( 100 );
请计算
sizeof ( p ) = 4 (2分)
三、简答题(25分)
1、头文件中的 ifndef/define/endif 干什么用?(5分)
答:防止该头文件被重复引用。
2、#include 和 #include “filename.h” 有什么区别?(5分)
华为的C\C++面试题
Q1:请你分别划划OSI的七层网络结构图,和TCP/IP的五层结构图?
: Q2:请你详细的解释一下IP协议的定义,在哪个层上面,主要有什么作用?
: TCP与UDP呢?
: 总得来说前面两道题目还是比较简单的!
: Q3:请问交换机和路由器分别的实现原理是什么?分别在哪个层次上面实
: 现的?
: Q4:请问C++的类和C里面的struct有什么区别?
: Q5:请讲一讲析构函数和虚函数的用法和作用?
: Q6:全局变量和局部变量有什么区别?实怎么实现的?操作系统和编译器
: 是怎么知道的?
: Q7:一些寄存器的题目,我忘记了具体实什么题目,主要好像是寻址和内
: 存管理等一些知识,不记得了。
: Q8:8086是多少尉的系统?在数据总线上是怎么实现的?还有一些硬件方
: 面的知识我既不清楚了。
: 一般建议参加华为的研发面试的同学先要准备一下相关的知识,软件的主要
: 是看看C和数据结构方面的,硬件模电,数电和微机原理
两道c面试题
1、一个学生的信息是:姓名,学号,性别,年龄等信息,用一个链表,把这些学生信息连在一起, 给出一个age, 在些链表中删除学生年龄等于age的学生信息。
2、实现一个函数,把一个字符串中的字符从小写转为大写。
文章出自网魂工作室http://www.netfetch.cn/
C/C++面试题大汇总之微软亚洲技术中心面试题
2006年10月28日 更新
C/C++面试题大汇总之微软亚洲技术中心面试题
1.进程和线程的差别。
2.测试方法
3.Heap与stack的差别。
4.Windows下的内存是如何管理的?
5.介绍.Net和.Net的安全性。
6.客户端如何访问.Net组件实现Web Service?
7.C/C++编译器中虚表是如何完成的?
8.谈谈COM的线程模型。然后讨论进程内/外组件的差别。
9.谈谈IA32下的分页机制
10.给两个变量,如何找出一个带环单链表中是什么地方出现环的?
11.在IA32中一共有多少种办法从用户态跳到内核态?
12.如果只想让程序有一个实例运行,不能运行两个。像winamp一样,只能开一个窗口,怎样实现?
13.如何截取键盘的响应,让所有的‘a’变成‘b’?
14.Apartment在COM中有什么用?为什么要引入?
15.存储过程是什么?有什么用?有什么优点?
16.Template有什么特点?什么时候用?
17.谈谈Windows DNA结构的特点和优点。
18.网络编程中设计并发服务器,使用多进程与多线程 ,请问有什么区别?
更多相关C\C++面试题集、面试题请参阅这里:
最全的C\C++面试题集(C\C++试题和部分答案)
最全的C\C++面试题集(最全的C\C++试题集和答案)(续)
一道有趣的C#考试题目
Java面试题汇总及c/c++面试题(最新最全)
某公司招收.net高级程序员的试卷
.Net面试题集(最全最新)(欢迎加入更多.Net 面试题)
Google:21道能力倾向测试面试题
GOOGLE面试题
Google的面试题,你能做出多少?
据说是阿尔卡特(中国)的面试题目(C)
世界大公司面试题——微软的面试题
世界大公司面试题——微软的面试题(答案)
成为编程高手的基础素养
微软面试题——微软的智力题——三个灯泡
面试官揭秘500强面试题
[ur=article.asp?id=507l]国际大公司面试“决胜题”——各国名企面试“决胜题”[/url]
J2EE面试题集(附答案)
Java常见面试题集--面试题全面综合(一)
Java常见面试题集--面试题全面综合(二)
Java面试题集(欢迎大家踊跃提供)
更多相关面试试题
更多面试题请点击左侧的相关内容主题……
C++面试题集和答案,C++面试基础题,C++笔试题和答案
这个函数有太多的错误了,以至让人不知从何说起了:
1). ISR 不能返回一个值。如果你不懂这个,那么你不会被雇用的。
2). ISR 不能传递参数。如果你没有看到这一点,你被雇用的机会等同第一项。
3). 在许多的处理器/编译器中,浮点一般都是不可重入的。有些处理器/编译器需要让额处的寄存器入栈,有些处理器/编译器就是不允许在ISR中做浮点运算。此外,ISR应该是短而有效率的,在ISR中做浮点运算是不明智的。
4). 与第三点一脉相承,printf()经常有重入和性能上的问题。如果你丢掉了第三和第四点,我不会太为难你的。不用说,如果你能得到后两点,那么你的被雇用前景越来越光明了。
代码例子(Code examples)
12 . 下面的代码输出是什么,为什么?
void foo(void)
{
unsigned int a = 6;
int b = -20;
(a+b > 6) puts("> 6") : puts("<= 6");
}
这个问题测试你是否懂得C语言中的整数自动转换原则,我发现有些开发者懂得极少这些东西。不管如何,这无符号整型问题的答案是输出是“>6”。原因是当表达式中存在有符号类型和无符号类型时所有的操作数都自动转换为无符号类型。 因此-20变成了一个非常大的正整数,所以该表达式计算出的结果大于6。这一点对于应当频繁用到无符号数据类型的嵌入式系统来说是丰常重要的。如果你答错了这个问题,你也就到了得不到这份工作的边缘。
13. 评价下面的代码片断:
unsigned int zero = 0;
unsigned int compzero = 0xFFFF;
/*1's complement of zero */
对于一个int型不是16位的处理器为说,上面的代码是不正确的。应编写如下:
unsigned int compzero = ~0;
这一问题真正能揭露出应试者是否懂得处理器字长的重要性。在我的经验里,好的嵌入式程序员非常准确地明白硬件的细节和它的局限,然而PC机程序往往把硬件作为一个无法避免的烦恼。
到了这个阶段,应试者或者完全垂头丧气了或者信心满满志在必得。如果显然应试者不是很好,那么这个测试就在这里结束了。但如果显然应试者做得不错,那么我就扔出下面的追加问题,这些问题是比较难的,我想仅仅非常优秀的应试者能做得不错。提出这些问题,我希望更多看到应试者应付问题的方法,而不是答案。不管如何,你就当是这个娱乐吧…
动态内存分配(Dynamic memory allocation)
14. 尽管不像非嵌入式计算机那么常见,嵌入式系统还是有从堆(heap)中动态分配内存的过程的。那么嵌入式系统中,动态分配内存可能发生的问题是什么?
这里,我期望应试者能提到内存碎片,碎片收集的问题,变量的持行时间等等。这个主题已经在ESP杂志中被广泛地讨论过了(主要是 P.J. Plauger, 他的解释远远超过我这里能提到的任何解释),所有回过头看一下这些杂志吧!让应试者进入一种虚假的安全感觉后,我拿出这么一个小节目:下面的代码片段的输出是什么,为什么?
char *ptr;
if ((ptr = (char *)malloc(0)) == NULL)
puts("Got a null pointer");
else
puts("Got a valid pointer");
这是一个有趣的问题。最近在我的一个同事不经意把0值传给了函数malloc,得到了一个合法的指针之后,我才想到这个问题。这就是上面的代码,该代码的输出是“Got a valid pointer”。我用这个来开始讨论这样的一问题,看看被面试者是否想到库例程这样做是正确。得到正确的答案固然重要,但解决问题的方法和你做决定的基本原理更重要些。
Typedef
15. Typedef 在C语言中频繁用以声明一个已经存在的数据类型的同义字。也可以用预处理器做类似的事。例如,思考一下下面的例子:
#define dPS struct s *
typedef struct s * tPS;
以上两种情况的意图都是要定义dPS 和 tPS 作为一个指向结构s指针。哪种方法更好呢?(如果有的话)为什么?
这是一个非常微妙的问题,任何人答对这个问题(正当的原因)是应当被恭喜的。答案是:typedef更好。思考下面的例子:
dPS p1,p2;
tPS p3,p4;
第一个扩展为
struct s * p1, p2;
上面的代码定义p1为一个指向结构的指,p2为一个实际的结构,这也许不是你想要的。第二个例子正确地定义了p3 和p4 两个指针。
晦涩的语法
16. C语言同意一些令人震惊的结构,下面的结构是合法的吗,如果是它做些什么?
int a = 5, b = 7, c;
c = a+++b;
这个问题将做为这个测验的一个愉快的结尾。不管你相不相信,上面的例子是完全合乎语法的。问题是编译器如何处理它?水平不高的编译作者实际上会争论这个问题,根据最处理原则,编译器应当能处理尽可能所有合法的用法。因此,上面的代码被处理成:
c = a++ + b;
因此, 这段代码持行后a = 6, b = 7, c = 12。
如果你知道答案,或猜出正确答案,做得好。如果你不知道答案,我也不把这个当作问题。我发现这个问题的最大好处是:这是一个关于代码编写风格,代码的可读性,代码的可修改性的好的话题
What will print out?
main()
{
char *p1=“name”;
char *p2;
p2=(char*)malloc(20);
memset (p2, 0, 20);
while(*p2++ = *p1++);
printf(“%sn”,p2);
}
Answer:empty string.
What will be printed as the result of the operation below:
main()
{
int x=20,y=35;
x=y++ + x++;
y= ++y + ++x;
printf(“%d%dn”,x,y);
}
Answer : 5794
What will be printed as the result of the operation below:
main()
{
int x=5;
printf(“%d,%d,%dn”,x,x< <2,x>>2);
}
Answer: 5,20,1
What will be printed as the result of the operation below:
#define swap(a,b) a=a+b;b=a-b;a=a-b;
void main()
{
int x=5, y=10;
swap (x,y);
printf(“%d %dn”,x,y);
swap2(x,y);
printf(“%d %dn”,x,y);
}
int swap2(int a, int b)
{
int temp;
temp=a;
b=a;
a=temp;
return 0;
}
Answer: 10, 5
10, 5
What will be printed as the result of the operation below:
main()
{
char *ptr = ” Cisco Systems”;
*ptr++; printf(“%sn”,ptr);
ptr++;
printf(“%sn”,ptr);
}
Answer:Cisco Systems
isco systems
What will be printed as the result of the operation below:
main()
{
char s1[]=“Cisco”;
char s2[]= “systems”;
printf(“%s”,s1);
}
Answer: Cisco
What will be printed as the result of the operation below:
main()
{
char *p1;
char *p2;
p1=(char *)malloc(25);
p2=(char *)malloc(25);
strcpy(p1,”Cisco”);
strcpy(p2,“systems”);
strcat(p1,p2);
printf(“%s”,p1);
}
Answer: Ciscosystems
The following variable is available in file1.c, who can access it?:
static int average;
Answer: all the functions in the file1.c can access the variable.
WHat will be the result of the following code?
#define TRUE 0 // some code
while(TRUE)
{
// some code
}
Answer: This will not go into the loop as TRUE is defined as 0.
What will be printed as the result of the operation below:
int x;
int modifyvalue()
{
return(x+=10);
}
int changevalue(int x)
{
return(x+=1);
}
void main()
{
int x=10;
x++;
changevalue(x);
x++;
modifyvalue();
printf("First output:%dn",x);
x++;
changevalue(x);
printf("Second output:%dn",x);
modifyvalue();
printf("Third output:%dn",x);
}
Answer: 12 , 13 , 13
What will be printed as the result of the operation below:
main()
{
int x=10, y=15;
x = x++;
y = ++y;
printf(“%d %dn”,x,y);
}
Answer: 11, 16
What will be printed as the result of the operation below:
main()
{
int a=0;
if(a==0)
printf(“Cisco Systemsn”);
printf(“Cisco Systemsn”);
}
Answer: Two lines with “Cisco Systems” will be printed.
11. 写一个函数,完成内存之间的拷贝。[考虑问题是否全面]
答:void* mymemcpy( void *dest, const void *src, size_t count )
{
char* pdest = static_cast<char*>( dest );
const char* psrc = static_cast<const char*>( src );
if( pdest>psrc && pdest<psrc+cout ) 能考虑到这种情况就行了
{
for( size_t i=count-1; i!=-1; --i )
pdest[i] = psrc[i];
}
else
{
for( size_t i=0; i<count; ++i )
pdest[i] = psrc[i];
}
return dest;
}
int main( void )
{
char str[] = "0123456789";
mymemcpy( str+1, str+0, 9 );
cout << str << endl;
system( "Pause" );
return 0;
}
C++面试题集锦 2006-7-7更新
29.编写一个函数,函数接收一个字符串,是由十六进制数组成的一组字符串,函数的功能是把接到的这组字符串转换成十进制数字.并将十进制数字返回.
30.编写一个函数将一条字符串分成两部分,将前半部分按ASCII码升序排序,后半部分不变,(如果字符串是奇数则中间的字符不变,)最后再将前后两部分交换,然后将该字符串输出,
测试字符串“ADZDDJKJFIEJHGI”
31.找错
Void test1()
{
char string[10];
char* str1="0123456789";
strcpy(string, str1);
}
Void test2()
{
char string[10], str1[10];
for(I=0; I<10;I++)
{
str1[i] ='a';
}
strcpy(string, str1);
}
Void test3(char* str1)
{
char string[10];
if(strlen(str1)<=10)
{
strcpy(string, str1);
}
}
32. 找错
#define MAX_SRM 256
DSN get_SRM_no()
{
static int SRM_no;
int I;
for(I=0;I{
SRM_no %= MAX_SRM;
if(MY_SRM.state==IDLE)
{
break;
}
}
if(I>=MAX_SRM)
return (NULL_SRM);
else
return SRM_no;
}
33. 写出程序运行结果
int sum(int a)
{
auto int c=0;
static int b=3;
c+=1;
b+=2;
return(a+b+C);
}
void main()
{
int I;
int a=2;
for(I=0;I<5;I++)
{
printf("%d,", sum(a));
}
}
34.
int func(int a)
{
int b;
switch(a)
{
case 1: 30;
case 2: 20;
case 3: 16;
default: 0
}
return b;
}
则func(1)=?
35:
int a[3];
a[0]=0; a[1]=1; a[2]=2;
int *p, *q;
p=a;
q=&a[2];
则a[q-p]=?
36.
定义 int **a[3][4], 则变量占有的内存空间为:_____
37.
编写一个函数,要求输入年月日时分秒,输出该年月日时分秒的下一秒。如输入2004年12月31日23时59分59秒,则输出2005年1月1日0时0分0秒。
38.写一个函数,判断一个int型的整数是否是2的幂,即是否可以表示成2^X的形式(不可以用循环)
我只知道是用递推,大概写了一下,如下:
int IsTwoPow(int s)
{
if(s==1)return FALSE;
s=s>>1;
if(s>1)IsTwoPow(s);
return (s==1)?TRUE:FALSE;//大概是这个意思,但是这一句似乎不该这么返回!
}
39 A,B从一堆玻璃球(共100个)里向外拿球,规则如下:
(1)A先拿,然后一人一次交替着拿;
(2)每次只能拿1个或2个或4个;
(3)谁拿最后一个球,谁就是最后的失败者;
问A,B谁将是失败者?写出你的判断步骤。
40.已知:无序数组,折半查找,各元素值唯一。
函数原型是:Binary_Seach(int array[], int iValue, int iCount)
array是数组,在里面用折半查找的方法找等于iValue的值,找到返回1否则0,iCount是元素个数
41.统计一个字符串中字符出现的次数
42.100位以上的超大整数的加法(主要考虑数据结构和加法的实现)。
43.对如下电文:"CASTCASTSATATATASA"给出Huffman编码。
44.int (* (*f)(int, int))(int)表示什么含义?
45.x=x+1,x+=1,x++,为这三个语句的效率排序。并说明为什么。
46.中缀表达式 A-(B+C/D)*E的后缀形式是什么?
47.struct S1
{
char c;
int i;
};
sizeof(S1) = ?
class X{
public:
X();
virtual ~X();
void myMemberFunc();
static void myStaticFunc();
virtual void myVirtualFunc();
private:
int i;
char * pstr;
char a;
}
sizeof(X) = ?
48.找出两个字符串中最大子字符串,如"abractyeyt","dgdsaeactyey"的最大子串为"actyet"
49.有一百个整数,其中有负数,找出连续三个数之和最大的部分.
50.写一程序实现快速排序. 假设数据输入为一文件
快速算法描述如下
Algorithm Partition
Input: sequence a0, ..., an-1 with n elements
Output: permutation of the sequence such that all elements a0, ..., aj are less than or equal to all
elements ai, ..., an-1 (i > j)
Method:
choose the element in the middle of the sequence as comparison element x
let i = 0 and j = n-1
while ij
search the first element ai which is greater than or equal to x
search the last element aj which is less than or equal to x
if ij
exchange ai and aj
let i = i+1 and j = j-1
After partitioning the sequence, Quicksort treats the two parts recursively by the same procedure.
The recursion ends whenever a part consists of one element only.
51.写一算法检测单向链表中是否存在环(whether there is a loop in a link list),
要求算法复杂度(Algorithm's complexity是O(n)) 并只使用常数空间(space is O(c)).
注意,你只知道一个指向单向链表头的指针。链表的长度是不定的,而且环出现的地方也是不定的,环有可能在头,有可能在中间。而且要求是检测, 不能破坏环的结构.
52.设下列函数已经通过了调试
bool Sort_Array(ArrayType * Pinputarray, ArrayType * Poutarray);
该函数在内存中排序,能把字节数最大为100M字节的ArrayType类型的数组排序。其中ArrayType是一个
预定义的数组类型(细节无关紧要),Pinputarray,Poutarray分别为排序前的指针和排序后的指针。
请用c语言的伪码风格设计一个算法,他调用上面给出的函数完成下列从输入到输出的任务:
输入:排序前的大文件,名称为char * pinoutfilename ,其内容为用分号分隔的ArrayType类型的数组元素,可装满4个100M字节的数组。
输出:排序后的大文件char * poutoutfilename。
53.用最有效率的方法算出2乘以8等於几?
54.
1.错误的转义字符是 (c )
A.'\091' B.'\\'
C.'\0' D.'\''
2.若数组名作实参而指针变量作形参,函数调用实参传给形参的是 (d )
A.数组的长度 B.数组第一个元素的值
C.数组所有元素的值 D.数组第一个元素的地址
3.变量的指针含意是指变量的 (b )
A.值 B.地址
C.存储 D.名字
5.某文件中定义的静态全局变量(或称静态外部变量)其作用域是 (d )
A.只限某个函数 B.本文件
C.跨文件 D.不限制作用域
55.
1. 解二次方程:a*x*x+b*x+c
int Quadratic( double a,double b,double c,double& x1,double& x2);
返回值:解的个数
2. 最大公约数
DWORD Divisor( DWORD dwFirst, DWORD dwSecond );
返回值:最大公约数
3. 根据蒙特卡洛算法计算圆周率
double PI( DOWRD dwCount/*测试次数*/ );
返回值:PI
4. 无符号整数乘法,乘数为32bit,结果为64bit
提示:32bit整数分解为16bit相乘
void Multiply( DWORD dwFirst, DWORD dwSecond, DWORD& dwHigh, DWORD& dwLower );
5. 链表排序(从小到大)
节点定义为:
struct Node{
int nValue;
struct Node* pNext;
};
最后一个节点的pNext = NULL.
Node* SortChain( Node* pHead );
返回值:链表头
[2006-07-07 09:23 AM]
c/c++面试题集锦 2006-7-7更新
24.Assignment 2: Picture Processing
Use C++, Java, or similar languages or/and any middleware such as EJB and J2EE to process a picture with a high resolution (3 Mega Pixels for example). Use some methodologies to degrade the resolution of the picture to make it quicker for browsing. Then divide the degraded picture into 9 sectors equally. Click any of the 9 sectors will result a detailed picture for this sector with the same resolution as that of the original picture. This assignment is designed for you to demonstrate your ability to handle pictures.
25.用<<,>>,|,&实现一个WORD(2个字节)的高低位交换!!
26.要开辟P1,P2,P3,P4内存来做缓冲,大小自定,但这四个缓冲的大小要一样,并且是连续的!
27.有一浮点型数组A,用C语言写一函数实现对浮点数组A进行降序排序,并输出结果,要求要以数组A作为函数的入口.(建议用冒泡排序法)
28.找错:
#include <string.h>
#include <stdio.h>
class Base
{
private:
char * name;
public:
Base(char * className)
{
name = new char[strlen(className)];
strcpy(name, className);
}
~Base()
{
delete name;
}
char * copyName()
{
char newname [256];
strcpy(newname, name);
return newname;
}
char * getName()
{
return name;
}
static void print(Base base)
{
printf("name: %s\n" , base.name);
}
};
class Subclass : public Base
{
public:
Subclass(char * className) : Base(className)
{
}
};
int main()
{
Base * pBase = new Subclass("test");
Base::print(*pBase);
printf("name: %s\n", pBase->getName());
printf("new name: %s\n", pBase->copyName());
return 0;
}
[2006-07-07 09:22 AM]
c/c++面试题集锦 2006-7-7更新
21.有双向循环链表结点:
typedef struct node
{
int date;
struct node *front,*next;
}_Node;
有两个双向循环链表A,B,知道其头指针为:pHeadA,pHeadB,请写一函数将两上链表中date值相同的结点删除
22.
char * GetStr()
{
char *tmp;
tmp = "123"
return tmp;
}
void main()
{
printf("%s", GetStr());
}
会输出123吗?123创建在堆上还是栈上呢?123的空间是什么时候释放的?
23.
字符指针、浮点数指针、以及函数指针这三种类型的变量哪个占用的内存最大?为什么?
类ClassB从ClassA派生,那么ClassA *a = new ClassB(…); 试问该表达