MT1331-MT1340 码题集 (c 语言详解)

MT1331·用函数求π的近似值

c 语言代码实现

#include <math.h>
#include <stdio.h>
double Fun() {
    double pi_over_4 = 0.0; // Π / 4 的近似值
    double current;         // 当前项
    int i = 0;              // 项的索引
    do {
        current = (i % 2 == 0 ? 1.0 : -1.0) / (2.0 * i + 1.0); // 计算当前项
        pi_over_4 += current; // 累加当前项
        i++;
    } while (fabs(current) >= 1e-6); // 小于等于 10^-6

    return pi_over_4 * 4;
}
int main() {
    double pi_approx = Fun();
    printf("%.2f", pi_approx);
    return 0;
}

MT1332·用函数求最大值

c 语言代码实现

#include <stdio.h>
int max_1(int a, int b) {
    if (a > b)
        return a;
    return b;
}
int max(int a, int b, int c, int d) {
    if (max_1(a, b) > max_1(c, d))
        return max_1(a, b);
    return max_1(c, d);
}
int main() {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    printf("%d",max(a, b, c, d));
    return 0;
}

MT1333·用函数求最小值

c 语言实现代码

#include <stdio.h>
int min_1(int a, int b) {
    if (a > b)
        return b;
    return a;
}
int min(int a, int b, int c, int d) {
    if (min_1(a, b) > min_1(c, d))
        return min_1(c, d);
    return min_1(a, b);
}
int main() {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);
    printf("%d", min(a, b, c, d));
    return 0;
}

MT1334·最小整数

c 语言代码实现

#include <math.h>
#include <stdio.h>
double getceil(double x) { return ceil(x); }
int main() {
    double x;
    scanf("%lf", &x);
    printf("%.0lf", getceil(x));
    return 0;
}

MT1335·最大整数

c 语言代码实现

#include <math.h>
#include <stdio.h>
double getfloor(double x) { return floor(x); }
int main() {
    double x;
    scanf("%lf", &x);
    printf("%.0lf", getfloor(x));
    return 0;
}

MT1336·用函数求阶乘

c语言代码实现

#include <stdio.h>
int fact(int x) {
    if (x == 1)
        return 1;
    return x * fact(x - 1);
}
int main() {
    int x;
    scanf("%d", &x);
    printf("%d", fact(x));
    return 0;
}

MT1337·n次方

c 语言代码实现

#include <math.h>
#include <stdio.h>
int fun(int m, int n) { return pow(m, n); }
int main() {
    int m, n;
    scanf("%d %d", &m, &n);
    printf("%d", fun(m, n));
    return 0;
}

MT1338·开n次方

c 语言代码实现

#include <math.h>
#include <stdio.h>
double S(double x, int n) { return pow(x, 1.0 / n); }
int main() {
    double x;
    int n;
    scanf("%lf %d", &x, &n);
    printf("%lf", S(x, n));
    return 0;
}

MT1339·平均数

c 代码实现

#include <math.h>
#include <stdio.h>

// 函数声明
double calculate_sum_of_squares(double arr[], int n);

int main() {
    // 示例数组
    double data[100];
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%lf", &data[i]);
    }

    // 计算平方差和
    double result = calculate_sum_of_squares(data, n);
    printf("%lf", result);

    return 0;
}

// 计算平方差和的函数
double calculate_sum_of_squares(double arr[], int n) {
    if (n <= 0)
        return 0; // 如果数组长度为0,返回0

    // 计算平均数
    double sum = 0;
    for (int i = 0; i < n; i++) {
        sum += arr[i];
    }
    double mean = sum / n;

    // 计算平方差和
    double sum_of_squares = 0;
    for (int i = 0; i < n; i++) {
        sum_of_squares += pow(arr[i] - mean, 2);
    }

    return sum_of_squares;
}

MT1340·平均值函数

c 语言实现代码

#include <stdio.h>
double avg(int arr[100], int begin, int end) {
    double sum = 0, mean = 0;
    for (int i = begin; i <= end; i++) {
        sum += arr[i];
    }
    mean = sum / (end - begin + 1);
    return mean;
}
int main() {
    int n, begin, end, a[100];
    scanf("%d %d %d", &n, &begin, &end);
    for (int i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    printf("%lf", avg(a, begin, end));
    return 0;
}
相关推荐
m0_7482478014 分钟前
SOA(面向服务架构)全面解析
java·微服务·架构
m0_7401546718 分钟前
Tomcat
java·tomcat
爱敲代码的小冰25 分钟前
【Excel】 Power Query抓取多页数据导入到Excel
android·java·excel
原来是猿36 分钟前
蓝桥备赛(四)- 数组(下)
开发语言·数据结构·c++·算法
心流时间39 分钟前
[Java基础] JVM常量池介绍(BeanUtils.copyProperties(source, target)中的属性值引用的是同一个对象吗)
java·开发语言·jvm
ephemerals__1 小时前
【数据结构进阶】哈希表
数据结构·算法·散列表
瑞金彭于晏1 小时前
通俗易懂版 Maven 科普,maven是什么?
java·maven
嵌入式修炼师1 小时前
C语言 enum 的详细解析:用法、注意事项与易错点
c语言
好看资源平台1 小时前
Java Web开发实战与项目——Spring Boot与Spring Cloud微服务项目实战
java
.猫的树1 小时前
Java集合List快速实现重复判断的10种方法深度解析
java·开发语言·list·集合