背代码

11/24/2024 代码

# C语言字符串分割成数组


char str[1000];
char words[100][100];
int wordCount =0;

char token = strtok(str,","); //通过 "," 分割
while(token!=NULL){
    strcpy(words[wordCount],token);
    wordCount++;
    token = strtok(NULL,",");
}

1
2
3
4
5
6
7
8
9
10
11
12

# 归并排序

all in 专业吧


#include <stdio.h>

void merge(int arr[],int left,int mid,int right){
    int n1= mid-left+1;
    int n2 = right-mid;
    int L[n1],R[n2];
    for(int i=0;i<n1;i++){
      L[i] = arr[left+i];
    }
    for(int j=0;j<n1;j++){
      R[j] = arr[mid+j+1];
    }

    int i=0,j=0,k = left;
    while(i<n1 && j<n2){
      if(L[i]<R[j]){
        arr[k++] = L[i++];
      }else{
        arr[k++] = R[j++];
      }
    }
    while(i<n1){
      arr[k++] = L[i++];
    }
    while(j<n2){
      arr[k++] = R[j++];
    }
}

void mergeSort(int arr[],int left,int right){
  if(left<right){
    int mid = (left+right)/2;
    mergeSort(arr,left,mid);
    mergeSort(arr,mid+1,right);
    merge(arr,left,mid,right);
  }
}

int main(){
  int arr[] = {14,2,3,4,5,6};
  int size = 6;
  mergeSort(arr,0,size-1);

  printf("%s",arr);
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Last Updated: 12/3/2024, 7:18:23 PM