01-复杂度1 最大子列和问题
#include <iostream>
using namespace std;
int main() {
int a, b;
long long max, sum;
while (cin >> a) {
max = 0;
sum = 0;
for (int i = 0; i < a; i++) {
cin >> b;
sum += b;
if (sum > max)
max = sum;
if (sum < 0)
sum = 0;
}
cout << max << endl;
}
}
01-复杂度2 Maximum Subsequence Sum (25 分)
#include <iostream>
using namespace std;
typedef struct ye {
int first;
int last;
long long sum;
} ye;
int main() {
int a, b;
int number_first, number_last,number;
ye sum, max;
bool frank = true;
max.sum = 0;
sum.sum = 0;
cin >> a;
number_first = a - 1;
while (a--) {
cin >> b;
sum.sum += b;
if(!b)number++;
if (frank) {
sum.first = b;
frank = false;
}
if (sum.sum < 0) {
sum.sum = 0;
frank = true;
}
sum.last = b;
if (a == number_first)
number_first = b;
if (!a)
number_last = b;
if (sum.sum > max.sum) {
max = sum;
}
}
if (max.sum == 0&&!number) {
max.first = number_first;
max.last = number_last;
}else if(max.sum==0&&number){
max.first=0;
max.last=0;
}
cout << max.sum << " " << max.first << " " << max.last << endl;
}
01-复杂度3 二分查找 (20 分)
Position BinarySearch( List L, ElementType X ) {
int first = 0;
int last = L->Last;
int middle; //= (first + last) / 2 ;
while (first<last||first==last) {
middle=first+(last-first)/2;
if (L->Data[middle] > X) {
last = middle - 1;
first = middle -1;
// middle = (first + last) / 2 ;
} else if(L->Data[middle]<X) {
first = middle +1;
// middle = (first + last) / 2 ;
}
if (X == L->Data[middle]) {
return middle;
}
}
return NotFound;
}
02-线性结构1 两个有序链表序列的合并 (15 分)
List Merge( List L1, List L2 ) {
List ptr = (List)malloc(sizeof(struct Node));
List head = ptr;
List t1=L1,t2=L2;
L1=L1->Next;
L2=L2->Next;
while (L1 != NULL && L2 != NULL) {
if (L1->Data < L2->Data) {
ptr->Next = L1;
L1 = L1->Next;
ptr = ptr->Next;
} else {
ptr->Next = L2;
L2 = L2->Next;
ptr = ptr->Next;
}
}
if (L1 == NULL) {
ptr->Next = L2;
} else {
ptr->Next = L1;
}
t1->Next=t2->Next=NULL;
return head;
}
02-线性结构2 一元多项式的乘法与加法运算 (20 分)
#include <iostream>
using namespace std;
typedef struct ye {
int xishu;
int zhishu;
struct ye *next;
} ye;
typedef ye *ptr;
ptr create(int number) {
ptr head = (ptr)malloc(sizeof(ye));
ptr ptr1 = head;
while (number--) {
ptr1->next = (ptr)malloc(sizeof(ptr));
ptr1 = ptr1->next;
cin >> ptr1->xishu >> ptr1->zhishu;
ptr1->next = NULL;
}
return head;
}
ptr jia(ptr ptr1, ptr ptr2) {
ptr head = (ptr)malloc(sizeof(ye));
ptr zhizhen = head;
while (ptr1->next && ptr2->next) {
zhizhen->next = (ptr)malloc(sizeof(ye));
zhizhen = zhizhen->next;
if (ptr1->next->zhishu > ptr2->next->zhishu) {
*(zhizhen ) = *(ptr1->next);
ptr1 = ptr1->next;
} else if (ptr1->next->zhishu == ptr2->next->zhishu) {
zhizhen->xishu = ptr1->next->xishu + ptr2->next->xishu;
zhizhen->zhishu = ptr1->next->zhishu;
ptr1 = ptr1->next;
ptr2 = ptr2->next;
} else {
*(zhizhen) = *(ptr2->next);
ptr2 = ptr2->next;
}
}
if (ptr1->next) {
while (ptr1->next) {
zhizhen->next = (ptr)malloc(sizeof(ye));
*(zhizhen->next ) = *(ptr1->next);
ptr1 = ptr1->next;
zhizhen = zhizhen->next;
}
} else {
while (ptr2->next) {
zhizhen->next = (ptr)malloc(sizeof(ye));
*(zhizhen->next ) = *(ptr2->next);
ptr2 = ptr2->next;
zhizhen = zhizhen->next;
}
}
zhizhen->next = NULL;
return head;
}
void show(ptr head) {
int number = 0;
while (head->next) {
head = head->next;
if (head->xishu != 0) {
if ( number!=0)
cout << " ";
number++;
cout << head->xishu << " " << head->zhishu ;
}
}
if (!number)
cout << 0 << " " << 0;
}
ptr function(ptr ptr1, ptr ptr2) {
ptr head = (ptr)malloc(sizeof(ye));
ptr fuben = head;
while (ptr1->next) {
head->next = (ptr)malloc(sizeof(ye));
head->next->xishu = ptr1->next->xishu * (ptr2->xishu);
head->next->zhishu = ptr1->next->zhishu + ptr2->zhishu;
head = head->next;
ptr1 = ptr1->next;
}
return fuben;
}
ptr cheng(ptr ptr1, ptr ptr2) {
ptr head = (ptr)malloc(sizeof(ye));
ptr frank;
while (ptr1->next) {
frank = function(ptr2, ptr1->next);
head = jia(frank, head);
ptr1 = ptr1->next;
}
return head;
}
int main() {
ptr ptr1, ptr2, ocean;
int a, b;
cin >> a;
ptr1 = create(a);
cin >> b;
ptr2 = create(b);
ocean = cheng(ptr1, ptr2);
show(ocean);
cout << endl;
ocean = jia(ptr1, ptr2);
show(ocean);
cout<<endl;
}
03-树1 树的同构 (25 分)
//There is a heaven above you
#include <iostream>
using namespace std;
const int MAXN = 11;
typedef struct pair1 {
int num;
int root;
} pair1;
typedef struct ye {
char c;
int left;
int right;
} ye;
typedef ye *ptr;
pair1 create(ptr head) {
pair1 shuzi;
int s, s1, num = 0;
char a, b, c;
scanf("%d\n", &s);
int check[s];
for (int i = 0; i < s; i++)
check[i] = 0;
for (int i = 0; i < s; i++) {
scanf("%c %c %c\n", &a, &b, &c );
head[num].c = a;
if (b == '-') {
head[num].left = 10;
} else {
head[num].left = b - '0';
check[head[num].left]++;
}
if (c == '-') {
head[num].right = 10;
} else {
head[num].right = c - '0';
check[head[num].right]++;
}
num++;
}
head[10].c = '?';
for (int i = 0; i < s; i++)
if (!check[i])
shuzi.root = i;
shuzi.num = num;
return shuzi;
}
bool roses(ptr frank, int number1, ptr ocean, int number2) {
bool tyler = false;
if (frank[number1].c == '?' && ocean[number2].c == '?')
return true;
if (frank[number1].c == '?' || ocean[number2].c == '?')
return false;
if (frank[number1].c == ocean[number2].c) {
if (roses(frank, frank[number1].left, ocean, ocean[number2].left)
&& roses(frank, frank[number1].right, ocean, ocean[number2].right))
return true;
else if (roses(frank, frank[number1].left, ocean, ocean[number2].right)
&& roses(frank, frank[number1].right, ocean, ocean[number2].left))
return true;
}
}
int main() {
pair1 a, b;
bool zyb;
ye love[MAXN], kanye[MAXN];
a = create(love);
b = create(kanye);
if (a.num != b.num)
zyb = false;
else {
if (a.num == 0) {
cout << "Yes" << endl;
return 0;
}
zyb = roses(love, a.root, kanye, b.root);
}
if (zyb) {
cout << "Yes" << endl;
} else {
cout << "No" << endl;
}
}
03-树2 List Leaves (25 分)
#include <iostream>
using namespace std;
const int MAXN = 11;
typedef struct ye {
int left;
int right;
} ye;
int change(char c) {
if (c == '-')
return -1;
else
return c - '0';
}
int create(int number, ye *frank) {
int check[10] = {0};
char a, b;
for (int i = 0; i < number; i++) {
cin >> a >> b;
frank[i].left = change(a);
frank[i].right = change(b);
check[frank[i].left]++;
check[frank[i].right]++;
}
for (int i = 0; i < 10; i++) {
if (!check[i])
return i;
}
}
void *show_leaves(ye *frank, int root, int *s) {
/*if (frank[num].left == -1 && frank[num].right == -1) {
cout << num << " ";
}
if (frank[num].left != -1)
show_leaves(frank, frank[num].left);
if (frank[num].right != -1)
show_leaves(frank, frank[num].right);*/
//最开始想像上题一样遍历,后来发现人家要求层序遍历就用队列了
int duilie[1000] = {0};
int begin, last, number = 0;
for (int i = 0; i < 10; i++) {
s[i] = -1;
}
begin = 0, last = 1;
duilie[0] = root;//先把根放进去;
while (begin != last) {
if (frank[duilie[begin]].left != -1) {
duilie[last] = frank[duilie[begin]].left;
last++;
}
if (frank[duilie[begin]].right != -1) {
duilie[last] = frank[duilie[begin]].right;
last++;
}
if (frank[duilie[begin]].left == -1 && frank[duilie[begin]].right == -1) {
//cout << duilie[begin] << " ";
//最开始想直接输,很明显格式不对
s[number++] = duilie[begin];
}
begin++;
}
}
int main() {
ye frank[MAXN];
int a, root;
int s[10];
cin >> a;
root = create(a, frank);
show_leaves(frank, root, s);
for (int i = 0; s[i] != -1; i++) {
if (i)
cout << " ";
cout << s[i];
}
}