• 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;
    }
  • 相关阅读:
    golang sync.WaitGroup
    golang 部分理解:关于channel 和 goroutine 例子
    golang filepath.Walk遍历指定目录下的所有文件
    golang filepath.Glob
    golang 函数传值
    golang panic and recover
    golang pipe
    golang 获取指定目录下的子文件列表
    eclipse:failed to create the java virtual machine
    如何在股市中捕捉涨停
  • 原文地址:https://www.cnblogs.com/wwww2/p/11745459.html
Copyright © 2020-2023  润新知