不准访问
- 积分
- 400
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2012-6-20
- 最后登录
- 1970-1-1
|
Problem 1:- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define MAX 50000
- int tabsize = MAX;
- struct node
- {
- int label;
- struct node * next;
- };
- struct node tab[MAX], count[MAX], test[MAX];
- void init();
- int hash(int);
- void insert(int, struct node *);
- int lookup(int, struct node *);
- void del(int, struct node *);
- int size(struct node *);
- void read_data(char *);
- int main(void)
- {
- init();
- read_data("D:\\HashInt.txt");
- insert(1, test);
- size(test);
- insert(1, test);
- size(test);
- insert(1, test);
- size(test);
- size_t begin = time(NULL);
- int x;
- for(x = 0; x < MAX; x++)
- {
- struct node *temp = tab + x;
- if(temp->label == 0)
- continue;
- else
- {
- while(temp != NULL)
- {
- //printf("%d ->", temp->label);
- int y;
- for(y = 2500; y <= 4000; y++)
- {
- if(y - temp->label != temp->label && y - temp->label > 0)
- {
- if(lookup(y - temp->label, tab))
- {
- insert(y, count);
- }
- }
- }
- temp = temp->next;
- }
- }
- }
- printf("Time spends : %ld\n", time(NULL) - begin);
- printf("The distinct Value is: %d\n", size(count));
- return 0;
- }
- void init()
- {
- int x;
- for(x = 0; x < MAX; x++)
- {
- tab[x].label = 0;
- tab[x].next = NULL;
- count[x].label = 0;
- count[x].next = NULL;
- test[x].label = 0;
- test[x].next = NULL;
- }
- }
- int hash(int label)
- {
- return label % tabsize;
- }
- void insert(int label, struct node *data)
- {
- int index = hash(label);
- struct node * temp = data + index;
- if(temp->label != 0)
- {
- // Collision happened.
- /*
- |Index|Label|Link| Overflow area
- | 0 | 1 | ->-> |3|->-> |5|->-> |7|x
- | 1 | 2 | ->-> |4|->-> |6|x
- */
- if(temp->label == label)
- return;
- while(temp->next != NULL)
- {
- // already in tab, do nothing.
- if(temp->next->label == label)
- return;
- temp = temp->next;
- }
- // Add a new node to the end of collision list.
- struct node * new_node = malloc(sizeof(struct node));
- new_node->label = label;
- new_node->next = NULL;
- temp->next = new_node;
- }
- else
- {
- temp->label = label;
- }
- }
- int lookup(int label, struct node * data)
- {
- int sign = 0;
- struct node * temp = data + hash(label);
- while(temp != NULL)
- {
- if(temp->label == label)
- {
- sign = 1;
- return sign;
- }
- temp = temp->next;
- }
- return sign;
- }
- void del(int label, struct node * data)
- {
- struct node * temp = data + hash(label);
- if(temp->label == label)
- {
- temp->label = 0;
- return;
- }
- else
- {
- while(temp->next != NULL)
- {
- if(temp->next->label == label)
- {
- if(temp->next->next == NULL)
- {
- free(temp->next);
- temp->next = NULL;
- return;
- }
- else
- {
- struct node * temp_next_next = temp->next->next;
- free(temp->next);
- temp->next = temp_next_next;
- return;
- }
- }
- temp = temp->next;
- }
- }
- }
- int size(struct node * data)
- {
- int x;
- int c = 0;
- for(x = 0; x < MAX; x++)
- {
- struct node * temp = data + x;
- if(temp->label == 0)
- continue;
- else
- {
- while(temp != NULL)
- {
- c++;
- //printf("%d ->", temp->label);
- temp = temp->next;
- }
- }
- //printf("\n");
- }
- return c;
- }
- void read_data(char * file)
- {
- FILE *fp = fopen(file, "rb");
- int label;
- while(!feof(fp))
- {
- fscanf(fp, "%d ", &label);
- insert(label, tab);
- }
- }
复制代码 Problem 2: 开始时结果老是不对,又找不出错误,后来发现Extrate-MAX和Extrate-MIN里面有问题,应该先求Smallest Child(或 Biggest Child)的位置,然后Bubble-Down交换数据.- #include <stdio.h>
- #include <time.h>
- #define MAX 10000
- struct heap
- {
- int size;
- int data[MAX];
- };
- struct heap min_heap, max_heap;
- long long count = 0;
- void init();
- void minheap_insert(int, struct heap *);
- int extract_min(struct heap *);
- void maxheap_insert(int, struct heap *);
- int extract_max(struct heap *);
- void swap(int *, int *);
- int parent(int);
- int smallest_child(int, struct heap *);
- int biggest_child(int, struct heap *);
- void display(struct heap *);
- void read_data(char *);
- int main(void)
- {
- init();
- read_data("D:\\Median.txt");
- printf("The count mod 10000 is : %d\n", count % 10000);
- return 0;
- }
- void init()
- {
- min_heap.size = 0;
- max_heap.size = 0;
- int x = 0;
- for(x = 0; x < MAX; x++)
- {
- *(min_heap.data + x) = 0;
- *(max_heap.data + x) = 0;
- }
- }
- void minheap_insert(int label, struct heap * temp)
- {
- temp->size += 1;
- int i = temp->size;
- *(temp->data + i - 1) = label;
- int *parent_i_th, *i_th;
- i_th = temp->data + i - 1;
- parent_i_th = temp->data + parent(i) - 1;
- while(*parent_i_th > *i_th)
- {
- swap(parent_i_th, i_th);
- i = parent(i);
- i_th = temp->data + i - 1;
- parent_i_th = temp->data + parent(i) - 1;
- }
- }
- void maxheap_insert(int label, struct heap * temp)
- {
- temp->size += 1;
- int i = temp->size;
- *(temp->data + i - 1) = label;
- int *parent_i_th, *i_th;
- i_th = temp->data + i - 1;
- parent_i_th = temp->data + parent(i) - 1;
- while(*parent_i_th < *i_th)
- {
- swap(parent_i_th, i_th);
- i = parent(i);
- i_th = temp->data + i - 1;
- parent_i_th = temp->data + parent(i) - 1;
- }
- if(label == 1260)
- {
- printf("1260 Test: ");
- display(temp);
- }
- }
- int extract_min(struct heap * temp)
- {
- if(temp->size == 0)
- {
- printf("No more element!\n");
- return -1;
- }
- int result = *(temp->data);
- *(temp->data) = *(temp->data + temp->size - 1);
- temp->size -= 1;
- int i = 1;
- int *i_th, *s_child;
- i_th = temp->data + i - 1;
- s_child = temp->data + smallest_child(i, temp) - 1;
- while(*i_th > *s_child)
- {
- // Attention, Need caculate smallest_child before swap.
- i = smallest_child(i, temp);
- swap(i_th, s_child);
- i_th = temp->data + i - 1;
- s_child = temp->data + smallest_child(i, temp) - 1;
- }
- return result;
- }
- int extract_max(struct heap * temp)
- {
- if(temp->size == 0)
- {
- printf("No more element!\n");
- return -1;
- }
- int result = *(temp->data);
- *(temp->data) = *(temp->data + temp->size - 1);
- temp->size -= 1;
- int i = 1;
- int *i_th, *b_child;
- i_th = temp->data + i - 1;
- b_child = temp->data + biggest_child(i, temp) - 1;
- while(*i_th < *b_child)
- {
- // Attention, Need to caculate biggest_child before swap.
- i = biggest_child(i, temp);
- swap(i_th, b_child);
- i_th = temp->data + i - 1;
- b_child = temp->data + biggest_child(i, temp) - 1;
- }
- return result;
- }
- void swap(int * label1, int * label2)
- {
- int temp = *label2;
- *label2 = *label1;
- *label1 = temp;
- }
- int parent(int label)
- {
- if(label == 1)
- return 1;
- else
- return label/2;
- }
- int smallest_child(int label, struct heap * temp)
- {
- int left_child = label * 2;
- int right_child = label * 2 + 1;
- if(left_child > temp->size)
- return label;
- else if(left_child == temp->size)
- return left_child;
- else
- {
- if(*(temp->data + left_child - 1) <= *(temp->data + right_child - 1))
- return left_child;
- else
- return right_child;
- }
- }
- int biggest_child(int label, struct heap * temp)
- {
- int left_child = label * 2;
- int right_child = label * 2 + 1;
- if(left_child > temp->size)
- return label;
- else if(left_child == temp->size)
- return left_child;
- else
- {
- if(*(temp->data + left_child - 1) > *(temp->data + right_child - 1))
- return left_child;
- else
- return right_child;
- }
- }
- void display(struct heap * temp)
- {
- int x = 0;
- for(x = 0; x < temp->size; x++)
- printf("%d-> ", *(temp->data + x));
- printf("\n");
- }
- void read_data(char * file)
- {
- FILE *fp = fopen(file, "rb");
- int label;
- FILE *wp = fopen("D:\\test.csv", "wb");
- int i = 0;
- while(!feof(fp))
- {
- // Please don't forget the space after %d.
- fscanf(fp, "%d ", &label);
- //printf("Label : %d \n", label);
- if(max_heap.size == 0)
- maxheap_insert(label, &max_heap);
- else
- {
- if(label <= *(max_heap.data))
- maxheap_insert(label, &max_heap);
- else
- minheap_insert(label, &min_heap);
- /*
- printf("1Low:%d\n", max_heap.size);
- display(&max_heap);
- printf("1High:%d\n", min_heap.size);
- display(&min_heap);
- */
- if(max_heap.size - min_heap.size > 1)
- {
- label = extract_max(&max_heap);
- minheap_insert(label, &min_heap);
- }
- if(max_heap.size < min_heap.size)
- {
- label = extract_min(&min_heap);
- maxheap_insert(label, &max_heap);
- }
- }
- /*
- printf("Low:%d\n", max_heap.size);
- display(&max_heap);
- printf("High:%d\n", min_heap.size);
- display(&min_heap);
- */
- int temp;
- temp = *(max_heap.data);
- count += temp;
- fprintf(wp, "%d\n", temp);
- printf("%d th Median: %d \n", ++i, temp);
- }
- fclose(wp);
- fclose(fp);
- }
复制代码 |
|