C++面试题之编程实现
2013-03-19 07:57
281 查看
1)给你个数组1……n进行排序,时间复杂度为O(n),空间复杂度是O(1),
#include<iostream.h>
int main()
{
int a[] = {10,6,9,5,2,8,4,7,1,3};
int len = sizeof(a) / sizeof(int);
int temp;
for(int i = 0; i < len; )
{
temp = a[a[i] - 1];
a[a[i] - 1] = a[i];
a[i] = temp;
if ( a[i] == i + 1)
i++;
}
for (int j = 0; j < len; j++)
cout<<a[j]<<",";
return 0;
}
2)写一个函数,完成内存之间的拷贝。[考虑问题是否全面]
char* mymemcpy(void *dest,void *src, size_t count)
{
assert((dest!=NULL)&&(src!=NULL));
byte *pdest=(byte*)dest; //防止地址被改变
byte *psrc=(byte*)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;
}
3)编写strcat函数
已知strcat函数的原型是char *strcat (char *strDest, const char *strSrc); 其中strDest 是目的字符串,strSrc是源字符串。不调用C++/C 的字符串库函数,请编写函数strcat
char * __cdecl strcat (char * dst, const char * src)
{
char * cp = dst;
while( *cp )
cp++; /* find end of dst */cp++; /
while( *cp++ = *src++ ) ; /* Copy src to end of dst */
return( dst ); /* return dst */return( dst );
}
strcat能把strSrc的内容连接到strDest,为什么还要char * 类型的返回值?
答:方便赋值给其他变量
二分查找的代码.:
int binary_search(int* arr, int key, int n)
{
int low = 0;
int high = n - 1;
int mid;
while (low <= high)
{
mid = (high + low) / 2;
if (arr[mid] > k)
high = mid - 1;
else if (arr[mid] < k)
low = mid + 1;
else
return mid;
}
return -1;
}
链表的倒序
void Reverse(node *H)
{node *p,*q;
p = H->next;
H->next = NULL;
while(p)
{q = p;p = p->next;q->next = H->next;H->next = p;}
}
#include<iostream.h>
int main()
{
int a[] = {10,6,9,5,2,8,4,7,1,3};
int len = sizeof(a) / sizeof(int);
int temp;
for(int i = 0; i < len; )
{
temp = a[a[i] - 1];
a[a[i] - 1] = a[i];
a[i] = temp;
if ( a[i] == i + 1)
i++;
}
for (int j = 0; j < len; j++)
cout<<a[j]<<",";
return 0;
}
2)写一个函数,完成内存之间的拷贝。[考虑问题是否全面]
char* mymemcpy(void *dest,void *src, size_t count)
{
assert((dest!=NULL)&&(src!=NULL));
byte *pdest=(byte*)dest; //防止地址被改变
byte *psrc=(byte*)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;
}
3)编写strcat函数
已知strcat函数的原型是char *strcat (char *strDest, const char *strSrc); 其中strDest 是目的字符串,strSrc是源字符串。不调用C++/C 的字符串库函数,请编写函数strcat
char * __cdecl strcat (char * dst, const char * src)
{
char * cp = dst;
while( *cp )
cp++; /* find end of dst */cp++; /
while( *cp++ = *src++ ) ; /* Copy src to end of dst */
return( dst ); /* return dst */return( dst );
}
strcat能把strSrc的内容连接到strDest,为什么还要char * 类型的返回值?
答:方便赋值给其他变量
二分查找的代码.:
int binary_search(int* arr, int key, int n)
{
int low = 0;
int high = n - 1;
int mid;
while (low <= high)
{
mid = (high + low) / 2;
if (arr[mid] > k)
high = mid - 1;
else if (arr[mid] < k)
low = mid + 1;
else
return mid;
}
return -1;
}
链表的倒序
void Reverse(node *H)
{node *p,*q;
p = H->next;
H->next = NULL;
while(p)
{q = p;p = p->next;q->next = H->next;H->next = p;}
}
相关文章推荐
- C/C++面试题:编程实现快速排序
- C、C++面试题:编程实现字符串中字串的查找
- C++读取、旋转和保存bmp图像文件编程实现
- VS2010 C++ 操作Excel表格的编程实现
- 编程作业 简单的学生信息处理程序实现[北京大学C++课程]
- C++面试题:介绍一下STL如何实现vector
- 【C++】声明并实现一个万年历类【腾讯面试题】
- 【面试题002】java实现的单例模式,c++实现单例模式,实现禁止拷贝
- [算法导论]2.2节编程练习C++实现
- 仿基因编程原理及其C++实现
- 基于visual c++之windows核心编程代码分析(53)在C++中嵌入汇编实现DLL注入源代码
- 华为经典面试题---大数乘法的c++实现
- 面试例题:输入n,求一个n x n矩阵,规定矩阵沿45度递增,形成一个zigzag数组(JPEG编码里取像素数据的排列顺序),请问如何用C++实现? (中国台湾著名硬件公司2007年11月面试题)
- 网易编程-最大的奇数约数c++实现
- C++ GUI Qt 编程(第二版)第8章 Diagram_2(实现主对话框)
- 【数字图像处理】C++读取、旋转和保存bmp图像文件编程实现
- 仿基因编程原理及其C++实现
- VS2010 C++ 操作Excel表格的编程实现
- TCP(SOCKETS)编程 C/C++实现
- 编程实现任意长度整数的加法(整数可以长度超出C++中int范围)