1. 程式人生 > 實用技巧 >淺談線段樹

淺談線段樹

實驗1

#include <stdio.h> 

const int N=3;
int main() {
    int a[N] = {1, 2, 3};  
    int i;
    
    printf("通過陣列名及下標直接訪問陣列元素:\n");
    for(i=0; i<N; i++)
        printf("%d: %d\n", &a[i], a[i]);
        
    printf("通過地址間接訪問陣列元素:\n");
    for(i=0; i<N; i++)
        printf("%d: %d\n", a+i, *(a+i));

    
return 0; }

實驗2

#include <stdio.h>
const int LINE = 2;
const int COL = 3;

int main() {
    int a[LINE][COL] = {1,2,3,4,5,6};  
    int i,j;
    
    printf("通過陣列名及下標直接訪問陣列元素:\n");
    for(i=0; i<LINE; i++)
        for(j=0; j<COL; j++)
            printf("%d: %d\n", &a[i][j], a[i][j]);
        
    printf(
"通過地址間接訪問陣列元素:\n"); for(i=0; i<LINE; i++) for(j=0; j<COL; j++) printf("%d: %d\n", a[i]+j, *(a[i]+j)); printf("二維地址中a+i表示的地址:\n"); for(i=0; i<LINE; i++) printf("a + %d: %d\n", i, a+i); return 0; }

實驗3

#include <stdio.h> 
#include <stdlib.h> 

const
int N=3; int main() { int a[N]; int *p,i; for(p=a; p<a+N; p++) scanf("%d", p); for(p=a; p<a+N; p++) printf("%d ", *p); printf("\n"); p = a; for(i=0; i<N; i++) scanf("%d", p+i); for(i=0; i<N; i++) printf("%d ", *(p+i)); printf("\n"); return 0; }

實驗4

#include <stdio.h> 
int main() {
    int a[2][3] = {1,2,3,4,5,6};
    int i,j;
    int *p;  
    int (*q)[3]; 
    
    for(p=a[0]; p<a[0]+6; p++)
        printf("%d ", *p);
    printf("\n");
    
    for(q=a; q<a+2; q++)
        for(j=0; j<3; j++)
            printf("%d ", *(*q+j));
    printf("\n");
    

    return 0;
}  

實驗5

1

#include  <stdio.h>

const int N=5;

int binarySearch(int x[], int n, int item); 

int main() {
    int a[N]={2,7,19,45,66};
    int i,index, key;
    
    printf("陣列a中的資料:\n");
    for(i=0;i<N;i++)
       printf("%d ",a[i]);
    printf("\n");
    
    printf("輸入待查詢的資料項: ");
    scanf("%d", &key);
    
    index = binarySearch(a,N,key) ;
    
    if(index>=0) 
        printf("%d在陣列中,下標為%d\n", key, index);
    else
        printf("%d不在陣列中\n", key); 
   
   return 0;
}


int binarySearch(int x[], int n, int item) {
    int low, high, mid;
    
    low = 0;
    high = n-1;
    
    while(low <= high) {
        mid = (low+high)/2;
        
        if (item==x[mid])
            return mid;
        else if(item<x[mid])
            high = mid - 1;
        else
            low = mid + 1;
    }
    
    return -1;
}

2

#include  <stdio.h>
const int N=5;
int binarySearch(int *x, int n, int item); 

int main() {
    int a[N]={2,7,19,45,66};
    int i,index, key;
    
    printf("陣列a中的資料:\n");
    for(i=0;i<N;i++)
       printf("%d ",a[i]);
    printf("\n");
    
    printf("輸入待查詢的資料項: ");
    scanf("%d", &key);
    
    index = binarySearch(a,N,key);
    
    if(index>=0) 
        printf("%d在陣列中,下標為%d\n", key, index);
    else
        printf("%d不在陣列中\n", key); 
   
   return 0;
}


int binarySearch(int *x, int n, int item) {
    int low, high, mid;
    
    low = 0;
    high = n-1;
    
    while(low <= high) {
        mid = (low+high)/2;
        
        if ( item == *(x+mid) )
            return mid;
        else if(item < *(x+mid))
            high = mid-1; 
        else
            low = mid+1;
    }
    
    return -1;
}

實驗6

#include <stdio.h>
#include <string.h>
const int N = 5;
void selectSort(char str[][20], int n );
int main() {
    char name[][20] = {"Bob", "Bill", "Joseph", "Taylor", "George"};
    int i;
    
    printf("輸出初始名單:\n");
    for(i=0; i<N; i++)
        printf("%s\n", name[i]);
        
    selectSort(name, N); 
    
    printf("按字典序輸出名單:\n");
    for(i=0; i<N; i++)
        printf("%s\n", name[i]);
    
    return 0;
} 

void selectSort(char str[][20], int n) {
    int i,j,k;
    char temp[20];
    for (i=0;i<n-1;i++){
        k=i;
        for (j=i+1;j<n;j++){
            if(strcmp(str[j],str[k])<0) k=j;
        }
        if(k != i) {
            strcpy(temp,str[i]);
            strcpy(str[i],str[k]);
            strcpy(str[k],temp);
        }
    }
}