当前位置:网站首页>《程序设计基础》 第十章 函数与程序结构 6-13 分治法求解金块问题 (20 分)
《程序设计基础》 第十章 函数与程序结构 6-13 分治法求解金块问题 (20 分)
2022-07-20 16:33:00 【茶然o】
老板有一袋金块(共n块,2≤n≤100),两名最优秀的雇员每人可以得到其中的一块,排名第一的得到最重的金块,排名第二的则得到袋子中最轻的金块。
输入一个正整数N(2≤N≤100)和N个整数,用分治法求出最重金块和最轻金块。
本题要求实现2个函数,分别使用分治法在数组中找出最大值、最小值。
函数接口定义:
int max(int a[ ], int m, int n);
int min(int a[ ], int m, int n);
递归函数max
用分治法求出a[m]~a[n]中的最大值并返回。
递归函数min
用分治法求出a[m]~a[n]中的最小值并返回。
裁判测试程序样例:
#include <stdio.h>
#define MAXN 101
int max(int a[ ], int m, int n);
int min(int a[ ], int m, int n);
int main(void)
{
int i, n;
int a[MAXN];
scanf ("%d", &n);
if(n >= 2 && n <= MAXN-1 ){
for(i = 0; i < n; i++){
scanf ("%d", &a[i]);
}
printf("max = %d\n", max(a, 0, n-1));
printf("min = %d\n", min(a, 0, n-1));
}else{
printf("Invalid Value.\n");
}
return 0;
}
/* 请在这里填写答案 */
输入样例:
6
3 9 4 9 2 4
结尾无空行
输出样例:
max = 9
min = 2
结尾无空行
int max(int a[ ], int m, int n){
int i;
int max = a[0];
for (i = m;i < n+1;i++){
if (a[i] > max){
max = a[i];
}
}
return max;
}
int min(int a[ ], int m, int n){
int i;
int min = a[0];
for (i = m;i < n+1;i++){
if (a[i] < min){
min = a[i];
}
}
return min;
}
边栏推荐
猜你喜欢
廖雪峰老师系列课程 迅速过一遍 1
VC all rested? In the first half of the year, Hillhouse venture capital made nearly 80 shots, 60% before the a-round
马斯克:我把大脑上传云端啦,不好意思,404了
论文笔记:Accurate Causal Inference on Discrete Data
网络安全学习(三)基本DOS命令
Wechat applet 04 route jump and life cycle function
Network Security Learning (VIII) domain
微信小程序 23 播放音乐页
kubernetes调度概念与工作流程
李宏毅老师2020年深度学习系列讲座笔记6
随机推荐
Separable Convolution可分离卷积
Kettle Job实现每6s就运行一个Kettle的转换任务
【2022华为开发者大赛系列直播】数据库老兵+畅销书作者的奇妙组合谁能不期待呢?
Some easily confused pointers [summary direction]
網絡安全學習(七)IIS
网络安全学习(八)域
IBM MQ operation and maintenance manual
yolov5
微信小程序 24 播放音乐页的完善①
Some things about pointer array and array pointer
acmStreamOpen返回值问题
Wechat applet 02 Hello miniprogram
张小泉,冤吗?
视频物体分割VOS
Image and Pattern Classification with Scattering
code snippet
For the IT Internet industry, does family think education is important or ability?
【愚公系列】2022年7月 Go教学课程 013-常量、指针
JD cloud and Forrester consulting released a hybrid cloud report that cloud Nativity has become a new engine driving industrial development
How to effectively avoid code being "poisoned"?