從頭開始學演算法:考研機試題練習(C/C++)–基礎知識

最近重學c語言,刷的是胡凡寫的《演算法筆記》,這本書的題主要是面向考研機試和一般演算法考試的,零基礎入門,還不錯,在此記錄學習過程。

本文回顧c語言的I/0操作和字元字串操作等基礎知識。

#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include "basics.h"
/*加*/
void add()
{
    printf("please input 'add a b',we will calculate their sum\n");
    int a, b;
    scanf("add %d %d", &a, &b);
    printf("sum is %d\n", a + b);
}

/*變數型別與輸出*/
void print_variable_size()
{
    typedef long long LL;
    long long a = 5;
    long b = 5;
    int c = 5;
    float d = 5;
    double e = 5;
    char f = '5';
    bool g = true;
    const int h = 5;
    LL i = 5;
    printf("size of ll(%lld) %d \n", a, sizeof(a));
    printf("size of l(%ld) %d \n", b, sizeof(b));
    printf("size of int(%d) %d \n", c, sizeof(c));
    printf("size of float(%f) %d \n", d, sizeof(d));
    printf("size of double(%lf) %d \n", e, sizeof(e));
    printf("size of char(%c) %d \n", f, sizeof(f));
    printf("size of bool(%d) %d \n", g, sizeof(g));
    printf("size of const int(%d) %d \n", h, sizeof(h));
    printf("size of LL(%lld) %d \n", i, sizeof(i));
    printf("---------\n");
    printf("%5d\n", 231);
    printf("%05d\n", 4321);
    printf("%.2f\n", 3.1415926575);
    printf("%.4f\n", 3.1415926575);

}

/*getchar,putchar函式*/
void get_put_char()
{
    char a, b, c;
    a = getchar();
    getchar();
    b = getchar();
    c = getchar();
    putchar(a);
    putchar(b);
    putchar(c);


}

/*數學函式*/
void math_fun()
{
    printf("abs: %d\n", abs(-3));
    printf("fabs: %lf\n", fabs(-3.14));
    printf("floor %.0f ceil %.0f\n", floor(-3.2), ceil(3.2));
    printf("2.0^3.0 pow: %f\n ", pow(2.0, 3.0));
    printf("sqrt 2: %lf\n", sqrt(2.0));
    printf("log 1: %lf\n", log(1));
    const double pi = acos(-1);
    printf("cos(1/3pi):%lf\n", cos(pi / 3));
    printf("sin(1/3pi):%lf\n", sin(pi / 3));
    printf("tan(1/3pi):%lf\n", tan(pi / 3));
    printf("round 3.4:%lf\n", round(3.4));
    printf("round 3.5:%lf\n", round(3.5));


}


/*氣泡排序*/
void bubble_sort()
{
    srand(unsigned(time(NULL)));
    int a[20],i,j;
    for (i = 0; i < 20; i++) {
        a[i] = rand() % 60;
        printf("%d ", a[i]);
    }
    printf("\n");
    for (j = 0; j < 20 - 1; j++) {
        for (i = 0; i < 20 - 1-j; i++) {
            if (a[i] > a[i + 1]) {
                int temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
            }
        }
    }

    printf("sort:");
    for (i = 0; i < 20; i++) {
        printf("%d ", a[i]);

    }
    printf("\n");


}
/*陣列操作*/
void array_fun()
{
    int i, j;
    int a[5][3] = { {1},{2,3},{4},{},{5} };
    for (i = 0; i < 5; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }
    printf("--\n");

    memset(a, -1, sizeof(a));//往整個儲存塊的每個位元組都填充-1
    for (i = 0; i < 5; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d ", a[i][j]);
        }
        printf("\n");
    }


}

/*gets,puts函式*/
void gets_puts_fun()
{
    printf("輸入字元,程式將去掉空格並倒序輸出:\n");
    int i,size=1;
    char str[20], mid_str[20], out_str[20];
    gets_s(str);
    mid_str[19] = '\0';
    for (i = 0; str[i] != '\0'; i++) {
        if (str[i] == ' ') continue;
        size++;
        mid_str[20 - size] = str[i];
    }
    strncpy(out_str, mid_str + (20 - size), size);
    puts(out_str);

}

/*將字串按從大到小串連,並輸出一個表示原來字串長度的串*/
void string_fun()
{
    int i, j, k;
    char str[4][20], out_str[80]="", size_str[5]="";
    int sort_index[4] = {0,1,2,3};
    for (i = 0; i < 4; i++)
        gets_s(str[i]);
    //下面用插入排序進行排序
    for (i = 1; i < 4; i++) {
        for (j = 0; j < i; j++) {//i前面的數
            if (strcmp(str[sort_index[i]], str[sort_index[j]]) < 0)
                continue;
            else {
                int temp = sort_index[i];
                for (k = i; k > j; k--) {
                    sort_index[k] = sort_index[k - 1];//j後面的往後移
                }
                sort_index[j] = temp; //每次只改標籤
                break;
            }
        }
    }
    int len;
    for (i = 0; i < 4; i++) {
        len = strlen(str[sort_index[i]]);
        strcat(out_str, str[sort_index[i]]);
        size_str[i] = len+'0';
    }
    size_str[4] = '\0';
    puts(out_str);
    puts(size_str);

}

/*sscanf,sprintf函式*/
void sscanf_sprintf_function()
{
    int num;
    float fnum;
    char str_scanf[40]="2048:3.13,good", str_printf[40], str_1[20];
    sscanf(str_scanf, "%d:%f,%s", &num, &fnum, str_1);
    printf("%f,%s\n", num + fnum, str_1);

    sprintf(str_printf, "hh:%d;%f;%s", num, fnum, str_1);
    printf("str_printf:  %s", str_printf);

}


int main()
{
    long start = clock();
    //add();
    //print_variable_size();
    //get_put_char();
    //math_fun();
    //bubble_sort();
    //array_fun();
    //gets_puts_fun();
    //string_fun();
    sscanf_sprintf_function();
    long end = clock();
    printf("\ntime: %LFs\n",double(end - start)/CLOCKS_PER_SEC);
    return 0;
}