一. 有n个文件的长度记载在一个无符号64 位整数数组中unsigned __int64 file_length[n],把这n 个文件从逻辑上按序首尾拼接在一起形成一个逻辑上的大文件,然后以每块长度为unsigned block_length把这个逻辑上的大文件划分成大小相等的数据块(当然,最后一块有可能比block_length小),请定义和实现一个函数,把边界块的序号集合返回给函数的调用者(第一个数据块序号为0)。
注:边界块指的是跨多个文件的数据块。(30分)
思路很简单,直接贴代码了。
程序如下:
#include <stdio.h>
const unsigned int n = 100;
unsigned __int64 file_length[n];
unsigned block_lenght = 100;
// index[]: 返回的集合, size: 集合的大小
void solve(unsigned int *index, unsigned int& size)
{
int i;
__int64 sum = 0;
for (i = 0; i < n; ++i)
{
// 判断是否跨越边界
if ((file_length[i] + sum % block_lenght) > block_lenght)
{
index[size++] = i;
}
sum += file_length[i];
}
}
int main()
{
unsigned int index[n];
unsigned int size = 0;
solve(index, size);
return 0;
}
二. 请实现一个函数,把两个从大到小的有序链表合并成一个链表,新的链表是一个从小到大的有序链表。
struct list
{
int value;
list* next;
};
list * merge (list *list1_head, list *list2_head);
(30分)
思路也很简单,就直接粘代码了。
程序:
#include <iostream>
using namespace std;
struct list
{
int value;
list *next;
};
list* insert(list *head, int value)
{
list *ptr = new list;
ptr->value = value;
ptr->next = head;
return ptr;
}
list* insert(list *head, list *list_head)
{
for(; list_head != NULL; list_head = list_head->next)
head = insert(head, list_head->value);
return head;
}
list* merge(list *list1_head, list *list2_head)
{
list *pHead = NULL;
for (; list1_head != NULL && list2_head != NULL;)
{
if (list1_head->value > list2_head->value)
{
pHead = insert(pHead, list1_head->value);
list1_head = list1_head->next;
}
else
{
pHead = insert(pHead, list2_head->value);
list2_head = list2_head->next;
}
}
if (list1_head != NULL)
pHead = insert(pHead, list1_head);
else if (list2_head != NULL)
pHead = insert(pHead, list2_head);
return pHead;
}
/*
// 从小到大输入
list* input(list *head)
{
int n;
int val;
cout << "Enter n: ";
cin >> n;
while (n--)
{
cin >> val;
head = insert(head, val);
}
return head;
}
void print(list *head)
{
for(; head != NULL; head = head->next)
{
cout << head->value << "/t";
}
cout << endl;
}
*/
int main()
{
list *list1_head = NULL;
list *list2_head = NULL;
list *head = NULL;
//list1_head = input(list1_head);
//list2_head = input(list2_head);
head = merge(list1_head, list2_head);
//pr int(list1_head);
//print(list2_head);
//print(head);
for (; head != NULL;)
{
list *ptr = head;
head = head->next;
delete ptr;
}
return 0;
}
三. 如果两个英文单词,组成它们的字符集合相同,而且相同字符出现的次数也相同,则称这两个词匹配:比如说:同”abbc”与词”babc”是匹配的。有一个词典,存储在字符串数组const char* dictionary[n]中,数组的每一个元素是一个词。对于任意给出的句子。句子中的单词使用空格分割。请实现以下函数,判断句子中是否有词和词典中的词匹配。
bool is_matching( const char* dictionary[], int n, const char* sentence);
(40分)
注意:这一题需要先描述思路,再写程序,没写思路扣10分。
/*
* 思路:
* 1. 词典的每个字符串内部先从小到大排序(STL sort)
* 2. 从小到大排序词典字符串数组(STL sort)
* 3. 读出 sentence 句子的每个单词存入字符串数组 sz[] 里
* 4. sz[]的每个字符串内部先从小到大排序(STL sort)
* 5. 用二分查找判断 sz[i] 是否匹配词典里的某个词
*/
#include <iostream>
#include <string>
#include <algorithm> // for sort and binary_search
using namespace std;
const int MAX_CHAR = 100;
const int MAX_SEN_WORD = 100;
const int MAX_N = 100;
struct Str
{
char arr[MAX_CHAR];
bool operator < (const Str& s) const
{
return strcmp(arr, s.arr) < 0;
}
};
struct Equal
{
bool operator () (const Str& s1, const Str& s2) const
{
return strcmp(s1.arr, s2.arr) == 0;
}
};
Str sz[MAX_SEN_WORD];
Str dic[MAX_N];
// 返回值为 sentence 里单词的个数
int init(const char *dictionary[], int n, const char *sentence)
{
int i;
for (i = 0; i < n; ++i)
{
strcpy(dic[i].arr, dictionary[i]);
int len = strlen(dic[i].arr);
sort(dic[i].arr, dic[i].arr + len);
}
///////////////////////////////////
// 把 sentence 拆成单词
i = 0;
sscanf(sentence, "%s", sz[i].arr);
sort(sz[i].arr, sz[i].arr + strlen(sz[i].arr));
while (true)
{
sentence = strstr(sentence, " ");
if (sentence == NULL) break;
sscanf(++sentence, "%s", sz[++i].arr);
sort(sz[i].arr, sz[i].arr + strlen(sz[i].arr));
}
////////////////////////////////////////
sort(dic, dic + n);
return (i + 1);
}
/*
// 自己的二分查找函数
bool binary_search(int n, const Str &s)
{
int l = 0, r = n;
while (l <= r)
{
int mid = (l + r) >> 1;
int cmp = strcmp(dic[mid].arr, s.arr);
if (cmp == 0)
return true;
else if (cmp > 0)
r = mid - 1;
else
l = mid + 1;
}
return false;
}
*/
bool is_matching(const char *dictionary[], int n, const char *sentence)
{
int cnt =0, i;
cnt = init(dictionary, n, sentence);
for (i = 0; i < cnt; ++i)
{
// 使用 STL库的二分查找函数
if (binary_search(dic, dic + n, sz[i], Equal()))
return true;
// 使用自己的二分查找函数
//if (binary_search(n, sz[i]))
//{
// return true;
//}
}
return false;
}
int main(int argc, char *argv[])
{
const char *dictionary[] = {"hello", "world", "i", "love", "you"};
char *sentence = "hi how are ouy";
int n = 5;
if (is_matching(dictionary, n, sentence))
cout << "match" << endl;
else
cout << "no match" << endl;
return 0;
}