qq_57125768 2022-03-05 19:29 采纳率: 100%
浏览 591
已结题

统计顺序表中小于指定元素值的个数

本题要求实现一个函数,要求返回顺序表中小于指定元素值的个数,空表返回0。题目保证顺序表中所有元素都为正整数

//顺序表结构体定义
typedef int DataType;
typedef struct node {
DataType data[MAXSIZE];
int length;
}SeqList, * PSeqList;
您要实现的函数为:

int GetSmaller(PSeqList PL, DataType x);
函数GetSmaller返回顺序表L中比x小的元素的个数,若顺序表为空,则返回0

用到的函数有:
PSeqList Init_SeqList( );//创建一个顺序表
int Insert_SeqList( PSeqList PL, int i, DataType x);//在顺序表第i个元素之前插入元素x;
int Destroy_SeqList(&PL);//销毁顺序表
裁判测试程序样例:
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 120
typedef int DataType;
//顺序表结构体定义
typedef struct node {
DataType data[MAXSIZE];
int length;
}SeqList, * PSeqList;

//函数申明区
PSeqList Init_SeqList();//创建一个空顺序表
int Destroy_SeqList(PSeqList *PL);//销毁顺序表
int Insert_SeqList( PSeqList PL, int i, DataType x);//在顺序表中插入
int GetSmaller(PSeqList PL, DataType x);//统计顺序表中小于x的个数

//主函数
int main( )
{
PSeqList PL;
DataType x;
int i=1,count;

PL = Init_SeqList();//创建一个空顺序表

scanf("%d",&x);
while((x!=-1)
{
Inset_SeqList(L,i,x); //在L的第i个元素位置上插入x
i++;
scanf("%d",&x);
}

scanf("\n%d",&x);
count=GetSmaller(PL,x);

printf("The number smaller than %d is %d.\n",x, count);
Destroy_SeqList(&PL);
return 0;
}

/* 请在这里填写答案 */
#输入格式: 输入数据有2行,第一行给出以-1结束的顺序表元素(不超过10000个,-1不属于顺序表元素),所有数据之间用空格分隔。题目保证输入的顺序元素都是整数。第二行给出要指定的元素值。

输入样例:
2 6 4 13 6 9 -1
6
输出样例:
The number smaller than 6 is 2.

  • 写回答

3条回答 默认 最新

  • CSDN专家-link 2022-03-05 19:33
    关注

    啥问题哈
    只需要写GetSmaller,还是所有函数都要写

    int GetSmaller(PSeqList PL, DataType x)
    {
        int count = 0,i;
        for(i=0;i<PL.length;i++)
            if(PL.data[i] < x)
              count++;
        return count;
    }
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 1月17日
  • 已采纳回答 1月9日
  • 创建了问题 3月5日

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作