📄 c-c++ 笔试、面试题目大汇总.htm
字号:
<DIV><FONT
size=2><STRONG>39.文件中有一组整数,要求排序后输出到另一个文件中</STRONG><BR>答案:</FONT></DIV>
<DIV><FONT size=2>#i nclude<iostream></FONT></DIV>
<DIV><FONT size=2>#i nclude<fstream></FONT></DIV>
<DIV><FONT size=2>using namespace std;</FONT></DIV>
<DIV><BR><FONT size=2>void Order(vector<int>& data)
//bubble sort<BR>{<BR>int count = data.size() ;<BR>int tag = false ;
// 设置是否需要继续冒泡的标志位<BR>for ( int i = 0 ; i < count ;
i++)<BR>{<BR>for ( int j = 0 ; j < count - i - 1 ;
j++)<BR>{<BR>if ( data[j] > data[j+1])<BR>{<BR>tag = true
;<BR>int temp = data[j] ;<BR>data[j] = data[j+1] ;<BR>data[j+1] =
temp ;<BR>}<BR>}<BR>if ( !tag )<BR>break ;<BR>}<BR>}</FONT></DIV>
<DIV><BR><FONT size=2>void main( void
)<BR>{<BR>vector<int>data;<BR>ifstream
in("c:\\data.txt");<BR>if ( !in)<BR>{<BR>cout<<"file
error!";<BR>exit(1);<BR>}<BR>int temp;<BR>while
(!in.eof())<BR>{<BR>in>>temp;<BR>data.push_back(temp);<BR>}<BR>in.close();
//关闭输入文件流<BR>Order(data);<BR>ofstream out("c:\\result.txt");<BR>if (
!out)<BR>{<BR>cout<<"file error!";<BR>exit(1);<BR>}<BR>for ( i
= 0 ; i < data.size() ; i++)<BR>out<<data[i]<<"
";<BR>out.close(); //关闭输出文件流<BR>}</FONT></DIV>
<DIV><FONT size=2></FONT> </DIV>
<DIV><FONT size=2><STRONG>40. 链表题:一个链表的结点结构<BR>struct
Node<BR>{<BR>int data ;<BR>Node *next ;<BR>};<BR>typedef struct Node
Node ;</STRONG></FONT></DIV>
<DIV><BR><STRONG><FONT size=2>(1)已知链表的头结点head,写一个函数把这个链表逆序 (
Intel)</FONT></STRONG></DIV>
<DIV><FONT size=2>Node * ReverseList(Node *head) //链表逆序<BR>{<BR>if (
head == NULL || head->next == NULL )<BR>return head;<BR>Node *p1
= head ;<BR>Node *p2 = p1->next ;<BR>Node *p3 = p2->next
;<BR>p1->next = NULL ;<BR>while ( p3 != NULL
)<BR>{<BR>p2->next = p1 ;<BR>p1 = p2 ;<BR>p2 = p3 ;<BR>p3 =
p3->next ;<BR>}<BR>p2->next = p1 ;<BR>head = p2 ;<BR>return
head ;<BR>}<BR><STRONG>(2)已知两个链表head1 和head2
各自有序,请把它们合并成一个链表依然有序。(保留所有结点,即便大小相同)</STRONG><BR>Node * Merge(Node
*head1 , Node *head2)<BR>{<BR>if ( head1 == NULL)<BR>return head2
;<BR>if ( head2 == NULL)<BR>return head1 ;<BR>Node *head = NULL
;<BR>Node *p1 = NULL;<BR>Node *p2 = NULL;<BR>if ( head1->data
< head2->data )<BR>{<BR>head = head1 ;<BR>p1 =
head1->next;<BR>p2 = head2 ;<BR>}<BR>else<BR>{<BR>head = head2
;<BR>p2 = head2->next ;<BR>p1 = head1 ;<BR>}<BR>Node *pcurrent =
head ;<BR>while ( p1 != NULL && p2 != NULL)<BR>{<BR>if (
p1->data <= p2->data )<BR>{<BR>pcurrent->next = p1
;<BR>pcurrent = p1 ;<BR>p1 = p1->next
;<BR>}<BR>else<BR>{<BR>pcurrent->next = p2 ;<BR>pcurrent = p2
;<BR>p2 = p2->next ;<BR>}<BR>}<BR>if ( p1 != NULL
)<BR>pcurrent->next = p1 ;<BR>if ( p2 != NULL
)<BR>pcurrent->next = p2 ;<BR>return head ;<BR>}<BR></FONT><FONT
size=2><STRONG>(3)已知两个链表head1 和head2
各自有序,请把它们合并成一个链表依然有序,这次要求用递归方法进行。 (Autodesk)<BR></STRONG>答案:<BR>Node
* MergeRecursive(Node *head1 , Node *head2)<BR>{<BR>if ( head1 ==
NULL )<BR>return head2 ;<BR>if ( head2 == NULL)<BR>return head1
;<BR>Node *head = NULL ;<BR>if ( head1->data < head2->data
)<BR>{<BR>head = head1 ;<BR>head->next =
MergeRecursive(head1->next,head2);<BR>}<BR>else<BR>{<BR>head =
head2 ;<BR>head->next =
MergeRecursive(head1,head2->next);<BR>}<BR>return head
;<BR>}</FONT></DIV>
<DIV><FONT size=2><STRONG>41. 分析一下这段程序的输出
(Autodesk)</STRONG><BR>class
B<BR>{<BR>public:<BR>B()<BR>{<BR>cout<<"default
constructor"<<endl;<BR>}<BR>~B()<BR>{<BR>cout<<"destructed"<<endl;<BR>}<BR>B(int
i):data(i) //B(int) works as a converter ( int
-> instance of B)<BR>{<BR>cout<<"constructed by
parameter " << data <<endl;<BR>}<BR>private:<BR>int
data;<BR>};</FONT></DIV>
<DIV><BR><FONT size=2>B Play( B b) <BR>{<BR>return b
;<BR>}</FONT></DIV>
<DIV><FONT
size=2>(1)
results:<BR>int main(int argc, char*
argv[]) constructed by parameter
5<BR>{
destructed B(5)<STRONG>形参</STRONG>析构<BR>B t1 = Play(5); B t2 =
Play(t1); destructed t1形参析构<BR>return
0; destructed t2 <STRONG><FONT
color=#ff0000>注意顺序!</FONT></STRONG><BR>} destructed
t1</FONT></DIV>
<DIV><FONT
size=2>(2) results:<BR>int
main(int argc, char*
argv[]) constructed by parameter
5<BR>{
destructed B(5)<STRONG>形参</STRONG>析构<BR>B t1 = Play(5); B t2 =
Play(10); constructed by parameter 10<BR>return
0; destructed
B(10)<STRONG>形参</STRONG>析构<BR>}
destructed t2 <FONT
color=#ff0000><STRONG>注意顺序!</STRONG></FONT></FONT></DIV>
<DIV><FONT color=#ff0000><FONT color=#000000
size=2>
destructed t1</FONT></FONT></DIV>
<DIV><FONT size=2><STRONG>42. 写一个函数找出一个整数数组中,第二大的数
(microsoft)<BR></STRONG>答案:<BR>const int MINNUMBER = -32767 ;<BR>int
find_sec_max( int data[] , int count)<BR>{<BR>int maxnumber =
data[0] ;<BR>int sec_max = MINNUMBER ;<BR>for ( int i = 1 ; i <
count ; i++)<BR>{<BR>if ( data[i] > maxnumber )<BR>{<BR>sec_max =
maxnumber ;<BR>maxnumber = data[i] ;<BR>}<BR>else<BR>{<BR>if (
data[i] > sec_max )<BR>sec_max = data[i] ;<BR>}<BR>}<BR>return
sec_max ;<BR>}</FONT></DIV>
<DIV><STRONG><FONT size=2>43.
写一个在一个字符串(n)中寻找一个子串(m)第一个位置的函数。</FONT></STRONG></DIV>
<DIV><FONT size=2>KMP算法效率最好,时间复杂度是O(n+m)。</FONT></DIV>
<DIV><FONT size=2><STRONG>44. 多重继承的内存分配问题:<BR> 比如有class
A : public class B, public class C {}<BR>
那么A的内存结构大致是怎么样的?</STRONG><BR><BR>这个是compiler-dependent的,
不同的实现其细节可能不同。<BR>如果不考虑有虚函数、虚继承的话就相当简单;否则的话,相当复杂。<BR>可以参考《深入探索C++对象模型》,或者:<BR><A
href="http://blog.csdn.net/wfwd/archive/2006/05/30/763797.aspx">http://blog.csdn.net/wfwd/archive/2006/05/30/763797.aspx</A></FONT></DIV>
<DIV><FONT size=2><STRONG>45.
如何判断一个单链表是有环的?(注意不能用标志位,最多只能用两个额外指针)<BR></STRONG><BR>
struct node { char val; node* next;}<BR><BR> bool
check(const node* head) {} //return false : 无环;true:
有环</FONT></DIV>
<DIV><FONT
size=2>一种O(n)的办法就是(搞两个指针,一个每次递增一步,一个每次递增两步,如果有环的话两者必然重合,反之亦然):<BR>bool
check(const node* head)<BR>{<BR>
if(head==NULL) return false;<BR> node
*low=head, *fast=head->next;<BR>
while(fast!=NULL &&
fast->next!=NULL)<BR>
{<BR>
low=low->next;<BR>
fast=fast->next->next;<BR>
if(low==fast) return true;<BR>
}<BR> return
false;<BR>}</FONT></DIV></SPAN></SPAN></SPAN></SPAN></DIV><BR><BR>
<SCRIPT type=text/javascript><!--
google_ad_client = "pub-2976487794163546";
google_ad_width = 468;
google_ad_height = 60;
google_ad_format = "468x60_as";
google_ad_type = "text_image";
//2006-11-22: xia
google_ad_channel = "3545415502";
//--></SCRIPT>
<SCRIPT src="C-C++ 笔试、面试题目大汇总.files/show_ads.js"
type=text/javascript>
</SCRIPT>
<TABLE style="CLEAR: both" cellSpacing=0 cellPadding=0
width="97%"><TBODY>
<TR>
<TD align=right>
<TABLE>
<TBODY>
<TR>
<TD>阅读:<SPAN id=news_hits></SPAN> 次<BR>录入:<SPAN
id=MemberNameLabel><A
href="http://www.yoent.com/memberProfile.aspx?id=2"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -