锘??xml version="1.0" encoding="utf-8" standalone="yes"?>
now, give you a balance, how many times do you need to pick out the bad one?
My resolution: need Ball(y) times to pick out the bad one.
#include <math.h>
int Ball(int y)
{
if(y <= 3) return 1;
return 1+Ball( y/3 * 3 == y ? y/3 : y/3 +1 );
}
int main()
{
int i = 0;
for(i = 2; i < 200; i++) {
printf("Ball(%3d)=%d \t", i, Ball(i));
if(i%5 == 0) puts("");
}
puts("");
return 0;
}
鐜板湪鏈?浜挎暟,娣峰簭,
鐒跺悗姹傝繖涓浜夸釜鏁扮殑涓綅鏁?
涓綅鏁?灝辨槸涓涓湁搴忔暟緇勭殑涓棿鏁板瓧,鏁扮粍闀垮害涓哄伓鏁板氨鏄袱涓?濂囨暟鍒欏彧鏈変竴涓?
/* this program should find out the median(s) in N(logN)/(logB) no matter there
/* is duplicated data or not.
/*
/* with memory limitation: 4MB memory available. in fact, it can work on 1MB.
/* I would like to express the idea with code.
/* here is just the pseudocode with out any optimization for easy understanding.
/*
/* maybe, there is no need to check sum == N/2 or N/2+1, but this will get the
/* median(s) as soon as possible with out more recursive invoking.
/*
/* sorry for any logical problem. please let me know if you found any.
/*****************************************************************************/
int N = 100000000;// total data number is N
int B = 1000000; // number of buckets.
int min, max;
scan data from 0 to N-1 get min and max; // O(N);
// num the data number that is less than min
// min the minimum data in the target data range to find median(s)
// max the maximum data in the target data range to find median(s)
void find_median(int num=0, int min, int max)
{
if(min == max) {
if(N%2) {
print medians are min and max;
}
else print median is min; // show you the result
return; // exit
}
// count all the data in O(N)
int m = max-min > B ? ((max-min)%B ? (max-min)/B + 1 : (max-min)/B) : 1;
int cnt[B] = {0}; // count the data read.
while(!EOF) {
int data = read_data()-min;
// count data in this range [min, max]
// data/m will get the position of data to increase the cnt[?] value
if(data >= min && data <= max) cnt[data/m]++;
}
int sum = num, median_1, median_2, i = 0;
while(sum < N/2) sum += cnt[i++];
i--; // median is in the range of [i*m, (i+1)*m-1]
/* get median(s) when sum is N/2 */
if(sum == N/2) {
if(N%2) { // N is even and there are two medians.
median_1 = (i+2)*m-1;
median_2 = i*m;
while(!EOF) {
int data = read_data()-min;
if(data/m == i) {
median_2 = (data > median_2 ? data : median_2);
}
if(data/m == i+1) {
median_1 = (data < median_1 ? data : median_1);
}
}
pintf medians are median_1 and median_2;
return;
}
else { // N is an odd number and there is one median.
median_1 = (i+2)*m-1;
while(!EOF) {
int data = read_data();
if(data/m == i+1) median_1 = (data < median_1 ? data : median_1);
}
print median is median_1;
return;
}
}
/* get median(s) when sum is N/2+1 */
if(sum == N/2+1) {
if(N%2) { // N is even and there are two medians.
median_1 = i*m;
median_2 = i*m;
while(!EOF) {
int data = read_data();
if(data/m == i) {
median_1 = max;
median_2 = (data > median_2 ? data : median_2);
}
}
pintf medians are median_1 and median_2;
return;
}
else {
median_2 = i*m; // get (N/2+1)th data.
while(!EOF) {
int data = read_data();
if(data/m == i) median_2 = (data > median_2 ? data : median_2);
}
print median is median_2;
return;
}
}
// if sum is not N/2 or (N/2 + 1), recursively to find out the median(s)
min = (i+1)*m-1;
max = i*m;
// get min and max for next processing
while(!EOF)
{
int data = read_data()-min;
if(data/m == i)
{
min = (data < min ? data : min);
max = (data > max ? data : max);
}
}
find_median(sum, min, max);
}
Precondition:
1. star field simulation graph is planar.
2. the coordinate of star is (x, y) that is treated as a point銆?br> 3. N = 1000000
1. bucket-sort all points according to x-coordinate. get B buckets.
this is will completed in O(NlogN).
struct bucket {
int num; // number of points in this bucket.
point* points; // points in this bucket.
double x; // value of x-coordinate.
}
bck[B]; // B buckets got.
2.
struct distance {
point p1;
point p2;
double d; // distance between p1 and p2.
}
distance kdis[K]; // to record K small distance. and it's a eliminating-tree.
kdis[0 to K-1] = 0;
for(i = [0, B-2]) // O(B)
{
// check bck[i] and bck[i+1]
if(bck[i+1].x - bck[i].x >= kdis[K-1].d && kdis[K-1] != 0)
{
// there is no need to check these two buckets.
i++;
continue; // save time.
}
point* poi = bck[i].points;
for(j = [0, bck.num-1]) // O(N/B)
{
point p = poi[j];
/*
find K points in bck[i+1] near to p
with binary searching method
according to p.y.
*/
kp[K]; // K points got in bck[i+1]
for(m = [0, K-1]) // O(K)
{
distance tmp = get_distance(kp[m], p);
if(tmp.d < kdis[K-1].d)
{
kdis[K-1] = tmp;
// adjust kdis[K-1], for it's a eliminating tree.
}
}
}
}
// finally, the kdis[K-1] is the kth distance in this star graph.
// the whole processing will be completed in O(NlogN) + O(B*N/B*K).
// and SC is O(N) + O(K) = O(N).
HOW TO FIND K POINTS?
/*
find K points in bck[i+1] near to p
with binary searching method
according to p.y.
*/
it's not complecated! and could be found in O(log(N/B)).
Refer Find kth number in O(n) TC
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define N 20
void init_a(int A[])
{
int i = 0;
for(;i<N;i++)
A[i] = rand()%100;
}
void print_array(int A[])
{
int i = 0;
for(;i<N;i++)
{
if(i==9)
printf("%d\n",A[i]);
else
printf("%d\t",A[i]);
}
}
void swap(int* a, int* b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
int rand_partition(int A[], int start, int end)
{
int i = start-1;
int j = start;
int num = rand()%(end-start+1)+start;
swap(A+num,A+end);
int x = A[end];
for(;j<end;j++)
{
if(A[j]<=x)
{
i++;
swap(A+i,A+j);
}
}
swap(A+i+1,A+end);
return i+1;
}
int random_select(int A[], int start, int end, int k)
{
int i = 0;
if(start == end)
return A[start];
int q = rand_partition(A, start, end);
i = q-start+1;
if(i == k)
return A[q];
else if(i<k)
return random_select( A, q+1, end, k-i);
else
return random_select( A, start, q-1, k);
}
void quick_sort(int A[], int start, int end)
{
if(start<end)
{
int q = rand_partition(A, start, end);
quick_sort( A, q+1, end);
quick_sort( A, start, q-1);
}
}
int main(int argc, char *argv[])
{
int A[N];
int num = 0;
int res = 0;
srand((unsigned)time(NULL));
init_a(A);
print_array(A);
num = rand()%(N-10)+5;
printf("we begin select %d minimun number\n",num);
res = random_select( A, 0 , N-1, num);
printf("and num is %d\n",res);
quick_sort( A, 0 , N-1);
printf("after quick sort the array is:\n");
print_array(A);
system("PAUSE");
return 0;
}
or function like this: fabonaci:
fabonaci(n) = fabonaci(n-1) + fabonaci(n-2).
2 {
3 printf("fabo(%d)\n", n);
4 if(n < 3)
5 {
6 *ptr = 1;
7 return 1;
8 }
9 else {
10 int tmp;
11 *ptr = fabo(n - 1, &tmp);
12
13 return *ptr + tmp;
14 }
15 }
16 int fabonaci(int n)
17 {
18 int tmp;
19 return fabo(n, &tmp);
20 }
21
22 int xxx(int n)
23 {
24 printf("xxx(%d)\n", n);
25 if(n < 3)return 1;
26 else return xxx(n-1)+xxx(n-2);
27 }
28 int main()
29 {
30 int n = fabonaci(10);
31 printf("n = %d\n", n);
32
33 n = xxx(10);
34 printf("n = %d\n", n);
35 getchar();
36 return 0;
37 }
2 /* func(n) = func(n-1) + func(n-2) + func(n-3) */
3 /* func(1) = 1; func(2) = 1; func(3) = 2; */
4 /************************************************************************/
5 int funk(int n, int* n_1, int* n_2)
6 {
7 printf("funk(%d)\n", n);
8 if(n == 3){
9 *n_1 = 1;
10 *n_2 = 1;
11 return 2;
12 }
13 else{
14 int n_1_1, n_1_2;
15 *n_1 = funk(n-1, &n_1_1, &n_1_2);
16 *n_2 = n_1_1;
17 return *n_1 + *n_2 + n_1_2;
18 }
19 }
20 int func(int n)
21 {
22 int n_1, n_2;
23 return funk(n, &n_1, &n_2);
24 }
25 int main()
26 {
27 int n = func(10);
28 printf("n = %d\n", n);
29 getchar();
30 return 0;
31 }