(上高中之前173现在183 )A代码填充--谁挡住了我

题目描述

n个人前后站成一列,对于队列中的任意一个人,如果排在他前面的人的身高大于等于他的身高,则称该人被挡住了。小明是队列中的一员,问有多少人挡住了他?

注:本题只需要提交填写部分的代码,请按照C++方式提交。

#include <iostream>
using namespace std;
struct Node
{
    float height;
    Node *next;
};
Node *creatlist(int n)
{
    Node *t=new Node;
    cin>>t->height;
    if(n>1)
        t->next = creatlist(n-1);
    else
        t->next = NULL;
    return t;
}
Node *findlist(Node *head,int n)
{
    if(n<1||!head)
        return NULL;
    if(n==1)
        return head;
    return findlist(head->next,n-1);
}
int countlist(Node *head,Node *p)
{
    if(!head||!p||head==p)
        return 0;
/*
    请在该部分补充缺少的代码
*/
}
int main(void)
{
    int n,pos;
    Node *head,*xiaoming;
    cin>>n;  //人数
    head = creatlist(n);
    cin>>pos; //小明的序号
    xiaoming = findlist(head,pos);
    cout<<countlist(head,xiaoming)<<endl;
    return 0;
}

输入

第一行 n
第二行 n个人的身高
第三行 小明从前往后数的序号

输出

挡住小明的人数

样例输入

copy
10 
1.86 1.74 1.67 1.87 1.68 1.9 1.65 1.65 1.68 1.65
8

样例输出

7
    else
    {
        Node *t=head;///定义一个节点指向头结点
        int sum=0;
        while(t!=p)
        {///只需要在前面找
            if(t->height>=p->height)
                sum++;///注意是大于等于他的身高
            t=t->next;///节点移动
        }
        return sum;///返回值在else里面
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值