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

迅雷2013C++笔试卷B

2016-03-03 16:40 519 查看

基础知识题

1. 下面程序的输出结果是:

char *p1= “123”, *p2 = “ABC”, str[50]= "xyz";
strcpy(str+2,strcat(p1,p2));
cout << str;


xyz123ABC
z123ABC
xy123ABC
出错

解析:这段代码执行会出bug,strcat(p1,p2)出错,因为p1只被分配了4个字节的地址空间,将p2拼接在p1后面会引起内存溢出。注意字符串字面常量;选择最后一项;

2. 浮点数与位运算:

所有的位运算都不能直接操作浮点数,但是,我们可以间接对浮点数进行位运算,例如,将float数据,取地址(&)强行转化为整型(int等)指针后,再取值(*)后赋值给整型变量(int等),然后就能对该变量进行位运算了。【注】对浮点数进行位运算,一般没什么意义,因为浮点数的存储结构分三部分,进行过位运算后,所得结果跟原始浮点数没多大关系。

3. 内联函数:类中使用内联函数的地方会在编译阶段用内联函数体替换掉。

编程题

1. 有n个文件的长度记载在一个无符号64位整数数组中unsigned__int64 file_length
,把这n 个文件从逻辑上按序首尾拼接在一起形成一个逻辑上的大文件,然后以每块长度为unsigned block_length把这个逻辑上的大文件划分成大小相等的数据块(当然,最后一块有可能比block_length小),请定义和实现一个函数,把边界块的序号集合返回给函数的调用者(第一个数据块序号为0)。

注:边界块指的是跨多个文件的数据块。

2. 请实现一个函数,把两个从大到小的有序链表合并成一个链表,新的链表是一个从小到大的有序链表。

/*
题目描述
请实现一个函数,把两个从大到小的有序链表合并成一个链表,新的链表是一个从小到大的有序链表。
*/

#include <iostream>
#include <cstdlib>

using namespace std;

struct list
{
int value;
list* next;
};

list * merge(list *list1_head, list*list2_head)
{
if (!list1_head && !list2_head)
return NULL;
else if (!list1_head)
return list2_head;
else if (!list2_head)
return list1_head;
else{
list *p = list1_head, *q = list2_head, *r = NULL;
list *rHead = NULL, *rEnd = rHead;
while (p && q)
{
if (p->value <= q->value)
{
r = p->next;
if (rHead == NULL)
{
rHead = p;
rEnd = rHead;
rEnd->next = NULL;
}//if
else{
rEnd->next = p;
p->next = NULL;
rEnd = p;
}//else
p = r;
}//if
else{
r = q->next;
if (rHead == NULL)
{
rHead = q;
rEnd = rHead;
rEnd->next = NULL;
}//if
else{
rEnd->next = q;
q->next = NULL;
rEnd = q;
}//else
q = q->next;
}//else
}//while

while (p)
{
r = p->next;
rEnd = p;
p->next = NULL;
rEnd = p;

p = r;
}//while

while (q)
{
r = q->next;
rEnd = q;
q->next = NULL;
rEnd = q;

q = r;
}//while

return rHead;
}//else
}
3. 如果两个英文单词,组成它们的字符集合相同,而且相同字符出现的次数也相同,则称这两个词匹配:比如说:同”abbc”与词 ”babc”是匹配的。有一个词典,存储在字符串数组const char* dictionary
中,数组的每一个元素是一个词。对于任意给出的句子。句子中的单词使用空格分割。请实现以下函数,判断句子中是否有词和词典中的词匹配。

bool is_matching( const char* dictionary[],int n, const char* sentence);

/*
题目描述
如果两个英文单词,组成它们的字符集合相同,而且相同字符出现的次数也相同,则称这两个词匹配:
比如说:同”abbc”与词 ”babc”是匹配的。有一个词典,存储在字符串数组const char* dictionary
中,
数组的每一个元素是一个词。对于任意给出的句子。句子中的单词使用空格分割。

请实现以下函数,判断句子中是否有词和词典中的词匹配。
bool is_matching( const char* dictionary[],int n, const char* sentence);
*/

#include <iostream>
#include <cstdlib>
#include <vector>

using namespace std;

bool isMatch(const char*str1, const char *str2)
{
int len1 = 0, len2 = 0;
const char *p = str1, *q = str2;
/*求两个字符串长度*/
while (*p++ != '\0')
++len1;
while (*q++ != '\0')
++len2;

if (len1 != len2)
return false;

vector<char> vMap(256, 0);
p = str1;
while (*p != '\0')
{
++vMap[*p];
++p;
}//while

/*验证匹配性*/
q = str2;
while (*q != '\0')
{
if (--vMap[*q] < 0)
return false;
++q;
}//while
return true;
}

bool is_matching(const char* dictionary[], int n, const char* sentence)
{
if (!sentence)
return false;

int len = 0;
const char *p = sentence;
while (*p++ != '\0')
++len;

char *word = new char[len + 1];
word[0] = '\0';

/*单词长度计数sz*/
int sz = 0;
p = sentence;
while (*p != '\0')
{
if (*p == ' ')
{
word[sz] = '\0';
for (int i = 0; i < n; ++i)
{
if (isMatch(word,dictionary[i]))
return true;
}//for
sz = 0;
word[sz] = '\0';
}
else{
word[sz++] = *p;
}//else

++p;
}//while
/*查找最后一个单词在不在词典中*/
word[sz] = '\0';
for (int i = 0; i < n; ++i)
{
if (isMatch(word, dictionary[i]))
return true;
}//for

return false;
}

int main()
{
const char *dictionary[] = { "I", "am", "a", "good", "student." };

const char *s = "very good ";

cout << is_matching(dictionary, 5, s) << endl;

system("pause");
return 0;

}


牛客网 -- 原题网址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: