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;
}
相关推荐
彧azz13 分钟前
算法设计与分析:贪心与动态规划
数据结构·学习·算法·贪心算法·动态规划
泡海椒14 分钟前
PDF 表格样式优化:jquick-pdf 边框、圆角、背景色
java·开发语言·pdf
Beyond_System|系统之外1 小时前
【学编程】Python基础编程题100道(21-60)
开发语言·python·算法
景熙55231 小时前
15.Java 8 Stream 流入门到实战
java·开发语言·数据结构
wno7042 小时前
Spring Security权限控制
java·python·spring
杨运交2 小时前
[071][验证码模块]基于Spring拦截器的验证码认证设计思想
java·后端·spring
Geek-Chow3 小时前
CountDownLatch in Java
java
漂流瓶jz3 小时前
UVA-1442 洞穴 题解答案代码 算法竞赛入门经典第二版
c++·算法·题解·aoapc·算法竞赛入门经典·uva
SL_staff3 小时前
从RBAC到场景化授权:《无忧·企业文档》三级权限模型的技术实践解析
java·开源·产品
传奇开心果编程4 小时前
【springboot基础语法学与练】第 1 课:从零开始
java·spring boot·后端·学习