自学内容网 自学内容网

MT1361-MT1370 码题集 (c 语言详解)

MT1361·重组字符串

c 语言代码实现

#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 100
void filterOddChars(const char *input, char *output) {
    int j = 0;
    for (int i = 0; input[i] != '\0'; i++) {
        if (i % 2 != 0 && (input[i] % 2 != 0)) {
            output[j++] = input[i];
        }
    }
    output[j] = '\0';
}
int main() {
    char str[MAX_LENGTH];
    char result[MAX_LENGTH];

    fgets(str, MAX_LENGTH, stdin);
    str[strcspn(str, "\n")] = '\0';
    filterOddChars(str, result);
    printf("%s", result);

    return 0;
}

MT1362·特殊排序

 

c 语言代码实现

#include <stdio.h>
#include <string.h>
#define MAX_LENGTH 100

void sortDescending(char *str, int start, int end) {
    for (int i = start; i < end; i++) {
        for (int j = i + 1; j < end; j++) {
            if (str[i] < str[j]) {
                char temp = str[i];
                str[i] = str[j];
                str[j] = temp;
            }
        }
    }
}

void sortAscending(char *str, int start, int end) {
    for (int i = start; i < end; i++) {
        for (int j = i + 1; j < end; j++) {
            if (str[i] > str[j]) {
                // 交换
                char temp = str[i];
                str[i] = str[j];
                str[j] = temp;
            }
        }
    }
}

int main() {
    char str[MAX_LENGTH];
    fgets(str, MAX_LENGTH, stdin);
    str[strcspn(str, "\n")] = '\0';

    int len = strlen(str);
    int mid = len / 2;

    // left descending order
    sortDescending(str, 0, mid);
    // right ascending order
    sortAscending(str, mid + (len % 2), len);

    // exchange left right
    char temp[MAX_LENGTH];
    strncpy(temp, str, mid);
    stpncpy(str, str + mid + (len % 2), len - mid - (len % 2));
    strncpy(str + (len - mid), temp, mid);

    printf("%s", str);

    return 0;
}

MT1363·前导中间末尾

c 语言代码实现

#include <stdio.h>
#include <string.h>
void removeAsterisks(char *str) {
    // first !*
    char *start = str;
    while (*start == '*') {
        start++;
    }
    // end !*
    char *end = str + strlen(str) - 1;
    while (end > start && *end == '*') {
        end--;
    }

    // copy !*
    char *current = str;
    while (start <= end) {
        if (*start != '*') {
            *current++ = *start;
        }
        start++;
    }
    *current = '\0';
}
int main() {
    char str[] = "****ABB*DDDFFF***";
    removeAsterisks(str);
    printf("%s", str);
    return 0;
}

MT1364·递归求工资 

 

c 语言代码实现

#include <stdio.h>
int salary(int num) {
    if (num == 1)
        return 1000;
    return 200 + salary(num - 1);
}
int main() {
    int num;
    scanf("%d", &num);
    printf("%d", salary(num));
    return 0;
}

MT1365·分桃 

c 语言代码实现

#include <stdio.h>

int total(int num) {
    if (num == 10)
        return 1;
    return (total(num + 1) + 1) * 2;
}

int main() {
    printf("%d", total(1));
    return 0;
}

MT1366·用函数求平方和 

c 语言代码实现

#include <math.h>
#include <stdio.h>
int fun(int num) {
    int sum;
    while (num >= 1) {
        sum += num * num;
        num--;
    }
    return sum;
}
int main() {
    int num;
    scanf("%d", &num);
    printf("%d", fun(num));
    return 0;
}

MT1367·求解函数

c 语言代码实现

#include <stdio.h>
int ack(int m, int n) {
    if (m == 0)
        return n + 1;
    else if (n == 0)
        return ack(m - 1, 1);
    else
        return ack(m - 1, ack(m, n - 1));
}
int main() {
    int m, n;
    scanf("%d %d", &m, &n);

    int result = ack(m, n);
    printf("%d", result);
    return 0;
}

MT1368·递归法求逆序 

c 语言代码实现

#include <stdio.h>
void reserve(int num) {
    if (num == 0)
        return;
    printf("%d", num % 10);
    reserve(num / 10);
}
int main() {
    int num;
    scanf("%d", &num);
    reserve(num);
    return 0;
}

MT1369·杨辉三角

c 语言代码实现

#include <stdio.h>

// 函数:输出杨辉三角形
void printPascalsTriangle(int rows) {
    int triangle[rows][rows]; // 创建一个二维数组来存储杨辉三角形的值
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j <= i; j++) {
            if (j == 0 || j == i) {
                triangle[i][j] = 1; // 每行的第一个和最后一个元素为 1
            } else {
                triangle[i][j] = triangle[i - 1][j - 1] + triangle[i - 1][j]; // 其他元素为上方两个元素之和
            }
        }
    }

    // 输出杨辉三角形
    for (int i = 0; i < rows; i++) {
        for (int j = 0; j <= i; j++) {
            printf("%d ", triangle[i][j]); // 打印三角形的元素
        }
        printf("\n"); // 换行
    }
}

int main() {
    int rows;
    scanf("%d", &rows);
    printPascalsTriangle(rows);

    return 0;
}

MT1370·递归函数

c语言实现代码

#include <stdio.h>

int sumOfDigits(int num) {
    if (num < 10)
        return num;
    int sum = 0;
    while (num > 0) {
        sum += num % 10;
        num /= 10;
    }
    return sumOfDigits(sum);
}

int main() {
    int num;
    scanf("%d", &num);
    printf("%d", sumOfDigits(num));
    return 0;
}

 有什么关于计算机的问题可以私聊我 到后期可以拉个算法群跟大家一起讨论解决问题


原文地址:https://blog.csdn.net/xinfanyyds/article/details/142962522

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!