雅虎三道面试题
2013-09-20 11:10
155 查看
雅虎三道面试题
1. 编程实现:把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列。
解:
2. 编程实现:找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"。
3. 有双向循环链表节点定义为:
struct node
{
int data;
struct node *front,*next'
};
有两个双向循环链表A,B,知道其头指针为:pHeadA,pHeadB,请写一函数将两链表中data值相同的节点删除.
解:
1. 编程实现:把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列。
解:
#include "stdafx.h" #include <iostream> using namespace std; /************************************************************************/ /* 函数功能:把十进制数(long型)分别以二进制 */ /************************************************************************/ void LongTo2(long n) { if(0 == n) return; else { LongTo2(n/2); cout<<n%2; } } /************************************************************************/ /* 函数功能:把十进制数(long型)分别以16进制 */ /************************************************************************/ const char arr[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; void LongTo16(long n) { if(0 == n) return; else { LongTo16(n/16); cout<<arr[n%16]; } } int main() { LongTo2(56); cout<<endl; LongTo16(256); cout<<endl; }
2. 编程实现:找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大子串为"cad"。
#include "stdafx.h" #include <iostream> using namespace std; /************************************************************************/ /* 函数功能:找出两个字符串中最大公共子字符串,如"abccade","dgcadde"的最大 子串为"cad"*/ /************************************************************************/ void GetMaxCommonSubString(const char *str1, const char *str2, char* &subString) { int length1 = strlen(str1); int length2 = strlen(str2); int max = 0; //存储最大值 int start = 0;//最大公共字串在str1中开始的位置 for (int i=0; i<length1; i++) { for (int j=0; j<length2; j++) { int temp1 = i; //每次查找的开始位置 int temp2 = j; int number = 0; //统计个数 while ((str1[temp1] == str2[temp2]) && (temp2 < length2) && (temp1 < length1)) //开始找公共字串 { number++; temp1++; temp2++; } if (number > max) //判断是否是最大的 { max = number; start = i; } } } strncpy(subString, str1 + start, max); //拷贝到subString中 } int main() { const char* str1 = "abccade"; const char* str2 = "dgcadde"; int length = (strlen(str1) <= strlen(str2) ? strlen(str1) : strlen(str2)); char *str = new char[length]; memset(str, '/0', length+1); GetMaxCommonSubString(str1, str2, str); cout<<str<<endl; }
3. 有双向循环链表节点定义为:
struct node
{
int data;
struct node *front,*next'
};
有两个双向循环链表A,B,知道其头指针为:pHeadA,pHeadB,请写一函数将两链表中data值相同的节点删除.
解:
struct Node { int data; struct Node *front,*next; Node( int value ): data( value ), front( NULL ), next( NULL ) { }; void SetPointer( Node *pPre, Node *pNext ) { front = pPre; next = pNext; }; }; //如果成功删除返回真。否则,返回假。 bool DeleteValue( Node *&pHead, int target ) { if( pHead == NULL ) return false; //至少有两个元素 bool flag = false; Node* ph = pHead; while( ph->next != pHead ) { Node *pPre = ph->front; Node *pNext = ph->next; if( ph->data == target ) { //如果删除的是第一个元素 if( ph == pHead ) pHead = ph->next; pPre->next = pNext; pNext->front = pPre; Node *tmp = ph; delete tmp; //设置删除标记 flag = true; } ph = pNext; } //只有一个元素或最后一个元素 if( ph->next == pHead ) { if( ph->data == target ) { //如果要删除的是最后一个元素 if( ph->front != ph ) { Node *pPre = ph->front; Node *pNext = ph->next; pPre->next = pNext; pNext->front = pPre; Node *tmp = ph; delete tmp; } else { delete pHead; pHead = NULL; } flag = true; } } return flag; } void DeleteSame( Node *&pHeadA, Node *&pHeadB ) { if( pHeadA != NULL && pHeadB != NULL ) { Node *pa = pHeadA; while( pa->next != pHeadA ) { //如果B中含有pa->data,并且已经删除 if( DeleteValue( pHeadB, pa->data ) ) { //在A中删除pa->data Node *tmp = pa->next; DeleteValue( pHeadA, pa->data ); pa = tmp; } else pa = pa->next; } //只有一个元素或最后一个元素 if( DeleteValue( pHeadB, pa->data ) ) { DeleteValue( pHeadA, pa->data ); } } }
相关文章推荐
- 雅虎三道面试题 答案
- 雅虎面试题-你真的了解HTML吗?
- 微软、google、雅虎、百度等各大著名公司的经典面试题!保证搞晕你!!
- 三道PHP面试题,换来一份PHP工作
- 雅虎面试题─把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列
- 雅虎面试题─把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列
- 三道关于面向对象设计的面试题
- 据说是雅虎的一份PHP面试题附答案
- 微软招聘总经理助理的三道面试题
- 雅虎经典面试题
- 三道经典的逻辑推理面试题:病狗、三盏灯、买鸡
- 雅虎面试题─把十进制数(long型)分别以二进制和十六进制形式输出,不能使用printf系列
- 雅虎面试题─将两个双向循环链表中data值相同的结点删除
- 微软、google、雅虎、百度等各大著名公司的经典面试题
- 你真的了解HTML吗?–雅虎面试题
- 一份 PHP 面试题,据说是雅虎的
- 你真的了解HTML吗?–雅虎面试题
- 雅虎面试题-你真的了解HTML吗?
- 雅虎面试题─有两个双向循环链表A,B,知道其头指针为:pHeadA,pHeadB,请写一函数将两链表中data值相同的结点删除
- 微软、google、雅虎、百度等各大著名公司的经典面试题!保证搞晕你!!(转)