从头开始学算法:考研机试题练习(C/C++)--基础知识

本文通过具体示例介绍C语言的基础知识,包括I/O操作、字符字符串处理、数学函数应用、排序算法实现等,适合初学者入门学习。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

从头开始学算法:考研机试题练习(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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值