HI! 我是Maple 剛滿20歲沒多久的小朋友 請ㄅ要欺負窩QAQ
#include <stdio.h>
int* pi = NULL;
float* pf = NULL;
pi = (int*)malloc(sizeof(int));
pf = (float*)malloc(sizeof(float));
*pi = 1024;
*pf = 3.14;
printf("An Interger = %d, A Float = %f\n", *pi, *pf);
free(pi);
free(pf);
Example BinarySearch(二元搜尋法)
#include <stdio.h>
int compare (int x, int y) {
if (x > y) {
return 1;
} else if (x < y) {
return -1;
} else {
return 0;
}
}
int BinarySearch(int list[], int searchnumber, int left, int right) {
int length = sizeof(list) / sizeof(int);
left = 0, right = length - 1;
while (left <= right) {
int middle = (left + right) / 2;
switch(compare(list[middle], searchnumber)):
case 1:
right = middle - 1;
break;
case 2:
left = middle + 1;
case 0:
return middle;
}
return -1;
}