从头开始学算法:考研机试题练习(C/C++)--入门模拟

来源:互联网 发布:手机铃声放大软件 编辑:程序博客网 时间:2024/05/01 00:12

从头开始学算法:考研机试题练习(C/C++)–入门模拟

最近重学c语言,刷的是胡凡写的《算法笔记》,这本书的题主要是面向考研机试和一般算法考试的,零基础入门,还不错,在此记录学习过程。

本文主要讲解一些简单的模拟题。

#include <stdio.h>#include <string.h>/*3n+1猜想*/void callatz_fun(){    int n;    scanf("%d", &n);    int step = 0;    while (n != 1) {        if (n % 2 == 0) {            n = n / 2;        }        else {            n = (3 * n + 1) / 2;        }        step++;    }    printf("%d", step);}/*求总分最高的挖掘机学校*/void wajueji_fun(){    int num, sum_score[100001] = { 0 },  highest_school=0 , school, score;    scanf("%d", &num);    while (num--) {        scanf("%d %d", &school, &score);        sum_score[school] += score;        if (sum_score[school] >sum_score[highest_school]) {            highest_school = school;        }    }    printf("%d %d", highest_school, sum_score[highest_school]);}/*二分查找*/void binary_seach_fun(){    int i, n, a[200], find, find_index=-1;    scanf("%d", &n);    for(i=0;i<n;i++){        scanf("%d", &a[i]);    }    scanf("%d", &find);    int low = 0, high = n - 1,mid;    while (low < high) {        mid = (low + high) / 2;        if (a[mid] == find) {            find_index = mid;            break;        }        //从小到大排序        else if (a[mid] > find) {            high = mid;        }        else {            low = mid;        }    }    if (find_index == -1 && a[low] == find)        find_index = low;}/*查找*/void seach_fun(){    int i, n, a[200], find, find_index = -1;    scanf("%d", &n);    for (i = 0; i<n; i++) {        scanf("%d", &a[i]);    }    scanf("%d", &find);    for (i = 0; i<n; i++) {        if (a[i] == find) {            find_index = i;            break;        }    }    printf("%d", find_index);}/*画正方形*/void draw_square(){    int i, j, n;    char shape;    scanf("%d %c", &n, &shape);    int row = n / 2 + n % 2;    for (i = 0; i < row; i++) {        printf("%c", shape);        for (j = 1; j < n - 1; j++) {            if (i == 0 || i == row - 1)                printf("%c", shape);            else                printf(" ");        }        printf("%c", shape);        printf("\n");    }}/*是否为闰年*/bool is_leap_year(int year) {    if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)        return true;    return false;}/*计算日期差*/void date_difference(){    int mouth_days[13] = {0, 31, 28, 31, 30, 31 ,30 ,31, 31, 30, 31, 30, 31 };    int from_day, from_month, from_year, to_day, to_month, to_year, difference_days=0;    char from_date[9], to_date[9], str_temp[5];    scanf("%s", from_date);    scanf("%s", to_date);    strncpy(str_temp, from_date, 4);    sscanf(str_temp, "%d", &from_year);    str_temp[2] = '\0';    strncpy(str_temp, from_date+4, 2);    sscanf(str_temp, "%d", &from_month);    strncpy(str_temp, from_date+6, 2);    sscanf(str_temp, "%d", &from_day);    //printf("%d %d %d\n", from_year, from_month, from_day);    strncpy(str_temp, to_date, 4);    sscanf(str_temp, "%d", &to_year);    str_temp[2] = '\0';    strncpy(str_temp, to_date + 4, 2);    sscanf(str_temp, "%d", &to_month);    strncpy(str_temp, to_date + 6, 2);    sscanf(str_temp, "%d", &to_day);    //将to_day变为与from_day相同    difference_days += to_day - from_day;    //将to_month变为与from_month相同    if (is_leap_year(to_year))        mouth_days[2] = 29;    if (to_month > from_month) {        for (int i = from_month; i < to_month; i++) {            difference_days += mouth_days[i];        }    }    else {        for (int i = to_month; i < from_month; i++) {            difference_days -= mouth_days[i];        }    }    //将to_year变为from_year    for (int i = from_year; i < to_year; i++) {        if (is_leap_year(i)) {            difference_days += 366;        }        else {            difference_days += 365;        }    }    //补上减去那些没有算的闰月    //起始年的月高于2月的该年不能当闰年算    if (from_month > 2 && is_leap_year(from_year)) {        difference_days--;    }    //终止年的月高于2月,最后一年少算一天    if (to_month > 2 && is_leap_year(to_year)) {        difference_days++;    }    printf("%d", difference_days);}//A、B的D进制和void scale_change(){    int a, b, d;    char out[31];    scanf("%d %d %d", &a, &b, &d);    int sum = a + b;    int n=31;    while (sum) {        n--;        out[n] = '0' + (sum % d);        sum = sum / d;    }    char result[31];    strncpy(result, out + n, 31 - n);    result[31 - n] = '\0';    printf("%s", result);}/*回文串*/void is_plalindrome(){    char sentence[256];    scanf("%s", sentence);    int first = 0, last = strlen(sentence) - 1;    while (first <= last) {        if (sentence[first] == sentence[last])        {            first++;            last--;        }else{            break;        }    }    if (first > last) {        printf("YES");    }    else {        printf("NO");    }}/*说反话*/void ironic_fun(){    char in_str[81],sentence[40][20];    gets_s(in_str);    int i, num = -1,start=0;    for (i = 0; i < strlen(in_str); i++) {        if (in_str[i] != ' ')            continue;        else {            num++;            strncpy(sentence[num], in_str + start, i - start);            sentence[num][i - start] = '\0';            start = i + 1;        }    }    num++;    strncpy(sentence[num], in_str + start, strlen(in_str) - start);    sentence[num][i - start] = '\0';    for (i = num; i >= 0; i--) {        printf("%s ", sentence[i]);    }    printf("\b");}int main(){    //callatz_fun();    //wajueji_fun();    //seach_fun();    //draw_square();    //date_difference();    //scale_change();    //is_plalindrome();    ironic_fun();    return 0;}
原创粉丝点击