输入样例:
5
3 4 1 6 7
6
5 2 7 9 3 6
0
输出样例:
1
3 4 6 7
2
5 6 7 9 3
error
#include
#define MAX_SIZE 100
typedef struct SqList {
int data[MAX_SIZE];
int length;
} SqList;
void ListPrint(SqList L);
int ListDelete(SqList *L);
int main() {
SqList L;
while (scanf("%d", &L.length) && L.length!= 0) {
for (int i = 0; i < L.length; i++) {
scanf("%d", &(L.data[i]));
}
printf("%d\n", ListDelete(&L));
ListPrint(L);
}
printf("error");
return 0;
}
void ListPrint(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d%c", L.data[i], (i == L.length - 1) ? 10 : 32);
}
}
int ListDelete(SqList *L) {
int min_pos = 0, min_val = 0;
for (int i = 0; i < L->length; i++) {
if (L->data[i] < L->data[min_pos]) min_pos = i;
}
min_val = L->data[min_pos];
L->data[min_pos] = L->data[L->length - 1];
L->length--;
return min_val;
}
输入样例:
5
3 4 1 6 7
6
5 2 7 9 3 6
0
输出样例:
7 6 1 4 3
6 3 9 7 2 5
#include
#define MAX_SIZE 100
typedef struct SqList {
int data[MAX_SIZE];
int length;
} SqList;
void ListPrint(SqList L);
void ListInvert(SqList *L);
int main() {
SqList L;
while (scanf("%d", &L.length) && L.length!= 0) {
for (int i = 0; i < L.length; i++) {
scanf("%d", &(L.data[i]));
}
ListInvert(&L);
ListPrint(L);
}
return 0;
}
void ListPrint(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d%c", L.data[i], (i == L.length - 1) ? 10 : 32);
}
}
void ListInvert(SqList *L) {
for (int i = 0; i < L->length / 2; i++) {
L->data[i] = L->data[i] + L->data[L->length - i - 1];
L->data[L->length - i - 1] = L->data[i] - L->data[L->length - i - 1];
L->data[i] = L->data[i] - L->data[L->length - i - 1];
}
}
输入样例:
5
3 5 8 3 1
3
5
3 5 8 3 1
8
0
输出样例:
5 8 1
3 5 3 1
#include
#define MAX_SIZE 100
typedef struct SqList {
int data[MAX_SIZE];
int length;
} SqList;
void ListPrint(SqList L);
void ListDelete(SqList *L, int x);
void ListDelete_(SqList *L, int x);
int main() {
SqList L;
while (scanf("%d", &L.length) && L.length!= 0) {
int x;
for (int i = 0; i < L.length; i++) {
scanf("%d", &(L.data[i]));
}
scanf("%d", &x);
ListDelete(&L, x);
/*ListDelete_(&L, x);*/
ListPrint(L);
}
return 0;
}
void ListPrint(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d%c", L.data[i], (i == L.length - 1) ? 10 : 32);
}
}
void ListDelete(SqList *L, int x) {
int cursor = 0, offset = 0;//offset记录等于x的元素的个数
while (cursor + offset < L->length) {
if (L->data[cursor + offset] == x) {
offset++;
} else {
L->data[cursor] = L->data[cursor + offset];//将当前不等于x的元素向前偏移offset
cursor++;
};
}
L->length -= offset;//更新顺序表长度
}
void ListDelete_(SqList *L, int x) {
int slow = 0;
for (int fast = 0; fast < L->length; fast++) {
if (L->data[fast] != x) {
L->data[slow] = L->data[fast];
slow++;
}
}
L->length = slow;//更新顺序表长度
}
输入样例:
5
1 2 3 4 5
2 4
6
5 6 7 8 9 10
8 10
0
输出样例:
1 5
5 6 7
error
#include
#define MAX_SIZE 100
typedef struct SqList {
int data[MAX_SIZE];
int length;
} SqList;
void ListPrint(SqList L);
void ListDelete(SqList *L, int s, int t);
int main() {
SqList L;
while (scanf("%d", &L.length) && L.length!= 0) {
int s, t;
for (int i = 0; i < L.length; i++) {
scanf("%d", &(L.data[i]));
}
scanf("%d %d", &s, &t);
if (s >= t) break;
ListDelete(&L, s, t);
ListPrint(L);
}
printf("error");
return 0;
}
void ListPrint(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d%c", L.data[i], (i == L.length - 1) ? 10 : 32);
}
}
void ListDelete(SqList *L, int s, int t) {
int slow = 0;
for (int fast = 0; fast < L->length; fast++) {
if (L->data[fast] < s || L->data[fast] > t) {
L->data[slow] = L->data[fast];
slow++;
}
}
L->length = slow;
}
输入样例:
5
3 4 1 6 7
4 6
6
5 2 7 9 3 6
7 9
0
输出样例:
3 1 7
5 2 3 6
#include
#define MAX_SIZE 100
typedef struct SqList {
int data[MAX_SIZE];
int length;
} SqList;
void ListPrint(SqList L);
void ListDelete(SqList *L, int s, int t);
int main() {
SqList L;
while (scanf("%d", &L.length) && L.length!= 0) {
int s, t;
for (int i = 0; i < L.length; i++) {
scanf("%d", &(L.data[i]));
}
scanf("%d %d", &s, &t);
if (s >= t) break;
ListDelete(&L, s, t);
ListPrint(L);
}
printf("error");
return 0;
}
void ListPrint(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d%c", L.data[i], (i == L.length - 1) ? 10 : 32);
}
}
void ListDelete(SqList *L, int s, int t) {
int slow = 0;
for (int fast = 0; fast < L->length; fast++) {
if (L->data[fast] < s || L->data[fast] > t) {
L->data[slow] = L->data[fast];
slow++;
}
}
L->length = slow;
}
输入样例:
5
1 2 2 2 3
10
1 2 2 2 3 4 4 4 5 5
输出样例:
1 2 3
1 2 3 4 5
#include
#define MAX_SIZE 10
typedef struct SqList {
int data[MAX_SIZE];
int length;
} SqList;
void ListPrint(SqList L);
void ListDelete(SqList *L);
void ListDelete_(SqList *L);
int main() {
SqList L;
while (scanf("%d", &L.length) && L.length!= 0) {
for (int i = 0; i < L.length; i++) {
scanf("%d", &(L.data[i]));
}
/*ListDelete(&L);*/
ListDelete_(&L);
ListPrint(L);
}
printf("error");
return 0;
}
void ListPrint(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d%c", L.data[i], (i == L.length - 1) ? 10 : 32);
}
}
/*对有序表和无序表都适用*/
void ListDelete(SqList *L) {
for (int i = 0; i < L->length; i++) {
int same = 0;//重复元素的个数
/*将当前元素与后面的所有元素进行比较*/
for (int j = i + 1; j < L->length; j++) {
if (L->data[i] == L->data[j]) {
same++;
} else {
L->data[j - same] = L->data[j];
}
}
L->length -= same;
}
}
/*只能处理有序表*/
void ListDelete_(SqList *L) {
int slow = 0, fast = 1;
while (fast < L->length) {
if (L->data[slow] != L->data[fast]) {
L->data[++slow] = L->data[fast];
}
fast++;
}
L->length = slow + 1;
}
输入样例:
3 4
1 3 5
2 4 6 8
0 0
输出样例:
1 2 3 4 5 6 8
#include
#define MAX_SIZE 10
typedef struct SqList {
int data[MAX_SIZE];
int length;
} SqList;
void ListPrint(SqList L);
void ListMerge(SqList A, SqList B, SqList *L);
int main() {
SqList A;
SqList B;
while (scanf("%d %d", &A.length, &B.length) && A.length != 0 && B.length != 0) {
SqList L;
L.length = 0;
for (int i = 0; i < A.length; i++) {
scanf("%d", &(A.data[i]));
}
for (int i = 0; i < B.length; i++) {
scanf("%d", &(B.data[i]));
}
ListMerge(A, B, &L);
ListPrint(L);
}
return 0;
}
void ListPrint(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d%c", L.data[i], (i == L.length - 1) ? 10 : 32);
}
}
void ListMerge(SqList A, SqList B, SqList *L) {
int cur_a = 0, cur_b = 0, cursor = 0;
while (cur_a < A.length && cur_b < B.length) {
if (A.data[cur_a] < B.data[cur_b]) {
L->data[cursor++] = A.data[cur_a++];
L->length++;
} else {
L->data[cursor++] = B.data[cur_b++];
L->length++;
}
}
while (cur_a < A.length) {
L->data[cursor++] = A.data[cur_a++];
L->length++;
}
while (cur_b < B.length) {
L->data[cursor++] = B.data[cur_b++];
L->length++;
}
}
输入样例:
2 3
1 2 3 4 5
2 5
1 2 3 4 5 6 7
0 0
输出样例:
3 4 5 1 2
3 4 5 6 7 1 2
#include
#define MAX_SIZE 10
typedef struct SqList {
int data[MAX_SIZE];
int length;
} SqList;
void ListPrint(SqList L);
void ListInvert(SqList *L, int start, int end);
void ListSwap(SqList *L, int m, int n);
int main() {
int m, n;
SqList L;
while (scanf("%d %d", &m, &n) && m != 0 && n != 0) {
L.length = m + n;
for (int i = 0; i < L.length; i++) {
scanf("%d", &(L.data[i]));
}
ListSwap(&L, m, n);
ListPrint(L);
}
return 0;
}
void ListPrint(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d%c", L.data[i], (i == L.length - 1) ? 10 : 32);
}
}
void ListSwap(SqList *L, int m, int n) {
ListInvert(L, 0, L->length - 1);
ListInvert(L, 0, n - 1);
ListInvert(L, L->length - m, L->length - 1);
}
void ListInvert(SqList *L, int start, int end) {
for (int i = start; i <= (start + end) / 2; i++) {
int tmp = L->data[i];
L->data[i] = L->data[start + end - i];
L->data[start + end - i] = tmp;
}
}
输入样例:
5
1 3 5 7 9
7
5
1 3 5 7 9
6
0
输出样例:
1 3 5 9 7
1 3 5 6 7 9
#include
#define MAX_SIZE 10
typedef struct SqList {
int data[MAX_SIZE];
int length;
} SqList;
void ListPrint(SqList L);
void SearchExchangeInsert(SqList *L, int x);
int BinarySearch(SqList L, int key);
int main() {
SqList L;
while (scanf("%d", &(L.length)) && L.length != 0) {
int x;
for (int i = 0; i < L.length; i++) {
scanf("%d", &(L.data[i]));
}
scanf("%d", &x);
SearchExchangeInsert(&L, x);
ListPrint(L);
}
return 0;
}
void ListPrint(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d%c", L.data[i], (i == L.length - 1) ? 10 : 32);
}
}
void SearchExchangeInsert(SqList *L, int x) {
int pos = BinarySearch(*L, x);
if (pos != -1) {
/*exchange*/
L->data[pos] = L->data[pos] + L->data[pos + 1];
L->data[pos + 1] = L->data[pos] - L->data[pos + 1];
L->data[pos] = L->data[pos] - L->data[pos + 1];
} else {
/*insert*/
int i = L->length - 1;
while (L->data[i] > x) {
L->data[i + 1] = L->data[i];
i--;
}
L->data[i + 1] = x;
L->length++;
}
}
int BinarySearch(SqList L, int key) {
int low = 0, high = L.length - 1, mid;
while (low < high) {
mid = (low + high) / 2;
if (L.data[mid] == key) {
return mid;
} else if (L.data[mid] > key) {
high = mid - 1;//继续查找前半部分
} else {
low = mid + 1;//继续查找后半部分
}
}
return -1;//查找失败
}
( X 0 , X 1 , … , X n − 1 ) (X_0, X_1, \dots, X_{n-1}) (X0,X1,…,Xn−1)变换为 ( X p , X p + 1 , … , X n − 1 , X 0 , X 1 , … , X p − 1 ) (X_p, X_{p+1}, \dots, X_{n-1},X_0, X_1, \dots, X_{p-1}) (Xp,Xp+1,…,Xn−1,X0,X1,…,Xp−1)。
输入样例:
5
3 4 1 6 7
3
6
5 2 7 9 3 6
5
0
输出样例:
6 7 3 4 1
6 5 2 7 9 3
#include
#define MAX_SIZE 10
typedef struct SqList {
int data[MAX_SIZE];
int length;
} SqList;
void ListPrint(SqList L);
void ListReverse(SqList *L, int low, int high);
void ListMove(SqList *L, int dis);
int main() {
SqList L;
int dis;
while (scanf("%d", &(L.length)) && L.length != 0) {
for (int i = 0; i < L.length; i++) {
scanf("%d", &(L.data[i]));
}
scanf("%d", &dis);
ListMove(&L, dis);
ListPrint(L);
}
return 0;
}
void ListPrint(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d%c", L.data[i], (i == L.length - 1) ? 10 : 32);
}
}
void ListMove(SqList *L, int dis) {
ListReverse(L, 0, L->length - 1);
ListReverse(L, 0, L->length - dis - 1);
ListReverse(L, L->length - dis, L->length - 1);
}
void ListReverse(SqList *L, int low, int high) {
for (int i = low; i <= (high + low) / 2; i++) {
int tmp = L->data[i];
L->data[i] = L->data[high + low - i];
L->data[high + low - i] = tmp;
}
}
输入样例:
3 3
1 5 8
2 4 6
5 5
1 3 4 6 7
2 3 5 6 7
0 0
输出样例:
4
4
#include
#define MAX_SIZE 10
typedef struct SqList {
int data[MAX_SIZE];
int length;
} SqList;
void ListPrint(SqList L);
int FindMedian(SqList A, SqList B);
int main() {
SqList A;
SqList B;
while (scanf("%d %d", &(A.length), &(B.length)) && A.length != 0 && B.length != 0) {
for (int i = 0; i < A.length; i++) {
scanf("%d", &(A.data[i]));
}
for (int i = 0; i < B.length; i++) {
scanf("%d", &(B.data[i]));
}
printf("%d\n", FindMedian(A, B));
}
return 0;
}
void ListPrint(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d%c", L.data[i], (i == L.length - 1) ? 10 : 32);
}
}
int FindMedian(SqList A, SqList B) {
int count = A.length;
int cur_a = 0, cur_b = 0;
int median;
while (count-- > 0) {
median = (A.data[cur_a] < B.data[cur_b]) ? A.data[cur_a++] : B.data[cur_b++];
}
return median;
}
输入样例:
8
0 5 5 3 5 7 5 5
8
0 5 5 3 5 1 5 7
0
输出样例:
5
-1
#include
#define MAX_SIZE 100
typedef struct SqList {
int data[MAX_SIZE];
int length;
} SqList;
void ListPrint(SqList L);
int GetMainEle(SqList L);
int main() {
SqList L;
while (scanf("%d", &(L.length)) && L.length != 0) {
for (int i = 0; i < L.length; i++) {
scanf("%d", &(L.data[i]));
}
printf("%d", GetMainEle(L));
}
return 0;
}
void ListPrint(SqList L) {
for (int i = 0; i < L.length; i++) {
printf("%d%c", L.data[i], (i == L.length - 1) ? 10 : 32);
}
}
int GetMainEle(SqList L) {
int count[MAX_SIZE] = {0};
for (int i = 0; i < L.length; i++) {
count[L.data[i]]++;
}
for (int i = 0; i < MAX_SIZE; i++) {
if (count[i] > L.length / 2)
return i;
}
return -1;
}
输入样例:
5
-5 2 9 7 8
5
1 2 3 4 5
0
输出样例:
1
6
#include
#define MaxSize 100
typedef struct SqList {
int data[MaxSize];
int len;
} SqList;
void ListQuickSort(SqList *sqList, int low, int high);
int ListPartition(SqList *sqList, int low, int high);
int GetMin(SqList sqList) {
int minValue = 1;
ListQuickSort(&sqList, 0, sqList.len - 1);
for (int i = 0; i < sqList.len; i++) {
if (minValue == sqList.data[i]) {
minValue++;
}
}
return minValue;
}
int main() {
SqList L;
while (scanf("%d%*c", &(L.len)) && L.len != 0) {
for (int i = 0; i < L.len; i++) {
scanf("%d%*c", &(L.data[i]));
}
printf("%d\n", GetMin(L));
}
return 0;
}
/**
* quick sort
* @param sqList
* @param low
* @param high
*/
void ListQuickSort(SqList *sqList, int low, int high) {
if (low < high) {
int pivotPosition = ListPartition(sqList, low, high);
ListQuickSort(sqList, low, pivotPosition - 1);
ListQuickSort(sqList, pivotPosition + 1, high);
}
}
/**
* separate all data by sqList->data[low]
* @param sqList
* @param low
* @param high
* @return
*/
int ListPartition(SqList *sqList, int low, int high) {
int pivotValue = sqList->data[low];
while (low < high) {
while (low < high && sqList->data[high] > pivotValue) {
high--;
}
sqList->data[low] = sqList->data[high];
while (low < high && sqList->data[low] < pivotValue) {
low++;
}
sqList->data[high] = sqList->data[low];
}
sqList->data[low] = pivotValue;
return low;//return pivot position
}
输入样例:
3 4 5
-1 0 9
-25 -10 10 11
2 9 17 30 41
0 0 0
输出样例:
2
#include
#include
#define MaxSize 100
typedef struct SqList {
int data[MaxSize];
int len;
} SqList;
int GetDistance(int x, int y, int z);
bool IsMinValue(int x, int y, int z);
int GetMin(SqList sqList1, SqList sqList2, SqList sqList3) {
int i, j, k, minDistance = 0x7FFFFFFF;
i = j = k = 0;
while (i < sqList1.len && j < sqList2.len && k < sqList3.len) {
int nowDistance = GetDistance(sqList1.data[i], sqList2.data[j], sqList3.data[k]);
minDistance = (minDistance < nowDistance) ? minDistance : nowDistance;
if (IsMinValue(sqList1.data[i], sqList2.data[j], sqList3.data[k])) {
i++;
} else if (IsMinValue(sqList2.data[j], sqList1.data[i], sqList3.data[k])) {
j++;
} else {
k++;
}
}
return minDistance;
}
int main() {
SqList sqList1, sqList2, sqList3;
while (scanf("%d %d %d%*c", &(sqList1.len), &(sqList2.len), &(sqList3.len)) && sqList1.len && sqList2.len && sqList3.len) {
for (int i = 0; i < sqList1.len; i++) {
scanf("%d%*c", &(sqList1.data[i]));
}
for (int i = 0; i < sqList2.len; i++) {
scanf("%d%*c", &(sqList2.data[i]));
}
for (int i = 0; i < sqList3.len; i++) {
scanf("%d%*c", &(sqList3.data[i]));
}
printf("%d\n", GetMin(sqList1, sqList2, sqList3));
}
return 0;
}
/**
* get the distance
* @param x
* @param y
* @param z
* @return |x - y| + |x - z| + |y - z|
*/
int GetDistance(int x, int y, int z) {
int distance = 0;
distance += (x > y) ? (x - y) : (y - x);
distance += (x > z) ? (x - z) : (z - x);
distance += (y > z) ? (y - z) : (z - y);
return distance;
}
/**
* judge whether x is the minimum value
* @param x
* @param y
* @param z
* @return
*/
bool IsMinValue(int x, int y, int z) {
return (x <= y && x <= z) ? true : false;
}