zoukankan      html  css  js  c++  java
  • 数据结构实验之栈五:下一较大值(一)

    数据结构实验之栈五:下一较大值(一)

    Time Limit: 1000MS Memory Limit: 65536KB

    Problem Description

    对于包含n(1<=n<=1000)个整数的序列,对于序列中的每一元素,在序列中查找其位置之后第一个大于它的值,如果找到,输出所找到的值,否则,输出-1。

    Input

     输入有多组,第一行输入t(1<=t<=10),表示输入的组数;

    以后是 t 组输入:每组先输入n,表示本组序列的元素个数,之后依次输入本组的n个元素。

    Output

     输出有多组,每组之间输出一个空行(最后一组之后没有);

    每组输出按照本序列元素的顺序,依次逐行输出当前元素及其查找结果,两者之间以-->间隔。

    Example Input

    24 12 20 15 185 20 15 25 30 6 

    Example Output

    12-->2020-->-115-->1818-->-120-->2515-->2525-->3030-->-16-->-1

    #include<iostream>
    #include<stdlib.h>
    using namespace std;
    typedef struct node
    {
        int data1;
        int data2;
        struct node *next;
    }node;
    #define god (node *)malloc(sizeof(node))
    node *creat(int n)
    {
        node *head,*p,*tail;
        head = god;
        head->next = NULL;
        tail = head;
        for(int i=0;i<n;i++)
        {
            p = god;
            cin>>p->data1;
            p->next = NULL;
            tail->next = p;
            tail = p;
        }
        tail->next = NULL;
        return head;
    }
    void display(node *head)
    {
        node *p=head->next;
        while(p->next)
        {
            cout<<p->data1<<"-->"<<p->data2<<endl;
            p=p->next;
        }
        cout<<p->data1<<"-->"<<"-1"<<endl;
    }
    void find(node *head)
    {
        int flag = 0;
        node *p=head->next,*q,*t;
        while(p)
        {
           t = p->next;
           flag = 0;
           while(t)
           {
               if(p->data1<t->data1)
               {
                   p->data2 = t->data1;break;
                   flag = 1;
               }
               else{p->data2 = -1;}
               t=t->next;
           }
           p=p->next;
        }
        display(head);
    }
    int main()
    {
        node *head;
        int n,T;
        cin>>T;
        while(T--)
        {
        cin>>n;
        head = creat(n);
        find(head);
        if(T>=1)
        {
            cout<<endl;
        }
        }
    
    
    }
    

  • 相关阅读:
    Linux系统格式化磁盘+挂载分区
    ELK日志处理
    memcached安装+绑定访问ip
    Redis安装+密码认证
    linux安全
    Mysql备份与恢复
    MySql binlog
    gitlab自动备份
    Rabbitmq安装与配置
    Hadoop实战-Flume之Source interceptor(十一)(2017-05-16 22:40)
  • 原文地址:https://www.cnblogs.com/CCCrunner/p/6444593.html
Copyright © 2011-2022 走看看