您的位置:首页 > 编程语言 > C语言/C++

c++面试题整理(四)

2012-05-20 22:29 211 查看
43. 分析一下这段程序的输出 (Autodesk)

#include<iostream>
using namespace std;

class B{
public:
B(){
cout<<"default constructor"<<endl;
}
~B(){
cout<<"destructed"<<endl;
}
B(const B& b){
data=b.data;
cout<<"copy constructor"<<endl;
}
const B& operator=(const B& b)
{
if(this!=&b)
{
data=b.data;
cout<<"operator="<<endl;
}
return *this;
}
B(int i):data(i)
{
cout<<"constructed by parameter"<<data<<endl;
}
private:
int data;
};

B Play( B b)
{
return b;
}

int main(int argc, char* argv[])
{

B t1;
t1=Play(5);
//t1=t1;
B t2=Play(t1);
return 0;
}

运行结果为:



44.写一个函数找出一个整数数组中,第二大的数 (microsoft)


#include<iostream>
using namespace std;

int main(int argc, char* argv[])
{
int a[]={1,6,5,6,7,9,3,4,10,18,9};
int size_a=sizeof(a)/sizeof(int);
int first=a[0];
int second=a[0];
for(int i=1;i!=size_a;i++)
{
if(a[i]>first)
{
second=first;
first=a[i];
}
if(a[i]>second && a[i]<first)
second=a[i];
}
cout<<first<<'\t'<<second<<endl;
return 0;
}

44.写一个在一个字符串(n)中寻找一个子串(m)第一个位置的函数。

KMP算法效率最好,时间复杂度是O(n+m),
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: