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;
    }
  • 相关阅读:
    Android Studio 开发环境设置
    Android-项目介绍
    Android-开发工具
    在js 中使用ajax 调用后台代码方法,解析返回值
    $.each解析json
    VS2008 "当前不会命中断点。源代码与原始版本不同"解决方法
    64位系统 安装oracle
    session丢失返回登陆页
    DataTable转换为JsonResult
    easyui 绑定数据
  • 原文地址:https://www.cnblogs.com/wwww2/p/11745459.html
Copyright © 2011-2022 走看看