YTU 2952: A代码填充--谁挡住了我

2952: A代码填充--谁挡住了我

时间限制: 1 Sec  内存限制: 128 MB
提交: 135  解决: 38

题目描述

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个人的身高
第三行 小明从前往后数的序号

输出

挡住小明的人数

样例输入

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

样例输出

7

你  离  开  了  ,  我  的  世  界  里  只  剩  下  雨  。  。  。

#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 i=0;
    while(head!=p)
    {
        if(head->height>=p->height)
        {
            i++;
        }
        head=head->next;
    }
    return i;
}
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;
}

原文地址:https://www.cnblogs.com/im0qianqian/p/5989647.html