C语言基础算法题

1、计算并输出1到100之间所有偶数之和;

cpp 复制代码
#include <stdio.h>

int main(){
    int sum = 0;
    for(int i = 2; i <= 100; i+ = 2) {
        sum+ = i;
    }
    printf("sum = :%d\n", sum);

    return 0;
}

2、招数数组中的最大值和最小值;

cpp 复制代码
#include <stdio.h>

int main() {
    int a[] = {2, 1, 4, 6, 5};
    int size = sizeof(a) / sizeof(a[0]);
    int max = a[0];
    int min = a[0];
    
    for (int i = 0; i < size; i++) {
        if (a[i] > max) {
            max = a[i];
        }
        if (a[i] < min) {
            min = a[i];
        }
    }
    printf("max = :%d\n", max);
    printf("min = :%d\n", min);

    return 0;
}

3、将给定的字符串反转;

cpp 复制代码
#include <stdio.h>
#include <string.h>

void reverse(char str[]) {
    int len = strlen(str);
    for (int i = 0; i < len / 2; i++) {
        char temp = str[i];
        str[i] = str[len - i - 1];
        str[len - i - 1] = temp;
    }
}

int main() {
    char str[] = "abcd";
    reverse(str);
    printf("reverse:%s\n", str);

    return 0;
}

4、判定一个给定的字符串是否是回文字符串;

cpp 复制代码
#include <stdio.h>
#include <string.h>

int palindrome(char str[]) {
    int len = strlen(str);
    for (int i = 0; i < len / 2; i++) {
        if(str[i] != str[len - i - 1]) {
            return 0;
        }
    }
    return 1;
}

int main() {
    char str[] = "level";
    if(palindrome(str)) {
        printf("%s is palindrome\n", str);
    } else {
        printf("%s is not a palindrome\n", str);
    }
    return 0;
}

5、计算一个给定数字的阶乘;

cpp 复制代码
#include <stdio.h>

int factorial(int n) {
    if (n == 0 || n == 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

int main() {
    int num = 5;
    int result = factorial(num);
    printf("factorialof %d is %d\n", num, result);
    return 0;
}

6、找出一个对给定数组中的所有重复元素;

cpp 复制代码
#include <stdio.h>

void findDuplicates(int a[], int size) {
    for (int i = 0; i < size; i++) {
        for(int j = i + 1; j < size; j++) {
            if (a[i] == a[j]) {
                printf("%d is aduplicate\n", a[i]);
            }
        }
    }
}

int main() {
    int a[] = {2, 3, 4, 2, 5, 6, 4, 3};
    int size = sizeof(a) / sizeof(a[0]);
    findDuplicates(a, size);
    return 0;
}

7、将一个给定的整数数组按升序排序;

cpp 复制代码
#include <stdio.h>

void bubbleSort(int a[], int size) {
    for (int i = 0; i < size - 1; i++) {
        for (int j = 0; j < size - i - 1; j++) {
            if (a[j] > a[j + 1]) {
                int temp = a[j];
                a[j] = a[j + 1];
                a[j + 1] = temp;
            }
        }
    }
}

int main() {
    int a[] = {5, 2, 9, 1, 7};
    int size = sizeof(a) / sizeof(a[0]);
    bubbleSort(a, size);
    printf("Sorted array in ascending order: ");
    for (int i = 0; i < size; i++) {
        printf("%d ", a[i]);
    }
    printf("\n");
    return 0;
}

8、判断一个给定的字符串是否是有效的ip地址;

cpp 复制代码
#include <stdio.h>
#include <string.h>

int ipAddress(char str[]) {
    int count = 0;
    char *token = strtok(str, ".");

    while (token != NULL) {
        int num = atoi(token);
        if (num < 0 || num > 255) {
            retturn 0;
        }
        count++;
        token = strtok(NULL, ".");
    }
    return (count == 4);
}

int main() {
    char str[] = "192.168.0.1";
    if(ipAddress(str)) {
        printf("%s 是一个有效IP地址\n", str);
    } else {
        printf("%s 不是一个有效的IP地址\n", str);
    }
    return 0;
}

9、判断一个给定的字符串是否是有效的括号序列(左右括号数量是否一致);

cpp 复制代码
#include <stdio.h>
#include <string.h>

int vaild(char str[]) {
    int len = strlen(str);
    int count = 0;

    for (int i = 0; i < len; i++) {
        if (str[i] == '(')  {
            count++;
        } else if (str[i] == ')') {
            count--;
        }
        if (count < 0) {
            return 0;
        }
    }
    return (count == 0);
}

int main() {
    char str[] = "((()))";
    if (vaild(str)) {
        printf("%s 是一个有效的括号\n", str);
    } else {
        printf("%s 不是一个有效的括号\n", str);
    }
    return 0;
}

10、将一个给定的字符串中的所有空格替换为"%20";

cpp 复制代码
#include <stdio.h>
#include <string.h>

void spaces(char str[]) {
    int len = strlen(str);
    int count = 0;

    for (int i = 0; i < len; i++) {
        if (str[i] = ' ') {
            count++;
        }
    }

    int newlen = len + 2 * count;
    str[newlen] = '\0';

    for(int i = len - 1; i >= 0; i--) {
        if (str[i] == ' ' ) {
            str[newlen - 1] = '0';
            str[newlen - 2] = '2';
            sre[newlen - 3] = '%';
            newlen--;
        } else {
            str[newlen - 1] = str[i];
            newlen--;
        }
    }
}

int main () {
    char str[] = "hello world";
    spaces(str);
    printf("空格替换后的字符串:%s\n", str);
    return 0;
}
相关推荐
浅陌pa13 分钟前
01:(寄存器开发)点亮一个LED灯
c语言·stm32·单片机·嵌入式硬件
爱叨叨的小嘟22 分钟前
windows配置C++编译环境和VScode C++配置(保姆级教程)
c++·windows·vscode
武昌库里写JAVA37 分钟前
Vue3常用API总结
数据结构·spring boot·算法·bootstrap·课程设计
C++忠实粉丝39 分钟前
位运算(7)_消失的两个数字
算法
卑微求AC39 分钟前
(C语言贪吃蛇)4.贪吃蛇地图优化及算法说明
c语言·算法
sjsjs1140 分钟前
【动态规划-最长公共子序列(LCS)】【hard】力扣1458. 两个子序列的最大点积
算法·leetcode·动态规划
qq_5352461441 分钟前
代码随想录 101. 孤岛的总面积
算法·深度优先·图论
sjsjs1141 分钟前
【动态规划-最长公共子序列(LCS)】力扣583. 两个字符串的删除操作
算法·leetcode·动态规划
陈序缘44 分钟前
LeetCode讲解篇之79. 单词搜索
算法·leetcode·职场和发展
南石.1 小时前
JVM 基础、GC 算法与 JProfiler 监控工具详解
jvm·算法