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;
}
相关推荐
疯疯癫癫才自由几秒前
爬取Leetcode Hot 100 题单
算法·leetcode
WolfGang0073214 分钟前
代码随想录算法训练营Day33 | 322.零钱兑换、279.完全平方数、139.单词拆分、背包总结
算法
CoderYanger7 分钟前
递归、搜索与回溯-综合练习:28.不同路径Ⅲ
java·算法·leetcode·深度优先·1024程序员节
可峰科技9 分钟前
Apriltag_ros CMakeList.txt一句话导致其他包编译失败
c++
code bean9 分钟前
【C++ 】C++ 与 C#:using 关键字、命名空间及作用域解析符对比
开发语言·c++·c#
我发在否9 分钟前
Rust > 牛客OJ在线编程常见输入输出练习场
算法·rust
CAE虚拟与现实10 分钟前
C# 调用 DLL为什么不像 C/C++调用 DLL 时需要lib库
开发语言·c++·c#·动态链接库·dll库·lib库
忆湫淮10 分钟前
ENVI 5.6 利用现场标准校准板计算地表反射率具体步骤
大数据·人工智能·算法
Larry_Yanan11 分钟前
Qt线程使用(一)直接继承QThread类
开发语言·c++·qt·ui
XH-hui12 分钟前
【打靶日记】群内靶机 Open
linux·网络安全