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;
}
相关推荐
lisw056 小时前
稀疏算法概述!
算法·机器人·无人机
qq_452396237 小时前
第三篇:Transactional Outbox 模式详解
java·kubernetes·rocketmq
3095566667 小时前
股票接口重试怎么分类:正常、空数组与错误响应的幂等处理
java·大数据·开发语言·数据库·php·api接口·股票数据api接口
YLCHUP8 小时前
【括号匹配+dp优化】题解:双端括号(brackets)_动态规划_状态优化_数学建模_贪心_性质挖掘_STLmap_C++算法竞赛
数据结构·c++·笔记·算法·数学建模·贪心算法·动态规划
郝学胜-神的一滴9 小时前
Effective STL 条款09:慎重选择删除元素的方法
开发语言·网络·数据结构·c++·算法·stl
FlightYe12 小时前
linux系统编程(二十一):C 语言常见坑
android·linux·运维·服务器·c语言·音视频
geovindu13 小时前
python:Logistic Regression
开发语言·后端·python·算法
摇滚侠14 小时前
《On Java 中文版 基础卷》阅读笔记 内部类 21
java·开发语言·笔记
番茄味城市甜菜14 小时前
二叉树刷题(C语言)
c语言·开发语言