zoukankan      html  css  js  c++  java
  • BJFU-218-基于链式存储结构的图书信息表的最贵图书的查找

    如果编译不通过,可以将C该为C++

    #include<stdio.h>
    #include<stdlib.h>
    #define MAX 100
    //创建节点
    typedef struct Book{
        double no;
        char name[MAX];
        double price;
        struct Book * next;
    }Book,*BList;
    
    //创建链表
    void CreatList(BList &B,int n)
    {
        B = (BList)malloc(sizeof(Book));
        B->next = NULL;
        BList rear = B;
        for(int i=1;i<=n;i++)
        {
           //这里用的是尾插法
           BList p = (BList)malloc(sizeof(Book));
           scanf("%lf",&p->no);
           scanf("%s",p->name);
           scanf("%lf",&p->price);
           if(p->no==0&&p->name[0]=='0'&&p->price==0) break;
           rear->next = p;
           p->next = NULL;
           rear = p;      
        }
    }
    //获得最贵图书的价格,并返回其值
    double getCost(BList &B)
    {
        BList p = B->next;
        double cost = p->price;
        int cout = 0;
        while(p)
        {
            if(p->price > cost)
            {
                cost = p->price;
            }
            p = p->next;
        }
        return cost;
    }
    void traverse(BList B,int n,double cost)
    {
        BList p = B->next;
        int cout = 0;
        for(int i=1;i<=n;i++)
        {
            if(p->price==cost) cout++;     //计算最贵图书的个数
            p = p->next;
        }
        printf("%d
    ",cout);
    
        BList q = B->next;
        for(int i=1;i<=n;i++)
        {
    
            if(q->price==cost)           //如果图书的价格是cost,就输出
            {
                printf("%.0f ",q->no);
                printf("%s ",q->name);
                printf("%.2f",q->price);
                printf("
    ");
    
            }
            q = q->next;
        }
    
    
    }
    
    int main()
    {
        BList B;
        int n;
        double cost;
        scanf("%d",&n);
        CreatList(B,n);
        cost = getCost(B);
        traverse(B,n,cost);
        return 0;
    }
  • 相关阅读:
    OS X EI Capitan 安装mysql-5.7.9
    CAS SSO
    单点登录SSO
    videojs 视频开发API
    NodeJS无所不能:细数10个令人惊讶的NodeJS开源项目
    程序员使用Node的十个技巧
    pdf 回退快捷键
    公式神器 Mathpix Snip 比mathtype快
    AI studio 尝试
    tmux 使用
  • 原文地址:https://www.cnblogs.com/wwww2/p/11745459.html
Copyright © 2011-2022 走看看