c++面试题整理(四)

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),

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值