MT1341·反比例函数
c 语言实现代码
#include <stdio.h>
double f(double x) { return 1.0 / x; }
double trapezoidal_integration(double a, double b, int n) {
// computer step length
double h = (b - a) / n;
// computer points value
double sum = (f(a) + f(b)) / 2.0;
// summation middle function value
for (int i = 1; i < n; i++) {
double x = a + i * h;
sum += f(x);
}
// last: mul * step
return sum * h;
}
int main() {
double a, b;
int n = 1000;
scanf("%lf %lf", &a, &b);
if (a > 0 && a < b) {
double result = trapezoidal_integration(a, b, n);
printf("%.2lf", result);
}
return 0;
}
MT1342·函数积分
c 语言实现代码
#include <math.h>
#include <stdio.h>
// 定义函数 f(x) = 1 / (1 + x)
double f(double x) { return 1.0 / (1.0 + x); }
int main() {
int a, b;
double ans = 0, h = 0.001;
scanf("%d %d", &a, &b);
double x = a;
while (x + h <= b) {
ans += (f(x) + f(x + h)) * h / 2;
x += h;
}
printf("%lf",ans);
return 0;
}
MT1343·积分
c 语言代码实现
#include <math.h>
#include <stdio.h>
// 定义函数 f(x)=1/(1+x*x),
double f(double x) { return 1.0 / (1.0 + x * x); }
int main() {
int a, b;
double ans = 0, h = 0.001;
scanf("%d %d", &a, &b);
double x = a;
while (x + h <= b) {
ans += (f(x) + f(x + h)) * h / 2;
x += h;
}
printf("%lf",ans);
return 0;
}
MT1344·组合
c 语言代码实现
#include <stdio.h>
int fac(int n) {
if (n == 1 || n == 0)
return 1;
int result = 1;
for (int i = 2; i <= n; i++) {
result *= i; // 计算 n!
}
return result;
}
int C(int m, int n) {
if (m < 0 || m > n)
return 0;
return fac(n) / (fac(m) * fac(n - m));
}
int main() {
int m, n;
scanf("%d %d", &m, &n);
printf("%d", C(n, m));
return 0;
}
MT1345·分段函数
c 语言代码实现
#include <math.h>
#include <stdio.h>
double Y(double x) {
if (x >= 0)
return sqrt(x);
return x + 33;
}
int main() {
double x;
scanf("%lf", &x);
printf("%lf", Y(x));
return 0;
}
MT1346·加密
c 语言代码实现
#include <stdio.h>
int encrypt(int number) {
int digits[4];
for (int i = 3; i >= 0; i--) {
digits[i] = number % 10;
number /= 10;
}
for (int i = 0; i < 4; i++) {
digits[i] = (digits[i] + 5) % 10;
}
int temp = digits[0];
digits[0] = digits[3];
digits[3] = temp;
temp = digits[1];
digits[1] = digits[2];
digits[2] = temp;
int encrypted_number = 0;
for (int i = 0; i < 4; i++) {
encrypted_number = encrypted_number * 10 + digits[i];
}
return encrypted_number;
}
int main() {
int number;
scanf("%d", &number);
int encrypted_number = encrypt(number);
printf("%d", encrypted_number);
return 0;
}
MT1347·数组计算
c 语言代码实现
#include <stdio.h>
void add(int A[], int B[], int C[], int N) {
for (int i = 0; i < N; i++) {
C[i] = A[i] + B[i];
}
}
void subtract(int A[], int B[], int C[], int N) {
for (int i = 0; i < N; i++) {
C[i] = A[i] - B[i];
}
}
void multiply(int A[], int B[], int C[], int N) {
for (int i = 0; i < N; i++) {
C[i] = A[i] * B[i];
}
}
void divide(int A[], int B[], int C[], int N) {
for (int i = 0; i < N; i++) {
if (B[i] != 0) {
C[i] = A[i] / B[i];
} else {
printf("Error: Division by zero at index %d\n", i);
C[i] = 0;
}
}
}
int main() {
int N;
scanf("%d", &N);
int A[N], B[N], C[N];
for (int i = 0; i < N; i++) {
scanf("%d", &A[i]);
}
for (int i = 0; i < N; i++) {
scanf("%d", &B[i]);
}
char operator;
scanf(" %c", &operator); // 注意前面的空格,确保读取字符时跳过空白字符
// 根据运算符调用相应的函数
switch (operator) {
case '+':
add(A, B, C, N);
break;
case '-':
subtract(A, B, C, N);
break;
case '*':
multiply(A, B, C, N);
break;
case '/':
divide(A, B, C, N);
break;
default:
printf("无效的运算符\n");
return 1;
}
// 输出结果
for (int i = 0; i < N; i++) {
printf("%d ", C[i]);
}
printf("\n");
return 0;
}
MT1348·方程系数
c 语言代码实现
#include <stdio.h>
// 函数:求两个数的最大公约数 (GCD)
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
void min(int a, int b) {
int g = gcd(a, b);
int x = b / g;
int y = a / g;
printf("%d %d", x, y);
}
int main() {
int a, b;
scanf("%d %d", &a, &b);
min(a, b);
return 0;
}
MT1349·欧几里德算法
c 语言实现代码
#include <stdio.h>
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d",gcd(a, b));
return 0;
}
MT1350·分数计算
#include <stdio.h>
#include <stdlib.h>
// 计算最大公约数
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
// 化简分数
void simplify(int *numerator, int *denominator) {
int g = gcd(abs(*numerator), abs(*denominator));
*numerator /= g;
*denominator /= g;
}
// 处理分数加减运算
void fraction_operation(const char *input) {
int A, B, C, D;
char op;
// 解析输入
sscanf(input, "%d/%d %c %d/%d", &A, &B, &op, &C, &D);
int numerator, denominator;
// 根据运算符进行加法或减法
if (op == '+') {
numerator = A * D + B * C; // A/B + C/D = (A*D + B*C) / (B*D)
denominator = B * D;
} else if (op == '-') {
numerator = A * D - B * C; // A/B - C/D = (A*D - B*C) / (B*D)
denominator = B * D;
} else {
printf("无效的运算符\n");
return;
}
// 化简分数
simplify(&numerator, &denominator);
// 输出结果
if (numerator == 0) {
printf("0\n");
} else {
// 确保分母为正
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
printf("%d/%d\n", numerator, denominator);
}
}
int main() {
char input[50];
// 输入分数运算
fgets(input, sizeof(input), stdin);
// 处理分数运算
fraction_operation(input);
return 0;
}