2014创新工场涂鸦移动校园招聘测试题 及 答案
本文地址: http://blog.csdn.net/caroline_wendy/article/details/28664807
by Spike 2014.6.5本题目仅供学术交流, 严禁用于其他目的, 答案仅供参考.
1. 合并有序链表, 给定两个升序的链表, 返回一个合并之后的升序链表.节点结构:struct Node{int val;Node *next;};要求实现的函数:Node* mergeList (Node *list_a, Node* list_b)代码:/*
* test.cpp
*
* Created on: 2014.04.24
* Author: Spike
*/
/*eclipse cdt, gcc 4.8.1*/
#include
#include
using namespace std;
struct Node{
int val;
Node *next;
};
Node* mergeList(Node *list_a, Node* list_b) {
if (list_a == NULL) //递归的终止条件
return list_b;
else if (list_b == NULL)
return list_a;
Node* pMergedHead = NULL; //合并后的链表
if (list_a->val < list_b->val) {
pMergedHead = list_a; //指向头结点
pMergedHead->next = mergeList(list_a->next, list_b); //递归
} else {
pMergedHead = list_b;
pMergedHead->next = mergeList(list_a, list_b->next);
}
return pMergedHead;
}
Node* initList(const std::vector& vi) {
Node* pHead = new Node;
Node* pTemp = pHead;
for (std::size_t i=0; ival = vi[i];
if (i != vi.size()-1) { //非尾结点
Node* pNode = new Node;
pTemp->next = pNode;
pTemp = pTemp->next;
}
}
pTemp->next = NULL;
return pHead;
}
void printList(Node* L) {
Node* pTemp = L;
while (pTemp->next != NULL) {
std::cout << pTemp->val << " ";
pTemp = pTemp->next;
}
std::cout << pTemp->val << " "; //打印最后一个值
std::cout << std::endl;
}
int main(void) {
std::vector via = {1, 2, 3, 4, 5, 13};
std::vector vib = {2, 4, 5, 7, 9, 11};
Node* list_a = initList(via);
Node* list_b = initList(vib);
std::cout << "list_a = "; printList(list_a);
std::cout << "list_b = "; printList(list_b);
Node* list_merge = mergeList(list_a, list_b);
std::cout << "list_merge = "; printList(list_merge);
return 0;
}
输出:list_a = 1 2 3 4 5 13
list_b = 2 4 5 7 9 11
list_merge = 1 2 2 3 4 4 5 5 7 9 11 13
2. 大整数乘法, 给定两个长度不超过10000的整数, 返回乘法的结果.char* multi(char* number_a, char* number_b)代码:/*
* test.cpp
*
* Created on: 2014.04.24
* Author: Spike
*/
/*eclipse cdt, gcc 4.8.1*/
#include
#include
using namespace std;
char* multi(char* number_a, char* number_b) {
int len_a = strlen(number_a); //计算长度
int len_b = strlen(number_b);
int* num_arr = new int[len_a+len_b];
memset(num_arr, 0, sizeof(int)*(len_a+len_b)); //置0
for (int i=len_a-1; i>=0; --i) { //计算每一位
for (int j=len_b-1; j>=0; --j) {
num_arr[i+j+1] += (number_b[j]-'0')*(number_a[i]-'0');
}
}
for (int i=len_a+len_b-1; i>=0; --i) { //进位
if (num_arr[i] >= 10) {
num_arr[i-1] += num_arr[i]/10;
num_arr[i] %= 10;
}
}
char* result = new char[len_a+len_b+1];
for( int i=0; i<(len_a+len_b); ++i){
result[i] = (char)(((int)'0')+num_arr[i]);
}
result[len_a+len_b] = '