修改文件结构。
This commit is contained in:
62
POP/11/Exercise01.c
Normal file
62
POP/11/Exercise01.c
Normal file
@@ -0,0 +1,62 @@
|
||||
#include <stdio.h>
|
||||
void sort(int *start, int length);
|
||||
void swap(int *a, int *b);
|
||||
|
||||
int main() {
|
||||
int a[10], b[10], result[20];
|
||||
int *aIndex = a, *bIndex = b, *resultIndex = result;
|
||||
for (aIndex = a; aIndex < a+10; aIndex++) {
|
||||
scanf("%d", aIndex);
|
||||
}
|
||||
for (bIndex = b; bIndex < b + 10; bIndex++) {
|
||||
scanf("%d", bIndex);
|
||||
}
|
||||
aIndex = a;
|
||||
bIndex = b;
|
||||
sort(a, 10);
|
||||
sort(b, 10);
|
||||
aIndex = a;
|
||||
bIndex = b;
|
||||
while (resultIndex < result + 20) {
|
||||
if (*aIndex <= *bIndex || bIndex > b + 9) {
|
||||
// aIndex不能越界,要改
|
||||
*resultIndex = *aIndex;
|
||||
aIndex++;
|
||||
}
|
||||
else if (*bIndex < *aIndex || aIndex > a + 9) {
|
||||
*resultIndex = *bIndex;
|
||||
bIndex++;
|
||||
}
|
||||
resultIndex++;
|
||||
}
|
||||
resultIndex = result;
|
||||
while (resultIndex < result+20) {
|
||||
printf("%d ", *resultIndex);
|
||||
resultIndex++;
|
||||
}
|
||||
printf("\n");
|
||||
return 0;
|
||||
}
|
||||
|
||||
void sort(int *start, int length) {
|
||||
int moved = 1;
|
||||
int *index = start;
|
||||
while (moved) {
|
||||
moved = 0;
|
||||
index = start;
|
||||
while (index < start + length - 1) {
|
||||
if ( *index > *(index + 1)) {
|
||||
moved = 1;
|
||||
swap(index, index+1);
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void swap(int *a, int *b) {
|
||||
int tmp;
|
||||
tmp = *a;
|
||||
*a = *b;
|
||||
*b = tmp;
|
||||
}
|
||||
Reference in New Issue
Block a user