LeetCode解題----C語言基本庫函數的使用


在做題的時候,借助標准庫中的函數,可以使我們更加專注於算法和題目本身。庫函數的熟練使用,有助於我們聚焦於思路,快速解決問題。因此,很有必要熟練庫函數了。以下庫函數是本人在做題中經常遇到使用到的庫函數,以下介紹按使用頻率從大到小排序。

1.快速排序qsort

1.1 qsort函數原型

函數原型 void qsort(voidbase, size_t num, size_t width, int( * compare)(const void,const void*))
庫函數 qsort
包含頭文件 stdlib.h
---- ----
函數輸入參數 參數描述
void *base 待排序數組首地址
size_t num 數組中帶排序元素數量
size_t width 各元素的占用空間大小
int( * compare)(const void,const void) 指向函數的指針,用於卻帶排序的順序,傳入的是地址
函數原型 compare( (void *) & elem1, (void *) & elem2)
compare函數的返回值 描述
<0 elem1將被排在ele2之前
=0 位置不變
>0 elem1將被排在elem2之后
exam:
從小到大排序 int comp(const void *a, const void *b) {return *(int*)a-*(int*)b;}
從大到小排序 int comp(const void *a, const void *b) {return *(int*)b-*(int*)a;}

1.2 qsort函數應用實例

1.2.1 對一維數組進行排序(從小到大排序):

#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b)
{
    return *(int *)a - *(int *)b; //從小到大排序
}
int main()
{
    /* 數組輸入准備 */
    int *nums;
    int numsSize;
    scanf("%d", &numsSize);
    nums = (int *)malloc(numsSize * sizeof(int)); // 動態申請數組
    for (int i = 0; i < numsSize; i++)
    {
        scanf("%d", &nums[i]); // 數組元素賦值
        printf("%d\t", nums[i]); //打印排序前數組
    }
    /* 數組調用qsort進行排序 */
    qsort(nums, numsSize, sizeof(int), compare);
    /* 打印排序后數組 */
    printf("\nby sorted:\n");
    for (int i = 0; i < numsSize; i++)
    {
        printf("%d\t", nums[i]);
    }
    return 0;
}

1.2.2 對二維數組進行排序(依據第一維對數組進行從小到大排序):

#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b)
{
    int *ap = *(int **)a;
    int *bp = *(int **)b;
    // 如果第一維相等,則依據第二維從小到大排序
    if (ap[0] == bp[0])
        return ap[1] - bp[1];
    else
        return ap[0] - bp[0]; // 第一維不相等,依據第一維從小到大排序
}
void printfnums(int **nums, int numsSize, int numsColSize)
{
    for (int i = 0; i < numsSize; i++)
    {
        for (int j = 0; j < numsColSize; j++)
        {
            printf("%d ", nums[i][j]);
        }
        printf("\n");
    }
}
int main()
{
    int inputnums[][2] = {{1, 54}, {-4, 89}, {45, 6545}};
    int numsColSize = sizeof(inputnums[0]) / sizeof(int);
    int numsSize = sizeof(inputnums) / sizeof(inputnums[0]);
    int **nums = (int **)malloc(numsSize * sizeof(int *));

    for (int i = 0; i < numsSize; i++)
    {
        nums[i] = (int *)malloc(numsColSize * sizeof(int));
    }
    for (int i = 0; i < numsSize; i++)
    {
        for (int j = 0; j < numsColSize; j++)
        {
            nums[i][j] = inputnums[i][j];
        }
    }

    printfnums(nums, numsSize, numsColSize);
    qsort(nums, numsSize, sizeof(nums[0]), compare);
    printfnums(nums, numsSize, numsColSize);
    return 0;
}

1.2.3 對結構體進行排序:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
typedef struct point
{
    int x;
    int y;
    double distance;
};
int compare(const void *a, const void *b)
{
    struct point *x = (struct point *)a;
    struct point *y = (struct point *)b;
    if (x->distance != y->distance)
    {
        return x->distance - y->distance;
    }
    else if (x->x != y->x)
    {
        return x->x - y->x;
    }
    else //if(x->y != x->y)
    {
        return x->y - y->y;
    }
}
void pointsprintf(struct point *points, int numsSize)
{
    for (int i = 0; i < numsSize; i++)
    {
        printf("distance :%0.2f x :%d y :%d\n", points[i].distance, points[i].x, points[i].y);
    }
}
int main()
{
    int nums[][2] = {{1, 3},
                     {-1, -1},
                     {56, 4},
                     {-1, -2}};
    int numsSize = sizeof(nums) / sizeof(nums[0]);
    int numsColSize = sizeof(nums[0]) / sizeof(int);
    struct point points[numsSize];
    //int *points = (int *)malloc(numsSize * sizeof(struct point));
    for (int i = 0; i < numsSize; i++)
    {
        points[i].x = nums[i][0];
        points[i].y = nums[i][1];
        points[i].distance = sqrt(points[i].x * points[i].x + points[i].y * points[i].y);
    }
    pointsprintf(points, numsSize);
    qsort(points, numsSize, sizeof(struct point), compare);
    printf("\n");
    pointsprintf(points, numsSize);
    return 0;
}

1.2.4 對字符串進行排序:

compare 函數:

int compare(const void *a, const void *b)
{
    return strcmp(*(char **)a, *(char **)b);
}

調用:

qsort(products, productsSize, sizeof(char **), compare);

2. 整數與字符串的相互轉換itoa/atoi

2.1 整數轉字符串itoa

函數原型 char* itoa(int value,char*string,int radix)
庫函數 itoa
包含頭文件 stdlib.h
---- ----
函數輸入參數 參數描述
int value 要轉換的整數
char*string 轉換后的字符串
int radix 轉換進制數,如2,8,10,16 進制等
返回值 指向轉換后的字符串指針
#include <stdio.h>
#include <stdlib.h> // itoa 所在頭文件
int main()
{
    int num = 3;
    char str[10];
    // num:待轉換的整數;str:轉換后的字符串;10:轉換進制數
    itoa(num, str, 10);
    printf("%s", str);
    return 0;
}

2.2 字符串轉整數atoi

函數原型 int atoi(const char *str)
庫函數 atoi
包含頭文件 stdlib.h
---- ----
函數輸入參數 參數描述
const char *str 待轉換字符串
返回值 轉換后的整數(int型)
#include <stdio.h>
#include <stdlib.h> // atoi 所在庫函數
int main()
{
    char s[] = "22";
    // s:待轉換字符串;函數返回值:轉換后的整數int
    int num = atoi(s);
    printf("%d", num);
    return 0;
}

3. 字符串比較strcmp()/strncmp()

3.1 strcmp()

函數原型 int strcmp(const char *str1, const char *str2)
庫函數 strcmp
包含頭文件 string.h
---- ----
函數輸入參數 參數描述
const char *str1 待比較字符串1的指針
const char *str2 待比較字符串2的指針
返回值 如果返回值小於 0,則表示 str1 小於 str2。
如果返回值大於 0,則表示 str1 大於 str2。
如果返回值等於 0,則表示 str1 等於 str2。
#include <stdio.h>
#include <string.h>
int main()
{
    char s1[] = "ab";
    char s2[] = "aB";
    if (strcmp(s1, s2) == 0)
    {
        printf("s1 == s2");
    }
    else if (strcmp(s1, s2) < 0)
    {
        printf("s1 < s2");
    }
    else if (strcmp(s1, s2) > 0)
    {
        printf("s1 > s2");
    }
    return 0;
}

3.2 strncmp()

函數原型 int strncmp(const char *str1, const char *str2, size_t n)
庫函數 strncmp
包含頭文件 string.h
---- ----
函數輸入參數 參數描述
const char *str1 待比較字符串1的指針
const char *str2 待比較字符串2的指針
size_t n 最多比較前 n 個字節
返回值 如果返回值小於 0,則表示 str1 小於 str2。
如果返回值大於 0,則表示 str1 大於 str2。
如果返回值等於 0,則表示 str1 等於 str2。

4. 字符串復制strcpy()/strncpy()

4.1 strcpy()

函數原型 char *strcpy(char *dest, const char *src)
庫函數 strcpy
包含頭文件 string.h
---- ----
函數輸入參數 參數描述
char *dest 指向用於存儲復制內容的目標數組
const char *src 要復制的字符串
返回值 該函數返回一個指向最終的目標字符串 dest 的指針
#include <stdio.h>
#include <string.h>
int main()
{
    char dest[20] = "ha ";
    char src[] = "haha";
    strcpy(dest, src);
    printf("%s", dest);
    return 0;
}

4.2 strncpy()

函數原型 char *strncpy(char *dest, const char *src, size_t n)
庫函數 strncpy
包含頭文件 string.h
---- ----
函數輸入參數 參數描述
char *dest 指向用於存儲復制內容的目標數組
const char *src 要復制的字符串
size_t n 要從源中復制的字符數
返回值 該函數返回一個指向最終的目標字符串 dest 的指針

更多庫函數的使用,可參考如下位置:

C語言庫函數


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM