本题要求实现一个函数,要求返回顺序表的最大值,空表返回0。题目保证顺序表中所有元素都为正整数。
函数接口定义:
int GetMax(SqList L);
其中SqList结构定义如下:
typedef struct
{
ElemType *elem;
int length;
}SqList;
裁判测试程序样例:
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 5
typedef int ElemType;
typedef struct{
ElemType *elem;
int length;
}SqList;
void InitList(SqList &L);/*细节在此不表*/
int GetMax(SqList L);
int main()
{
SqList L;
InitList(L);
int p;
p=GetMax(L);
if(p) printf("The max of SequenceList L is %d.\n",p);
else printf("The SequenceList is null.\n");
return 0;
}
/* 请在这里填写答案 */
输入格式:
输入数据有1行,首先给出以-1结束的顺序表元素(不超过100个,-1不属于顺序表元素),所有数据之间用空格分隔。题目保证输入的顺序元素都是正整数。
输入样例:
2 6 4 13 9 -1
输出样例:
The max of SequenceList L is 13.
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
题解:
int GetMax(SqList L){
int max = 0;
for (int i = 0; i < L.length; i++) {
if (L.elem[i] > max) {
max = L.elem[i];
}
}
return max;
}
本文介绍了一个C语言函数GetMax,用于在顺序表(SqList)中找到最大值。函数通过遍历结构中的元素,找出并返回最大整数值,如果列表为空则返回0。
1553

被折叠的 条评论
为什么被折叠?



