笔试共三道大题,不是很难,但是编译器烂的要死,还不让用本地编译器。
第一道题:输入数组A和数组B,删除数组A中B中存在的元素!
输入描述:A:{1,2,3,4,5,6,7,8} B:{2,3,4}
输出描述:{1,5,6,7,8}
class solution {
public:
void DelAwhichinB(int array1[], int array1_length, int array2[],int array2_length) {
int count = 0, k = 0;;
bool flag;
for (int i = 0; i < array1_length; i++) {
flag = false;
for (int j = 0; j < array2_length; j++){
if (array2[j] == array1[i])
{
count++;
flag = true;
break;
}
}
if (flag == false)
array1[k++] = array1[i];
}
for (int i = 0; i < k; i++)
cout << array1[i] << " ";
}
};
第二题:输入一个链表,翻转链表从N到M个节点。
输入描述:1,2,3,4,5,6 3,5
输出描述:1,2,5,4,3,6
typedef struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
}node;
class Solution {
public:
node *reverseBetween(node *head, int m, int n) {
if (head == NULL) return NULL;
node *dummy = new node(0);
dummy->next = head;
node *pre, *start = head;
for (int i = 1; i < m; i++) {
pre = start;
start = start->next;
}
for (int i = m; i < n; i++) {
node *temp = start->next;
start->next = temp->next;
temp->next = pre->next;
pre->next = temp;
}
return dummy->next;
}
};
第三题:总钱数total,有n件礼品,每件礼品有相应的热度,购买其中一件或多件礼品,使热度最大。
输入描述:第一行输入总钱数,第二行输入礼品价格,第三行输入对应的热度
输出描述:最大热度
输入样例:
1000
200 600 100 180 300 450
6 10 3 4 5 8
输出样例:21
class solution {
public:
int pack(int prices[], int vals[], int total) {
int dp[1005] = { 0 };
for (int i = 1; i <= 6; i++) {
for (int j = total; j >= prices[i]; j--) {
dp[j] = max(dp[j - prices[i]] + vals[i], dp[j]);
}
}
return dp[total];
}
};