更新一部分代码
This commit is contained in:
parent
3f4d6cd144
commit
dda94299d0
17
1/data.out
17
1/data.out
|
@ -1,5 +1,12 @@
|
|||
1 4 3 2
|
||||
1 4 5
|
||||
8 2
|
||||
3 5 2
|
||||
4
|
||||
4 10 6 10
|
||||
4 10 6 10
|
||||
4 8 10 8
|
||||
4 8 8 10
|
||||
4 6 10 10
|
||||
10 8 6 6
|
||||
10 10 10
|
||||
8 6 6 10
|
||||
8 6 6 10
|
||||
6 6 10 8
|
||||
6 6 8 10
|
||||
11
|
|
@ -11,6 +11,8 @@ int solution_count = 0;
|
|||
void dfs(int t, int sum, int k) {
|
||||
if (sum == T) {
|
||||
solution_count++;
|
||||
if (solution_count > 50)
|
||||
return;
|
||||
int *re = malloc(sizeof(int) * k);
|
||||
int j = 0;
|
||||
for (int i = 0; i < k; i++) {
|
||||
|
|
|
@ -10,6 +10,8 @@ int solution_count = 0;
|
|||
|
||||
void printSubset(int *subset, int size) {
|
||||
solution_count++;
|
||||
if (solution_count > 50)
|
||||
return;
|
||||
for (int i = 0; i < size; i++) {
|
||||
printf("%d", subset[i]);
|
||||
if (i != size - 1) {
|
||||
|
@ -85,7 +87,7 @@ void sort(int *arr, int n) {
|
|||
|
||||
int main() {
|
||||
freopen("data.in", "r", stdin);
|
||||
// freopen("data.out", "w", stdout);
|
||||
freopen("data.out", "w", stdout);
|
||||
scanf("%d", &T);
|
||||
scanf("%d", &n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
|
@ -94,11 +96,10 @@ int main() {
|
|||
}
|
||||
|
||||
sort(w, n);
|
||||
for (int i = 0; i < n; i++) {
|
||||
printf("%d ", w[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
// for (int i = 0; i < n; i++) {
|
||||
// printf("%d ", w[i]);
|
||||
// }
|
||||
// printf("\n");
|
||||
|
||||
d(w, n, T);
|
||||
// printf("Total Solution Count: %d\n", solution_count);
|
||||
|
|
7
3/key.h
7
3/key.h
|
@ -1,2 +1,5 @@
|
|||
#define LEFT_ARROW 0x50
|
||||
#define RIGHT_ARROW 0x4F
|
||||
#define KEY_LEFT_ARROW 0x4B
|
||||
#define KEY_UP_ARROW 0x48
|
||||
#define KEY_RIGHT_ARROW 0x4D
|
||||
#define KEY_DOWN_ARROW 0x50
|
||||
#define KEY_ENTER 0x0D
|
381
3/main.c
381
3/main.c
|
@ -1,70 +1,381 @@
|
|||
#include "key.h"
|
||||
#include <conio.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define MAX_COLUMN 20
|
||||
#define MAX_ROW 20
|
||||
#define MAX_COLUMN_WIDTH 10
|
||||
#define MAX_DATA_LENGTH 100
|
||||
#define MAX_TICK 200
|
||||
#define MAX_MESSAGE_LENGTH 100
|
||||
#define FUNCTION_NUM 7
|
||||
|
||||
int tick = 0;
|
||||
int current_x = 0;
|
||||
int current_y = 0;
|
||||
int alive = 1;
|
||||
|
||||
char functions[FUNCTION_NUM][100] = {
|
||||
"Save", "Load", "Sum", "Average", "Max", "Min", "Sort",
|
||||
};
|
||||
|
||||
char *message = "Hi~";
|
||||
|
||||
typedef struct {
|
||||
int current_width;
|
||||
int current_height;
|
||||
int column_width[MAX_COLUMN];
|
||||
int row_height[MAX_ROW];
|
||||
|
||||
char data[MAX_ROW][MAX_COLUMN][MAX_DATA_LENGTH];
|
||||
} TableInfo;
|
||||
|
||||
void init_table(int width, int height, TableInfo *table) {
|
||||
int max(int a, int b) { return a > b ? a : b; }
|
||||
|
||||
int min(int a, int b) { return a < b ? a : b; }
|
||||
|
||||
char *int_to_string(int num) {
|
||||
char *str = (char *)malloc(sizeof(char) * 100);
|
||||
sprintf(str, "%d", num);
|
||||
return str;
|
||||
}
|
||||
|
||||
void init_table(int width, int height, int column_width, TableInfo *table) {
|
||||
table->current_width = width;
|
||||
table->current_height = height;
|
||||
for (int i = 0; i < width; i++) {
|
||||
table->column_width[i] = 5;
|
||||
table->column_width[i] = column_width;
|
||||
}
|
||||
for (int i = 0; i < height; i++) {
|
||||
table->row_height[i] = 5;
|
||||
for (int j = 0; j < width; j++) {
|
||||
table->data[i][j][0] = '\0';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void drawGUI(int currentSelection) {
|
||||
system("cls"); // 清除控制台屏幕
|
||||
void printTable(TableInfo *table) {
|
||||
system("cls");
|
||||
printf("Table v0.1 by Luthics\n");
|
||||
// render table
|
||||
for (int i = 0; i < table->current_height; i++) {
|
||||
for (int j = 0; j < table->current_width; j++) {
|
||||
printf("+");
|
||||
for (int k = 0; k < table->column_width[j]; k++) {
|
||||
printf("-");
|
||||
}
|
||||
}
|
||||
printf("+\n");
|
||||
for (int j = 0; j < table->current_width; j++) {
|
||||
printf("|");
|
||||
int data_length =
|
||||
table->data[i][j] == NULL ? 0 : strlen(table->data[i][j]);
|
||||
|
||||
printf("======== GUI ========\n");
|
||||
printf("| |\n");
|
||||
printf("| [ ] [ ] |\n");
|
||||
printf("| |\n");
|
||||
printf("=====================\n");
|
||||
|
||||
if (currentSelection == 0) {
|
||||
printf(" ^ \n"); // 显示指向第一个方块被选中
|
||||
if (data_length == 0) {
|
||||
if (i == current_y && j == current_x) {
|
||||
printf("_");
|
||||
} else {
|
||||
printf(" \n");
|
||||
printf(" ");
|
||||
}
|
||||
for (int k = 0; k < table->column_width[j] - 1; k++) {
|
||||
printf(" ");
|
||||
}
|
||||
} else {
|
||||
int add_ = 0;
|
||||
if (i == current_y && j == current_x) {
|
||||
// RED
|
||||
printf("\033[31m");
|
||||
// BOLD
|
||||
printf("\033[1m");
|
||||
}
|
||||
for (int k = 0; k < data_length; k++) {
|
||||
printf("%c", table->data[i][j][k]);
|
||||
}
|
||||
if (i == current_y && j == current_x) {
|
||||
// RESET
|
||||
printf("\033[0m");
|
||||
if (table->column_width[j] - data_length > 0) {
|
||||
printf("_");
|
||||
add_ = 1;
|
||||
}
|
||||
}
|
||||
for (int k = 0; k < table->column_width[j] - data_length - add_;
|
||||
k++) {
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
}
|
||||
printf("|\n");
|
||||
}
|
||||
for (int j = 0; j < table->current_width; j++) {
|
||||
printf("+");
|
||||
for (int k = 0; k < table->column_width[j]; k++) {
|
||||
printf("-");
|
||||
}
|
||||
}
|
||||
printf("+\n");
|
||||
|
||||
// render functions
|
||||
for (int i = 0; i < FUNCTION_NUM; i++) {
|
||||
if (current_y == -1 && i == current_x) {
|
||||
// RED
|
||||
printf("\033[31m");
|
||||
// BOLD
|
||||
printf("\033[1m");
|
||||
}
|
||||
printf("%s", functions[i]);
|
||||
if (current_y == -1 && i == current_x) {
|
||||
// RESET
|
||||
printf("\033[0m");
|
||||
}
|
||||
if (i != FUNCTION_NUM - 1) {
|
||||
printf(" ");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
printf("%s\n", message);
|
||||
}
|
||||
|
||||
int is_data_file_exist() {
|
||||
FILE *fp = fopen("table.dat", "r");
|
||||
if (fp == NULL) {
|
||||
return 0;
|
||||
} else {
|
||||
fclose(fp);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
TableInfo load_table_from_file(char *filename) {
|
||||
TableInfo table;
|
||||
FILE *fp = fopen(filename, "r");
|
||||
fread(&table, sizeof(TableInfo), 1, fp);
|
||||
fclose(fp);
|
||||
return table;
|
||||
}
|
||||
|
||||
int save_table_to_file(char *filename, TableInfo table) {
|
||||
FILE *fp = fopen(filename, "w");
|
||||
fwrite(&table, sizeof(TableInfo), 1, fp);
|
||||
fclose(fp);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void setCell(TableInfo *table, int row, int column, char *data) {
|
||||
if (row >= table->current_height || column >= table->current_width ||
|
||||
row < 0 || column < 0) {
|
||||
return;
|
||||
}
|
||||
strcpy(table->data[row][column], data);
|
||||
if (strlen(data) > table->column_width[column]) {
|
||||
table->column_width[column] = strlen(data);
|
||||
}
|
||||
}
|
||||
|
||||
int get_max_width_in_a_column(TableInfo *table, int column) {
|
||||
int max_width = 0;
|
||||
for (int i = 0; i < table->current_height; i++) {
|
||||
int data_length =
|
||||
table->data[i][column] == NULL ? 0 : strlen(table->data[i][column]);
|
||||
if (data_length > max_width) {
|
||||
max_width = data_length;
|
||||
}
|
||||
}
|
||||
return max(max_width, table->column_width[column]);
|
||||
}
|
||||
|
||||
void add_char_to_cell(TableInfo *table, int row, int column, char c) {
|
||||
if (row >= table->current_height || column >= table->current_width ||
|
||||
row < 0 || column < 0) {
|
||||
return;
|
||||
}
|
||||
int data_length = strlen(table->data[row][column]);
|
||||
if (data_length < MAX_DATA_LENGTH - 1) {
|
||||
table->data[row][column][data_length] = c;
|
||||
table->data[row][column][data_length + 1] = '\0';
|
||||
table->column_width[column] = get_max_width_in_a_column(table, column);
|
||||
}
|
||||
}
|
||||
|
||||
void del_char_from_cell(TableInfo *table, int row, int column) {
|
||||
if (row >= table->current_height || column >= table->current_width ||
|
||||
row < 0 || column < 0) {
|
||||
return;
|
||||
}
|
||||
int data_length = strlen(table->data[row][column]);
|
||||
if (data_length > 0) {
|
||||
table->data[row][column][data_length - 1] = '\0';
|
||||
table->column_width[column] = get_max_width_in_a_column(table, column);
|
||||
}
|
||||
}
|
||||
|
||||
// 返回 1 代表响应成功
|
||||
// 返回 0 代表响应失败
|
||||
int handleKeyPress(int key, TableInfo *table) {
|
||||
int status = 0;
|
||||
if (key == 224) { // 特殊键
|
||||
key = getch(); // 获取特殊键码
|
||||
if (key == KEY_LEFT_ARROW) {
|
||||
if (current_x > 0) {
|
||||
current_x--;
|
||||
} else if (current_x == 0) {
|
||||
if (current_y == -1) {
|
||||
current_x = FUNCTION_NUM - 1;
|
||||
} else {
|
||||
current_x = table->current_width - 1;
|
||||
}
|
||||
}
|
||||
status = 1;
|
||||
}
|
||||
if (key == KEY_RIGHT_ARROW) {
|
||||
if (current_y == -1) {
|
||||
current_x = (current_x + 1) % FUNCTION_NUM;
|
||||
} else {
|
||||
current_x = (current_x + 1) % table->current_width;
|
||||
}
|
||||
status = 1;
|
||||
}
|
||||
if (key == KEY_UP_ARROW) {
|
||||
if (current_y > 0) {
|
||||
current_y--;
|
||||
} else if (current_y == 0) {
|
||||
current_y = -1;
|
||||
current_x = min(FUNCTION_NUM - 1, current_x);
|
||||
} else if (current_y == -1) {
|
||||
current_y = table->current_height - 1;
|
||||
current_x = min(table->current_width - 1, current_x);
|
||||
}
|
||||
status = 1;
|
||||
}
|
||||
if (key == KEY_DOWN_ARROW) {
|
||||
if (current_y < table->current_height - 1 && current_y >= 0) {
|
||||
current_y++;
|
||||
} else if (current_y == table->current_height - 1) {
|
||||
// TO FUNCTIONS AREA
|
||||
current_y = -1;
|
||||
current_x = min(FUNCTION_NUM - 1, current_x);
|
||||
} else if (current_y == -1) {
|
||||
current_y = 0;
|
||||
current_x = min(table->current_width - 1, current_x);
|
||||
}
|
||||
status = 1;
|
||||
}
|
||||
} else {
|
||||
// ctrl + c
|
||||
if (key == 3) {
|
||||
alive = 0;
|
||||
message = "Bye!";
|
||||
status = 1;
|
||||
}
|
||||
// ctrl + s
|
||||
if (key == 19) {
|
||||
save_table_to_file("table.dat", *table);
|
||||
message = "Saved!";
|
||||
status = 1;
|
||||
}
|
||||
// 判断 key 是可打印字符
|
||||
if (key >= 32 && key <= 126) {
|
||||
add_char_to_cell(table, current_y, current_x, key);
|
||||
status = 1;
|
||||
}
|
||||
// 退格键
|
||||
if (key == 8) {
|
||||
del_char_from_cell(table, current_y, current_x);
|
||||
status = 1;
|
||||
}
|
||||
// ENTER
|
||||
if (key == KEY_ENTER) {
|
||||
if (current_y == -1) {
|
||||
if (current_x == 0) {
|
||||
save_table_to_file("table.dat", *table);
|
||||
message = "Saved!";
|
||||
}
|
||||
if (current_x == 1) {
|
||||
*table = load_table_from_file("table.dat");
|
||||
message = "Loaded!";
|
||||
}
|
||||
if (current_x == 2) {
|
||||
|
||||
}
|
||||
if (current_x == 3) {
|
||||
|
||||
}
|
||||
if (current_x == 4) {
|
||||
|
||||
}
|
||||
if (current_x == 5) {
|
||||
|
||||
}
|
||||
if (current_x == 6) {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (currentSelection == 1) {
|
||||
printf(" ^ \n"); // 显示指向第二个方块被选中
|
||||
} else {
|
||||
printf(" \n");
|
||||
}
|
||||
char *tmp_message = (char *)malloc(sizeof(char) * MAX_MESSAGE_LENGTH);
|
||||
sprintf(tmp_message, "x: %d, y: %d", current_x, current_y);
|
||||
message = tmp_message;
|
||||
return status;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int currentSelection = 0;
|
||||
int keyPressed;
|
||||
TableInfo table;
|
||||
int load_from_file = 0;
|
||||
|
||||
while (1) {
|
||||
drawGUI(currentSelection);
|
||||
|
||||
keyPressed = getch(); // 获取键盘输入
|
||||
|
||||
if (keyPressed == 224) { // 特殊键
|
||||
keyPressed = getch(); // 获取特殊键码
|
||||
|
||||
if (keyPressed == 75) { // ←键
|
||||
currentSelection = (currentSelection - 1) % 2; // 向左选择方块
|
||||
} else if (keyPressed == 77) { // →键
|
||||
currentSelection = (currentSelection + 1) % 2; // 向右选择方块
|
||||
if (is_data_file_exist()) {
|
||||
char tmp;
|
||||
printf("Load from file? ([Y]/n): ");
|
||||
scanf("%c", &tmp);
|
||||
if (tmp == 'n' || tmp == 'N') {
|
||||
load_from_file = 0;
|
||||
} else {
|
||||
load_from_file = 1;
|
||||
}
|
||||
}
|
||||
if (load_from_file) {
|
||||
table = load_table_from_file("table.dat");
|
||||
} else {
|
||||
printf("Let's create a table!\n");
|
||||
int width = 0, height = 0;
|
||||
int column_width = 0;
|
||||
while (width <= 0 || width > MAX_COLUMN) {
|
||||
printf("Input width (1-%d): ", MAX_COLUMN);
|
||||
scanf("%d", &width);
|
||||
}
|
||||
while (height <= 0 || height > MAX_ROW) {
|
||||
printf("Input height (1-%d): ", MAX_ROW);
|
||||
scanf("%d", &height);
|
||||
}
|
||||
while (column_width <= 0 || column_width > MAX_COLUMN_WIDTH) {
|
||||
printf("Input column width (1-%d): ", MAX_COLUMN_WIDTH);
|
||||
scanf("%d", &column_width);
|
||||
}
|
||||
|
||||
init_table(width, height, column_width, &table);
|
||||
}
|
||||
setCell(&table, 0, 0, "Hello");
|
||||
|
||||
while (alive) {
|
||||
printTable(&table);
|
||||
// drawGUI(currentSelection);
|
||||
|
||||
int keyPressed = getch(); // 获取键盘输入
|
||||
handleKeyPress(keyPressed, &table);
|
||||
|
||||
// tick = (tick + 1) % MAX_TICK;
|
||||
}
|
||||
|
||||
int save_to_file = 0;
|
||||
char tmp;
|
||||
printf("Save to file? ([Y]/n): ");
|
||||
scanf("%c", &tmp);
|
||||
if (tmp == 'n' || tmp == 'N') {
|
||||
save_to_file = 0;
|
||||
} else {
|
||||
save_to_file = 1;
|
||||
}
|
||||
if (save_to_file) {
|
||||
save_table_to_file("table.dat", table);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
Binary file not shown.
27
4/main.c
27
4/main.c
|
@ -5,14 +5,33 @@ int ans, a[10005];
|
|||
int n;
|
||||
int cx[100], zx[100], col[100];
|
||||
|
||||
void dfs(int x) {
|
||||
if (x > n) {
|
||||
ans++;
|
||||
if (ans <= 3) {
|
||||
void pt() {
|
||||
for (int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++) {
|
||||
if (a[i] == j) {
|
||||
printf("Q");
|
||||
} else {
|
||||
printf(".");
|
||||
}
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
printf("\n");
|
||||
for (int i = 1; i <= n; i++) {
|
||||
printf("%d ", a[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
void dfs(int x) {
|
||||
if (x > n) {
|
||||
ans++;
|
||||
if (ans <= 3) {
|
||||
pt();
|
||||
// for (int i = 1; i <= n; i++) {
|
||||
// printf("%d ", a[i]);
|
||||
// }
|
||||
// printf("\n");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
|
50
5/main.c
50
5/main.c
|
@ -1,31 +1,47 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int rs[105],n,m,xcz;
|
||||
int rs[105]; // 每个人的状态
|
||||
int n; // 人数
|
||||
|
||||
int zj(int a){
|
||||
int xcz; // 剩余人数
|
||||
int m[105], m0; // 间隔
|
||||
|
||||
int zj(int a) {
|
||||
// 寻找下一个活着的人
|
||||
a++;
|
||||
if(a>n) a-=n;
|
||||
while(rs[a]==0&&xcz>0){
|
||||
if (a > n)
|
||||
a -= n;
|
||||
while (rs[a] == 0 && xcz > 0) {
|
||||
a++;
|
||||
if(a>n) a-=n;
|
||||
if (a > n)
|
||||
a -= n;
|
||||
}
|
||||
return a;
|
||||
}
|
||||
|
||||
int main(){
|
||||
//freopen("title.txt","r",stdin);
|
||||
scanf("%d%d",&n,&m);
|
||||
xcz=n;
|
||||
for(int i=1;i<=n;i++) rs[i]=1;
|
||||
for(int i=m;xcz>0;){
|
||||
if(rs[i]==1){
|
||||
rs[i]=0;
|
||||
xcz--;
|
||||
printf("%d ",i);
|
||||
int main() {
|
||||
freopen("data.in", "r", stdin);
|
||||
scanf("%d%d", &n, &m0);
|
||||
for (int i = 1; i <= n; i++)
|
||||
scanf("%d", &m[i]);
|
||||
xcz = n;
|
||||
for (int i = 1; i <= n; i++)
|
||||
// 把每个人设置为活着
|
||||
rs[i] = 1;
|
||||
while (m0 > n) {
|
||||
m0 -= n;
|
||||
}
|
||||
int ls=m;
|
||||
while(ls--) i=zj(i);
|
||||
for (int i = m0; xcz > 0;) {
|
||||
if (rs[i] == 1) {
|
||||
rs[i] = 0;
|
||||
xcz--;
|
||||
printf("%d ", i);
|
||||
}
|
||||
int ls = m[i];
|
||||
// printf("ls=%d\n", ls);
|
||||
while (ls--)
|
||||
i = zj(i);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,128 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef struct Tree {
|
||||
int val;
|
||||
struct Tree *l, *r;
|
||||
struct Tree *parent;
|
||||
} Tree;
|
||||
|
||||
int n;
|
||||
int m;
|
||||
|
||||
void makeTree(Tree *root, int val) {
|
||||
if (val < root->val) {
|
||||
if (root->l == NULL) {
|
||||
root->l = (Tree *)malloc(sizeof(Tree));
|
||||
root->l->val = val;
|
||||
root->l->l = NULL;
|
||||
root->l->r = NULL;
|
||||
root->l->parent = root;
|
||||
} else {
|
||||
makeTree(root->l, val);
|
||||
}
|
||||
} else {
|
||||
if (root->r == NULL) {
|
||||
root->r = (Tree *)malloc(sizeof(Tree));
|
||||
root->r->val = val;
|
||||
root->r->l = NULL;
|
||||
root->r->r = NULL;
|
||||
root->r->parent = root;
|
||||
} else {
|
||||
makeTree(root->r, val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 中序遍历
|
||||
void inOrder(Tree *root) {
|
||||
if (root == NULL)
|
||||
return;
|
||||
inOrder(root->l);
|
||||
printf("%d ", root->val);
|
||||
inOrder(root->r);
|
||||
}
|
||||
|
||||
// 二叉排序树T查找成功的平均查找长度
|
||||
double ASL(Tree *root, int level) {
|
||||
if (root == NULL)
|
||||
return 0;
|
||||
return level + ASL(root->l, level + 1) + ASL(root->r, level + 1);
|
||||
}
|
||||
|
||||
// 找到二叉树中值为val的节点并删除
|
||||
void delNode(Tree *root, int val) {
|
||||
if (root == NULL)
|
||||
return;
|
||||
if (root->val == val) {
|
||||
if (root->l == NULL && root->r == NULL) {
|
||||
if (root->parent->l == root) {
|
||||
root->parent->l = NULL;
|
||||
} else {
|
||||
root->parent->r = NULL;
|
||||
}
|
||||
free(root);
|
||||
} else if (root->l == NULL) {
|
||||
if (root->parent->l == root) {
|
||||
root->parent->l = root->r;
|
||||
} else {
|
||||
root->parent->r = root->r;
|
||||
}
|
||||
free(root);
|
||||
} else if (root->r == NULL) {
|
||||
if (root->parent->l == root) {
|
||||
root->parent->l = root->l;
|
||||
} else {
|
||||
root->parent->r = root->l;
|
||||
}
|
||||
free(root);
|
||||
} else {
|
||||
Tree *p = root->r;
|
||||
while (p->l != NULL) {
|
||||
p = p->l;
|
||||
}
|
||||
root->val = p->val;
|
||||
delNode(p, p->val);
|
||||
}
|
||||
} else if (root->val > val) {
|
||||
delNode(root->l, val);
|
||||
} else {
|
||||
delNode(root->r, val);
|
||||
}
|
||||
}
|
||||
|
||||
void pt(Tree *root) {
|
||||
printf("inOrder:\n");
|
||||
inOrder(root);
|
||||
printf("\n");
|
||||
printf("ASL: %lf\n", ASL(root, 1) / n);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
freopen("data.in", "r", stdin);
|
||||
scanf("%d", &n);
|
||||
Tree *root = (Tree *)malloc(sizeof(Tree));
|
||||
root->parent = NULL;
|
||||
for (int i = 0; i < n; i++) {
|
||||
int val;
|
||||
scanf("%d", &val);
|
||||
if (i == 0) {
|
||||
root->val = val;
|
||||
root->l = NULL;
|
||||
root->r = NULL;
|
||||
} else {
|
||||
makeTree(root, val);
|
||||
}
|
||||
}
|
||||
pt(root);
|
||||
|
||||
scanf("%d", &m);
|
||||
for (int i = 0; i < m; i++) {
|
||||
int x;
|
||||
scanf("%d", &x);
|
||||
delNode(root, x);
|
||||
pt(root);
|
||||
}
|
||||
return 0;
|
||||
}
|
|
@ -0,0 +1,106 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
int n;
|
||||
int m;
|
||||
// 二叉排序树,根节点为 1,左子树为 2 * i,右子树为 2 * i + 1
|
||||
int tree[1000];
|
||||
|
||||
void makeTree(int val) {
|
||||
int r = 1;
|
||||
while (1) {
|
||||
if (val < tree[r]) {
|
||||
if (tree[2 * r] == 0) {
|
||||
// printf("r: %d, val: %d\n", 2 * r, val);
|
||||
tree[2 * r] = val;
|
||||
break;
|
||||
} else {
|
||||
r = 2 * r;
|
||||
}
|
||||
} else {
|
||||
if (tree[2 * r + 1] == 0) {
|
||||
// printf("r: %d, val: %d\n", 2 * r + 1, val);
|
||||
tree[2 * r + 1] = val;
|
||||
break;
|
||||
} else {
|
||||
r = 2 * r + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 中序遍历
|
||||
void inOrder(int root) {
|
||||
if (tree[2 * root] != 0)
|
||||
inOrder(2 * root);
|
||||
printf("%d ", tree[root]);
|
||||
if (tree[2 * root + 1] != 0)
|
||||
inOrder(2 * root + 1);
|
||||
}
|
||||
|
||||
// 二叉排序树T查找成功的平均查找长度
|
||||
double ASL(int root, int level) {
|
||||
if (tree[root] == 0)
|
||||
return 0;
|
||||
return level + ASL(2 * root, level + 1) + ASL(2 * root + 1, level + 1);
|
||||
}
|
||||
|
||||
// 找到二叉树中值为val的节点并删除
|
||||
void delNode(int root, int x) {
|
||||
if (tree[root] == 0)
|
||||
return;
|
||||
if (tree[root] == x) {
|
||||
if (tree[2 * root] == 0 && tree[2 * root + 1] == 0) {
|
||||
tree[root] = 0;
|
||||
} else if (tree[2 * root] != 0 && tree[2 * root + 1] == 0) {
|
||||
tree[root] = tree[2 * root];
|
||||
tree[2 * root] = 0;
|
||||
} else if (tree[2 * root] == 0 && tree[2 * root + 1] != 0) {
|
||||
tree[root] = tree[2 * root + 1];
|
||||
tree[2 * root + 1] = 0;
|
||||
} else {
|
||||
int r = 2 * root + 1;
|
||||
while (tree[2 * r] != 0) {
|
||||
r = 2 * r;
|
||||
}
|
||||
tree[root] = tree[r];
|
||||
tree[r] = 0;
|
||||
}
|
||||
} else if (tree[root] > x) {
|
||||
delNode(2 * root, x);
|
||||
} else {
|
||||
delNode(2 * root + 1, x);
|
||||
}
|
||||
}
|
||||
|
||||
void pt() {
|
||||
printf("inOrder:\n");
|
||||
inOrder(1);
|
||||
printf("\n");
|
||||
printf("ASL: %lf\n", ASL(1, 1) / n);
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
int main() {
|
||||
freopen("data.in", "r", stdin);
|
||||
scanf("%d", &n);
|
||||
for (int i = 1; i <= n; i++) {
|
||||
int val;
|
||||
scanf("%d", &val);
|
||||
if (i == 1) {
|
||||
tree[1] = val;
|
||||
} else {
|
||||
makeTree(val);
|
||||
}
|
||||
}
|
||||
pt();
|
||||
|
||||
scanf("%d", &m);
|
||||
for (int i = 0; i < m; i++) {
|
||||
int x;
|
||||
scanf("%d", &x);
|
||||
delNode(1, x);
|
||||
pt();
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue