C 计算题 SDUT

Time Limit: 1000 ms Memory Limit: 65536 KiB


Problem Description

一个简单的计算,你需要计算f(m,n),其定义如下:
当m=1时,f(m,n)=n;
当n=1时,f(m,n)=m;
当m>1,n>1时,f(m,n)= f(m-1,n)+ f(m,n-1)


Input

第一行包含一个整数T(1<=T<=100),表示下面的数据组数。
以下T行,其中每组数据有两个整数m,n(1<=m,n<=2000),中间用空格隔开。


Output

对每组输入数据,你需要计算出f(m,n),并输出。每个结果占一行。


Sample Input

2
1 1
2 3


Sample Output

1
7


Hint


类似的给出递推公式求职定的项的题目,用函数的递归便能简单的实现;

#include <stdio.h>
#include <stdlib.h>
int f(int m,int n)
{
    int y;
    if(m==1)
        y=n;
    else if(n==1)
        y=m;
    else
        y=f(m-1,n)+f(m,n-1);
    return y;
}
int main()
{

    int T,i,m,n;
    scanf("%d",&T);
    for(i=1; i<=T; i++)
    {
        scanf("%d %d",&m,&n);
        printf("%d\n",f(m,n));
    }

    return 0;
}

### 关于 SDUT C语言 期末考试 PTA 题目的分析 以下是关于山东科技大学 (SDUT) 的 C语言期末考试可能涉及的一些典型 PTA 练习题目及其解析: #### 1. **余弦函数近似计算** 该类题目通常考察学生对泰勒级数展开的理解以及循环控制的应用。 ```c #include <stdio.h> int main() { double x, sum, t, item; int n, i; while (scanf("%lf %d", &x, &n) == 2) { t = x * x; item = 1.0; sum = 1.0; for (i = 1; i <= n; i++) { item *= -t / ((2 * i - 1) * (2 * i)); sum += item; } printf("%.4lf\n", sum); } return 0; } ``` 此代码实现了基于泰勒级数的 `cos(x)` 近似计算[^1]。输入为角度值 `x` 和迭代次数 `n`,输出为四舍五入到四位小数的结果。 --- #### 2. **结构体应用:比赛排名统计** 此类题目主要考查学生对结构体定义、初始化及排序算法的实际运用能力。 ```c #include <stdio.h> #include <stdlib.h> typedef struct Team { long id; int score; } Team; // 比较函数用于 qsort 排序 int compare(const void* a, const void* b) { Team team_a = *(Team*)a; Team team_b = *(Team*)b; if (team_a.score != team_b.score) return team_b.score - team_a.score; // 按分数降序排列 else return team_a.id - team_b.id; // 如果分数相同,则按 ID 升序排列 } int main() { int T, N, i; scanf("%d", &T); while (T--) { scanf("%d", &N); Team teams[N]; for (i = 0; i < N; i++) scanf("%ld %d", &teams[i].id, &teams[i].score); qsort(teams, N, sizeof(Team), compare); for (i = 0; i < N; i++) printf("%ld %d\n", teams[i].id, teams[i].score); } return 0; } ``` 这段代码实现了一个简单的竞赛成绩管理系统[^2]。通过读取多个团队的成绩并按照特定规则排序后输出结果。 --- #### 3. **整除条件判断** 这类基础编程题旨在帮助初学者掌握基本逻辑运算符和分支语句。 ```c #include <stdio.h> int main() { int n; scanf("%d", &n); if (n % 3 == 0 && n % 5 == 0) printf("Yes"); else printf("No"); return 0; } ``` 上述程序验证给定数值是否能同被 3 和 5 整除[^3]。如果满足条件则打印 `"Yes"`;否则打印 `"No"`。 --- #### 4. **数组排序与索引追踪** 本题综合考察了冒泡排序法以及其他辅助功能的设计思路。 ```c #include <stdio.h> void bubble_sort(int arr[], int index[], int n) { int i, j, temp, temp_idx; for (i = 0; i < n - 1; i++) { for (j = 0; j < n - 1 - i; j++) { if (arr[j] > arr[j + 1]) { temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; temp_idx = index[j]; index[j] = index[j + 1]; index[j + 1] = temp_idx; } } } } int main() { int arr[10], index[10], i; for (i = 0; i < 10; i++) { scanf("%d", &arr[i]); index[i] = i + 1; } bubble_sort(arr, index, 10); for (i = 0; i < 10; i++) { if (i == 9) printf("%d", arr[i]); else printf("%d ", arr[i]); } printf("\n"); for (i = 0; i < 10; i++) { if (i == 9) printf("%d", index[i]); else printf("%d ", index[i]); } return 0; } ``` 在此例子中,我们不仅完成了对原始数据列表从小到大重新安排的任务,而且记录下了每一个位置变动前后的映射关系[^4]。 --- ### 总结说明 以上列举了几种常见的针对大学计算机科学课程中的实践作业形式。它们分别涵盖了不同的知识点和技术要点,有助于提高学生的实际动手能力和理论联系实际的能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

碧羽o(* ̄▽ ̄*)ブ回雪

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值