zoukankan      html  css  js  c++  java
  • vj p1357

    题目描述

    此乃水题也!

    既然如此,为何还要写这个题解呢?当然,他水了,我不能水,所以水题就用树来完成!

    先建个排序二叉树,然后来个中序遍历,一样搞定!

    速度很快!有图有真相:

    代码如下

    type
            link
    =^point;
            point
    =record
                    l,r:link;
                    da,t:longint;
            
    end;
    var
            head:link;
    procedure add(h:link;m:longint);
    var
            t:link;
    begin
            
    if m<h^.da then
                    
    begin
                            
    if h^.l<>nil then
                                    
    begin
                                            add(h^.l,m);
                                    
    end
                            
    else
                                    
    begin
                                            new(t);
                                            t^.l:
    =nil;t^.r:=nil;
                                            t^.da:
    =m;
                                            t^.t:
    =1;
                                            h^.l:
    =t;
                                    
    end;
                    
    end
            
    else
            
    if m=h^.da then inc(h^.t)
            
    else
            
    if m>h^.da then
                    
    begin
                            
    if h^.r<>nil then
                                    
    begin
                                            add(h^.r,m);
                                    
    end
                            
    else
                                    
    begin
                                            new(t);
                                            t^.l:
    =nil;t^.r:=nil;
                                            t^.da:
    =m;
                                            t^.t:
    =1;
                                            h^.r:
    =t;
                                    
    end;
                    
    end;
    end;
    procedure print(h:link);
    begin
            
    if h<>nil then
                    
    begin
                            print(h^.l);
                            writeln(h^.da,
    ' ',h^.t);
                            print(h^.r);
                    
    end;
    end;
    procedure init;
    var
            n,i,a:longint;
    begin
            new(head);head^.l:
    =nil;head^.r:=nil;
            readln(n);
            readln(a);head^.da:
    =a;head^.t:=1;
            
    for i:=2 to n do
                    
    begin
                            readln(a);
                            add(head,a);
                    
    end;
    end;
    begin
            init;
            print(head);
    end.
  • 相关阅读:
    常用 Git 命令清单
    radhat 6.4/centos 6.4 下编译安装 最新ruby 2.1.5
    centos 6.4/redhat 6.4 安装gitlab
    微信小程序——navigator无法跳转
    微信小程序——修改data里面数组某一个值
    微信小程序——template的循环嵌套
    微信小程序——template的使用方法
    node学习笔记8——发布npm包
    淘宝镜像使用方法
    node学习笔记7——npm安装包
  • 原文地址:https://www.cnblogs.com/waterfalleagle/p/1593817.html
Copyright © 2011-2022 走看看