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;
}
相关推荐
van叶~4 分钟前
Linux网络-------1.socket编程基础---(TCP-socket)
linux·网络·tcp/ip
风吹落叶花飘荡16 分钟前
Ubuntu系统 系统盘和数据盘扩容具体操作
linux·运维·ubuntu
go546315846520 分钟前
基于深度学习的食管癌右喉返神经旁淋巴结预测系统研究
图像处理·人工智能·深度学习·神经网络·算法
QQ_43766431430 分钟前
C++11 右值引用 Lambda 表达式
java·开发语言·c++
aramae30 分钟前
大话数据结构之<队列>
c语言·开发语言·数据结构·算法
大锦终41 分钟前
【算法】前缀和经典例题
算法·leetcode
zoulingzhi_yjs43 分钟前
haproxy配置详解
linux·云原生
bingbingyihao44 分钟前
Node.js 模拟 Linux 环境
linux·node.js
大神的风范1 小时前
从0开始学linux韦东山教程Linux驱动入门实验班(5)
linux
想变成树袋熊1 小时前
【自用】NLP算法面经(6)
人工智能·算法·自然语言处理